Вопрос

I'm a beginner with batch. I would like to copy file in \Debug\test.ext into ..\..\new by batch command.

I tried copy "\Debug\text.txt" "..\..\new", but get error "the system cannot find the file specified"

Это было полезно?

Решение

if you start your path with \, it's an absolute, not a relative path. Try copy "Debug\text.txt" "..\..\new" instead

Другие советы

if you have Debug subdir try with

md   "..\..\new" >nul 2>&1
copy ".\Debug\text.txt" "..\..\new"

md will create a new directory two levels up if you don't have it already.

If you are using bat for running the copy commands, your syntax should be.

bat 'copy "C:\\Program Files (x86)\\ApplicationFiles\\firstfolder\\application.yml" "C:\\Program Files (x86)\\ApplicationFiles\\secondfolder\\application.yml"'

There are two main things in this above command:

  1. If your file path contains spaces then enclosed the path with double quotes.
  2. Use escape character while using windows file path, as some command line tools require file path to be escaped. So instead of using '\' use '\\'. This will help you when you are running bat commands in CI/CD tools.

The proper command in Windows 10 for relative paths is:
copy "%~dp0%\your_file_name.extension" "%systemdrive%\Some Folder\Other Folder"
Explanation:

  • the quotes are nedded when the files or folders contains spaces.
  • The %~dp0% set the current directory. No worry about drive letter or how many levels folders you have. It can be used for forward and backward folders as "%~dp0%\subfolder\your_file.ext" or "%~dp0%\..\..\your_file.ext" (>>> for backward, please consult this link Get the path two directories up in batch file )
  • the %systemdrive% is as it name, the drive were the Windows system files are.

It means that you have not specified the correct path. Make sure you specify correct full path of the file. .

Instead of "\Debug\text...." specify the entire path like "C:\Debug\text..."

To properly refer to the complete path, need to include path with drives along with folder names and file name having correct file format.

for example :
"C:\Testfolder\test.txt"

if referring in network then " \\192.168.1.225\c$\testfolder\test.txt"

It will work correctly .

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top