Copy file from batch file directory

I know only the basics of writing batch files. I'm trying to figure out how to write one that, given any directory in which it is located, will copy the file located in the same directory and put it in a new location. I know how to copy a file and move it, but I don’t know how to write a batch file to understand its directory and then capture another file.

I read that% 0 represents the directory where the file is located, but how can I add a file to it?

I tried this:

copy "%0\Move.txt" "C:\" 

It might have been stupid, but I'm new. Help me please?

+7
windows batch-file command-prompt
source share
2 answers

%0 contains the full path and file name for the script package.

Use only %~dp0 to get the path without the script file name.

 copy "%~dp0\Move.txt" "C:\" 

Use the echo command to see which variable runs when a problem occurs.

 echo %0 

From call /?

 Substitution of batch parameters (%n) has been enhanced. You can now use the following optional syntax: %~1 - expands %1 removing any surrounding quotes (") %~f1 - expands %1 to a fully qualified path name %~d1 - expands %1 to a drive letter only %~p1 - expands %1 to a path only %~n1 - expands %1 to a file name only %~x1 - expands %1 to a file extension only %~s1 - expanded path contains short names only %~a1 - expands %1 to file attributes %~t1 - expands %1 to date/time of file %~z1 - expands %1 to size of file %~$PATH:1 - searches the directories listed in the PATH environment variable and expands %1 to the fully qualified name of the first one found. If the environment variable name is not defined or the file is not found by the search, then this modifier expands to the empty string The modifiers can be combined to get compound results: %~dp1 - expands %1 to a drive letter and path only %~nx1 - expands %1 to a file name and extension only %~dp$PATH:1 - searches the directories listed in the PATH environment variable for %1 and expands to the drive letter and path of the first one found. %~ftza1 - expands %1 to a DIR like output line In the above examples %1 and PATH can be replaced by other valid values. The %~ syntax is terminated by a valid argument number. The %~ modifiers may not be used with %* 
+17
source share

If you are just trying to copy a file from the current directory to a new one, you can simply do this:

 copy "Move.txt" "C:\" 

Not prefixing the file "Move.txt" means that it is in the current directory.

+2
source share