Cannot run batch file with xcopy command - batch-file

The code in batch file is :XCOPY "D:\hosts" "C:\Windows\System32\Drivers\etc" /R /Y. But it doesn't work. cmd run like :
How do I fix this?

Based on the output, I'd say you've called your batch file xcopy.cmd or `xcopy.bat.
That means, when it tries to call the actual xcopy executable, it instead calls itself in an infinitely recursive loop.
If that is the case, you have a couple of options:
Rename your batch file to something more sensible, such as copy_hosts.cmd, something that's not likely to clash with a real command; or
Make sure you call xcopy.exe explicitly from your batch file, so it goes and gets the real one.

Related

Why does my IrfanView command not work on batch file but work when typing directly in CMD?

I'm a first-day user of IrfanView and have a question. I have a bunch of multi-page tiff files and I want to split all of them individually. So I write a batch file with the command like this:
C:\Program Files\IrfanView>i_view64.exe D:\originaldirectory\filename1.tif /extract=(D:\newdirectory,tif)
C:\Program Files\IrfanView>i_view64.exe D:\originaldirectory\filename2.tif /extract=(D:\newdirectory,tif)
...and so on...
I put the batch file on D drive, let's say in folder "batchfolder". But it can't do the job, this message shows up for each unsuccessful case (all of them were unsuccessful):
D:\batchfolder>C:\Program Files\IrfanView D:\originaldirectory\filename1.tif /extract=(D:\newdirectory,tif) 1>i_view64.exe
'C:\Program' is not recognized as an internal or external command, operable program or batch file.
I guess that has something to do with the batch file location, so I bring it to C drive. But still it can't run properly, this time a different message shows up:
C:\>C:\Program Files\IrfanView D:\originaldirectory\filename1.tif /extract=(D:\newdirectory,tif) 1>i_view64.exe
Access is denied.
This C:\>C:\ makes me think maybe the C:\ part on the batch file was redundant. So I take it out to make it look like this:
Program Files\IrfanView>i_view64.exe D:\originaldirectory\filename1.tif /extract=(D:\newdirectory,tif)
...
But it doesn't work, either with the batch file on D or C drive.
I then try to type it directly in the CMD window and it works normally, like this:
C:\Program Files\IrfanView>i_view64.exe D:\originaldirectory\filename1.tif /extract=(D:\newdirectory,tif)
Can you tell where my batch file goes wrong?
This is another question. Typing (or copy and paste) the batch file contents into the CMD works OK. But upon successful splitting, the original, multi-image file automatically opens. How can I deactivate this feature?
Note: Cross-post here: https://irfanview-forum.de/showthread.php?t=11150&p=47111#post47111. Hope it doesn't violate policy.
enclose paths/filenames with spaces into quotes to tell the interpreter, it's not two words, but one string (or even better: get used to always enclose path/filenames):
"C:\Program Files\IrfanView\i_view64.exe" "D:\originaldirectory\filename1.tif" /extract=("D:\newdirectory",tif)`
Before you build a batchfile with dozends or hundreds of nearly identical lines, use a for loop to process all .tif files in the folder:
#echo off
for %%a in ("D:\originaldirectory\*.tif") do (
"C:\Program Files\IrfanView\i_view64.exe" "%%~fa" /extract=("D:\newdirectory",tif)
)
see for /? for more information.
You need to call the executable with quotes in the batch. Also, the > in the path will not work either. Also consider using a for loop instead of creating single batch lines.
Please try this:
"C:\Program Files\IrfanView\i_view64.exe" "D:\originaldirectory\filename1.tif" /extract=("D:\newdirectory",tif)

running multiple exe through batch file

Experts, I am trying to run a bat file using the below.
start /wait "D:|Silent_installer.bat"
start /wait 'D:def.bat"
It's happening like both bat files running at the same time.
But I wanted the first bat file to run completely then def.bat should start. The first bat file takes approx 60 min and inbetween second bat file starting.Ideally I wanted the first batch to complete 100% then second bat file should start.
I also used call like below but no luck
call "abc.bat"
call "def.bat"
Any suggestions would be of great help
You have to use start "" /wait command with the program in the abc.bat file itself. One of the programs being used inside abc.bat is multi-threaded and lets the batch file end before it finishes.
why not simply
"abc.bat"
"def.bat"
in your batch file ?
You could use Start command to start application
Ok. Two points here.
The start command is used for asynchronous execution, so if you " wanted the first batch to complete 100% then second bat file should start", just don't use it!
In order to execute two Batch files from inside another one, you must use call command as you show us in your question, that is:
.
call "abc.bat"
call "def.bat"
Perhaps if you explain what "I also used call like below but no luck" means, we may help you in a better way.
PS - Did you realized that your first example
"D:|Silent_installer.bat"
contain an invalid character | in the name of the Batch file?

Why won't a batch file run when being called from within another batch file?

I have a batch file that first creates another batch file containing a ClearCase cleartool command and second, runs it:
ECHO cleartool lsactivity -long "%ACTIVITY%"^>"%OUTPUTFILE%">FILETORUN.bat
CALL FILETORUN.bat
When running the batch, FILETORUN.bat is generated with the correct format, but the CALL to it is completely ignored.
If I ECHO output after the CALL to a log file, I can see that the script just skips over it.
What could it be?
I have tried removing CALL but it makes no difference.
EDIT: SOLUTION
Thank you all for the input. I found the problem. Before the write to batch and batch call in the script there was a command that read information into a variable from a file:
SET /p FILETODELETE=<rmname_%CLEARCASE_USER%.tmp
It reads only the first line. For some reason this created a conflict with temporary batch file, and I have no idea why. I used a different solution for reading the first line from a file and the conflict doesn't happen anymore:
(set FILETODELETE=)
for /f "delims=" %%q in (rmname_%CLEARCASE_USER%.tmp) do if not defined FILETODELETE set FILETODELETE=%%q
If anyone can shed some light it would be great!
SET /P waits for user input, so it actually will finish the command with what you are trying to execute after that and consume the input buffer, which might produce different results on each machine.
See set command reference for more details

Read command-line parameters to .bat from file

I have a build.bat file which uses %1 internally... so you might call:
build 1.23
I wanted it to read the parameter from a separate file, so I tried putting "1.23" in version.txt and doing:
build < version.txt
But it doesn't work. Isn't this how piping works? Is what I want possible and if so how?
The FOR command in DOS has a form which parses files and assigns the tokens it finds to variables, which can then be used as arguments to other batch files. Assuming version.txt contains the single line "1.23", this batch file will open it, assign 1.23 to the variable, then call your original batch file, passing the variable's value as a command-line argument.
#echo off
for /f %%i in (version.txt) do call build.bat %%i
If version.txt contains more than one line, build.bat is called once for each line. Type help for at a DOS prompt to see what other features you might be able to use.
I think it would make more sense if you handle the file processing within the batch file on the off chance that version.txt is not edited correctly.
You should modify your script to parse the file to get the version if the .bat file is executed as:
build FILE version.txt

Windows batch file for iterative invocation of other batch files

Consider a directory structure containing the following files:
\1.3\Baseline\GeneratedScripts\One\FullInstall.cmd
\1.3\Baseline\GeneratedScripts\Two\FullInstall.cmd
\1.3\Baseline\GeneratedScripts\Three\FullInstall.cmd
\1.3\Patches\Patch1\GeneratedScripts\One\FullInstall.cmd
\1.3\Patches\Patch1\GeneratedScripts\Two\FullInstall.cmd
\1.3\Patches\Patch1\GeneratedScripts\Three\FullInstall.cmd
\1.3\Patches\Patch2\GeneratedScripts\One\FullInstall.cmd
\1.3\Patches\Patch2\GeneratedScripts\Two\FullInstall.cmd
\1.3\Patches\Patch2\GeneratedScripts\Three\FullInstall.cmd
\1.3\Patches\Patch3\GeneratedScripts\One\FullInstall.cmd
\1.3\Patches\Patch3\GeneratedScripts\Two\FullInstall.cmd
\1.3\Patches\Patch3\GeneratedScripts\Three\FullInstall.cmd
I would like to construct a Windows batch file InstallEnvironment.cmd which:
Takes an environment name as a parameter; then
Executes the baseline install script, and each of the patch scripts in turn.
The batch file should automatically execute any additional patches that are added later.
Essentially I need to do something along the lines of this:
for %%_ in (1.3\**\GeneratedScripts\%%1\FullInstall.cmd) do cal %%_
However I'm not sure the wildcard system is rich enough to allow this as I don't get any matches for the ** directory wildcard.
For example, calling with the parameter "Two" should execute the following scripts, in order:
\1.3\Baseline\GeneratedScripts\Two\FullInstall.cmd
\1.3\Patches\Patch1\GeneratedScripts\Two\FullInstall.cmd
\1.3\Patches\Patch2\GeneratedScripts\Two\FullInstall.cmd
\1.3\Patches\Patch3\GeneratedScripts\Two\FullInstall.cmd
This will execute all the *.cmd files in the sub folders based on the argument:
for /r 1.3\ %%X in (GeneratedScripts\%1\*.cmd) do call "%%X"
In my experience, the %1 substitution works within directory names.
This should work:
InstallEnvironment.bat:
\1.3\Baseline\GeneratedScripts\%1\FullInstall.cmd
\1.3\Patches\Patch1\GeneratedScripts\%1\FullInstall.cmd
\1.3\Patches\Patch2\GeneratedScripts\%1\FullInstall.cmd
\1.3\Patches\Patch3\GeneratedScripts\%1\FullInstall.cmd
Edit this batch file to add additional patches in order, and it works. If you need to run the same batch file on multiple directories, create another batch file:
call InstallEnvironment.bat %1
call InstallEnvironment.bat %2
If you want to run a batch file in the background, use a vbs file to run that bat file in background instead.
Here is the code:
CreateObject("Wscript.Shell").Run"""" & Wscript.Arguments(0)& """",0,False
Save this exactly as invisible.vbs (or anything) and then make another batch file which will call your batch file to run it in background.
The code for the second batch file is:
wscript.exe "invisible.vbs" "Your_Batch_File.bat"
Then run the second batch file.
Note: WSH should be enabled on your computer, and the invisible.vbs file and the second batch file should be in the same folder. If not then you can give the full path to the invisible.vbs file in the 2nd batch file's script.

Resources