I have a problem with my batch file. I want to copy some files using xcopy, but it isn't working.
#echo off
md %systemroot%\system32\hello
md %systemroot%\system32\wlppr
xcopy /y "%cd%\qwerty.bmp" "%systemroot%\system32\hello"
xcopy /y "%cd%\qwerty2.bmp" "%systemroot%\system32\wlppr"
The folders create succesfully, but when Windows tries to copy files this message appears in CMD:
File not found - qwerty.bmp
0 File(s) copied
File not found - qwerty2.bmp
0 File(s) copied
And yes, I tried to run the file with administrator privileges.
What am I doing wrong?
By double clicking on a batch file the Windows command interpreter cmd.exe is executed with using implicitly option /C to close the console window automatically and terminate the command process when batch processing is finished after successful execution or an exit because of a syntax error. This behavior is not good for debugging a batch file not working as expected. Better for debugging a batch file is opening a command prompt window resulting in running cmd.exe with using implicitly option /K to keep open the console window and let the command process continue running after executing a command like a batch file (not containing exit without /B) to view all error messages output during execution of the batch file.
Double clicking on a batch file results usually in executing the batch file in the directory of the batch file. For that reason the current directory is the directory of the batch file.
But if the batch file is stored on a network share opened in Windows Explorer with a UNC path, Windows command interpreter outputs usually a message that the current directory is set to %SystemRoot% before executing the first command line from batch file. This behavior can be turned off via a special Windows registry value.
And if the batch file is executed with right clicking on it and using context menu option Run as administrator, the Windows command process is started with %SystemRoot%\System32 as current directory resulting in executing the batch file with the Windows system directory as current directory.
So it is advisable to write this batch file to work independent on current directory. A good idea would be to put the two bitmap files to copy into same directory as the batch file and use %~dp0 to reference the directory of the batch file independent on what is the current directory on execution of the batch file. %~dp0 references drive and path of argument 0 which is the name of the batch file. Run call /? in a command prompt window for details on referencing batch file arguments.
The command XCOPY creates automatically the entire folder tree on copying 1 or more files if the target string ends with a backslash making it clear for xcopy that the target is a folder name and not a file name.
So the commands to use are:
%SystemRoot%\System32\xcopy.exe "%~dp0qwerty.bmp" "%SystemRoot%\System32\hello\" /Q /Y >nul
%SystemRoot%\System32\xcopy.exe "%~dp0qwerty2.bmp" "%SystemRoot%\System32\wlppr\" /Q /Y >nul
The folder path of batch file referenced with %~dp0 always ends with a backslash. Therefore no additional backslash is specified in both source file name strings.
Run in a command prompt window xcopy /? for help on this command.
How can I edit path which i get from command line arguments and delete last subdirectory?
e.g. I run my batch file with argument:
C:\Users\Aga\Desktop\something
and I want to use only
"C:\Users\Aga\Desktop"
part in my batch file.
Add this as the very first line of your batch file:
#If Not "%~dp1"=="" #(Set "Parent=%~dp1"&Call Set "Parent=%%Parent:~,-1%%")
Then use %Parent% throughout your script as necessary, (%Parent% will be the next directory up the tree if the input was a directory or the container folder if the input was a file).
I am trying to create a batch file whereby it opens files from the start to the end of the folder. At the same time, it needs to save it as a different file type.
Try this:
%SystemRoot%\explorer.exe "folder path"
Or
START "FolderPath"
Without Quotes
I want to create a few batch files to automate a program.
My question is when I create the batch file, what is the current directory?
Is it the directory where the file is located or is it the same directory that appears in the command prompt, or something else?
From within your batch file:
%cd% refers to the current working directory (variable)
%~dp0 refers to the full path to the batch file's directory (static)
%~dpnx0 and %~f0 both refer to the full path to the batch directory and file name (static).
See also: What does %~dp0 mean, and how does it work?
It usually is the directory from which the batch file is started, but if you start the batch file from a shortcut, a different starting directory could be given. Also, when you'r in cmd, and your current directory is c:\dir3, you can still start the batch file using c:\dir1\dir2\batch.bat in which case, the current directory will be c:\dir3.
In a batch file, %cd% is the most commonly used command for the current directory, although you can set your own variable:
set mypath=%cd%
echo %mypath% (where %mypath% is the current directory that the batch file is sitting in)
So say you were wanting to open Myprog.exe. If it was in the same folder, you would use the command:
start %mypath%\Myprog.exe
That would open Myprog from the current folder.
The other option is to make a directory in C: called AutomatePrograms. Then, you transfer your files to that folder then you can open them using the following command:
start "" "C:\AutomatePrograms\Myprog1.exe"
start "" "C:\AutomatePrograms\Myprog2.exe"
start "" "C:\AutomatePrograms\Myprog3.exe"
Say you were opening a file in your current directory. The command would be:
start %cd%\filename.filetype
I hope I answered your question.
It is the directory from where you run the command to execute your batch file.
As mentioned in the above answers you can add the below command to your script to verify:
> set current_dir=%cd%
> echo %current_dir%
%__CD__% , %CD% , %=C:%
There's also another dynamic variable %__CD__% which points to the current directory but unlike %CD% it has a backslash at the end.
This can be useful if you want to append files to the current directory. Also %CD% does not work under disabled extensions environment ,but %__CD__% always works.
With %=C:% %=D:% you can access the last accessed directory for the corresponding drive. If the variable is not defined you haven't accessed the drive on the current cmd session.
And %__APPDIR__% expands to the executable that runs the current script a.k.a. cmd.exe directory.
It is the directory from where you start the batch file. E.g. if your batch is in c:\dir1\dir2 and you do cd c:\dir3, then run the batch, the current directory will be c:\dir3.
Just my 2 cents.
The following command fails if called from batch file (Windows 7) placed on a pendrive:
%SystemRoot%\System32\xcopy.exe /e /i "%cd%Ala" "C:\KS\Ala\"
But this does the job:
%SystemRoot%\System32\xcopy.exe /e /i "%~dp0Ala" "C:\KS\Ala\"
Your bat file should be in the directory that the bat file is/was in when you opened it. However if you want to put it into a different directory you can do so with cd [whatever directory]
I want to search a file in the current directory from which the batch is running, append the filename to the directory and include that entire directory as part of command that.
So.....
Directory:
C:\tempfiles\batch
Files in C:\tempfiles\batch
tmp1.txt
tmp2.txt
tmp3.txt
anyname.exe
I want the batch file, run from the directory, to find any .exe file and append it to the directory name, and use that new string as part of a command to copy the .exe file over to another directory. The command will eventually read like this (the FILETRANSFERSW.exe is the file transfer software that is also in the directory):
C:\tempfiled\batch> FILETRANSFERSW.exe "%CD%\tmp4.exe" X:\dest
The .exe file name will be changing so i need to dynamically add the new filename into the above command everytime i run the batch file. Any ideas??
If I read your problem correctly, is it sufficient to use the "for" keyword?
for %a in (*.exe) do FILETRANSFERSW.exe %a X:\dest
You can test the output with something innocuous like:
for %a in (*.exe) do echo [[%a]]
%a ends up iterating over *.exe in the current directory, returning the full file name for each one.