Command only works in cmd line but not in batch file - batch-file

I want to copy some file. It can already be copied by using the command line, but not in a batch file.
#echo off
E:
cd Testing\ihelpdesk_extraction
XCOPY Testing.csv "\\10.44.103.111\Shared_Folder\Shared_Folder\"
exit
My current directory is C:\Users\Administrator.

Use full paths:
i.e.:
XCOPY "E:\Testing\ihelpdesk_extraction\Testing.csv" "\\10.44.103.111\Shared_Folder\Shared_Folder\Testing.csv"

Related

Batch file to run cmd command

I have this cmd command (that I found here) that makes me list all image files in the current folder:
for %i in (*) do echo ^<img src="%i" /^> >> all.html
So if I want to run this I need to go to cmd and manually input the folder path every time I run said code.
Can I put this in a batch file/cmd file so I can just put the batch file/cmd file in any folder I want and it will run the code?
I believe you are looking for %~DP0 which is only available within a batch file and displays the current drive and directory in which that batch file is located note, this cannot change. It is obtained from %0 which is the batch file's name.
%CD% on the other hand is available either to a batch file or at the command prompt and expands to the drive letter and path of the current directory. Which could change by using CD for instance.
More info
Full answer is given by Compo:
#(for %%i in ("%~dp0*") do #echo ^<img src="%%i" /^>)>"all.html"

all the commands are not getting executed in bat file?

after the startup.bat command I wanted to start chrome with url: http://localhost:8080/Nexus but bat file getting stopped after tomcat is being started up.
Code in the batch file is as follows:
#echo off
start cmd
cd I:\Users\5251966\Nexus
echo yes|copy catalina.bat I:\apache-tomcat-7.0.55-windows-x64\apache-tomcat-7.0.55\bin
echo yes|copy tomcat-users.xml I:\apache-tomcat-7.0.55-windows-x64\apache-tomcat-7.0.55\conf
cd I:\apache-tomcat-7.0.55-windows-x64\apache-tomcat-7.0.55\bin
startup.bat
start chrome http://localhost:8080/Nexus
Here's some example code for you:
#Echo Off
Set "srcDir=I:\Users\5251966\Nexus"
Set "dstDir=I:\apache-tomcat-7.0.55-windows-x64\apache-tomcat-7.0.55"
Copy /Y "%srcDir%\catalina.bat" "%dstDir%\bin"
Copy /Y "%srcDir%\tomcat-users.xml" "%dstDir%\conf"
CD /D "%dstDir%\bin"
Call "startup.bat"
Start chrome http://localhost:8080/Nexus
Use call to call other batch files from a batch file, otherwise your parent batch file terminates and won't run any more commands after invoking the child:
call startup.bat

Why is xcopy command not working for copying a bitmap file into a subdirectory of Windows system32 directory?

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 to change to a subdirectory and run an exe using a bat file in a Windows 7 system?

Using a bat file, I want to change to a sub directory of folder which bat file is in, and run my_application.exe in that directory,
I try:
cd /d %cd%\my subdirectory
START %~dp0my_application.exe
But it doesn't work, it says it can't find my_application.exe
Just indicate to start command what program to start and what should be the starting folder for it.
Without the cd command, it can be written as
start "" /d "%~dp0my_subdirectory" "my_application.exe"
if the my_application.exe is located in the subdirectory, or
start "" /d "%~dp0my_subdirectory" "%~dp0my_application.exe"
if the application is located in the same folder as the batch file.
start command take the first quoted parameter as the title for the new process. To avoid problems, a empty string ("") is included in command as the title.
Try:
cd /d "%~dp0my_subdirectory"
start "" my_application.exe
or just:
start "" "%~dp0my_subdirectory\my_application.exe"

how to create a batch file that executes a single command on all the files in a directory

how to create a batch file that executes a single command on all the files in a directory
For Example, directory C;\Test contains 30 files with an .ini extension. I could run each file individually by using the command Trade abc.ini, bcd.ini, cde.ini ..... and so forth.
I would like to have a batch file to run the trade command on all the files in the directory without having to name each file individually. Thanks for your help!
how to create a batch file that executes a single command on all the files in a directory
for %%f in (*.ini) do call echo %%f
And replace "echo" with your own command.
you can use forfiles to do that
FORFILES /M *.ini /C "cmd /c Trade #file"

Resources