I'm writing a simple batch file that will execute a block of commands for each folder in a directory. Here is my code:
for /D %%x in ("C:"*) do echo "Folder found!">>test.txt
That works. The trouble is, when I add parentheses to execute several commands in each folder, the operation crashes. It doesn't run the loop:
for /D %%x in ("C:"*) do(
echo "Folder found!">>test.txt
)
Causes a crash.
I can't find anything on google. Does anyone have any ideas?
Its simply the missing space after do change to do (
Also the pattern "C:"* is the current directory prepended with "C:", for C:\ itself;
for /D %%x in ("C:\*") do (
echo "Folder found!"
)
Related
I am trying to run a batch file against a list of remote computers.
The batch file will check for a specific file, if the file is not found in the directory it should be copied there.
I am using the %%a variable for the PC names.
The xcopy command fails in the batch file with invalid drive specification.
If I take the xcopy command and replace the variable with the computer name and run from a command prompt it will copy the file over.
I am not sure why the xcopy command fails in the batch file when using %%a
PC.txt is my text file with the list of computers.
I am using the hostname and not the FQDN in the text file
for /f %%a in (PCList.txt) do (
If Exist "\\%%a\c$\Program Files\Folder\App.exe" (
ECHO %%a App.exe exists
) ELSE (
ECHO %%a App.exe Does Not Exist
xcopy "C:\Folder\App.exe" "\\%%a\c$\Program Files\SubFolder\" /Q /R /Y
First of all, it looks like you have one or two syntax errors. You opened a parenthesis in ... Folder\App.exe" (and you haven't closed it since, the same thing with the ) ELSE (. Also, sometimes the parenthesis inside a FOR can cause interference with the rest of the code. If you add the missing parenthesis does not resolve, try to fix the code to: Folder\App.exe" ^( which can help.
Try this:
for /f %%a in (PCList.txt) do (
If Exist "\\%%a\c$\Program Files\Folder\App.exe" ECHO %%a App.exe exists
) ELSE (
ECHO %%a App.exe Does Not Exist
xcopy "C:\Folder\App.exe" "\\%%a\c$\Program Files\SubFolder\" /Q /R /Y )
Also, you can use the COPY only, maybe the problem is with the COPY. And using the parameter if exist to a entire .exe isn't the best idea, the user can just create a txt file and rename it as App.exe.
Hope this helps,
K.
I am trying to create a batch file that will check a list of computers for a specific directory and if the directory exist pop-up a message on the screen.
If the directory does not exist then copy it to the remote system.
I have not been able to find any good examples.
The batch file I am working on does not function well at all. It does no tell you when the directory exist, it does not write to the text file and when it goes to copy I am getting invalid path.
This is where I am at now. Any help would be appreciated as this is my 1st attempt at writing a batch file
#echo off
for /F %%A in (machines.txt) do (
if exist "\\%%A\C$\temp\test\" (
echo %%A Directory Exits >>exists.txt
) else (
XCOPY "C:\temp\test\" "\\%%a\c$\Temp\test\" /D /E /C /R /I /K /Y /Q
)
Since the code above was not working I decided to start over and make sure the batch file could find the directory called Test.
When I run the batch file it creates the missing.txt file instead of the exists.txt file which would indicate that it found the directory. I am not sure why it is not finding the directory or maybe it is but my code is incorrect?
This is what I ran
#echo off
for /F %%A in (machines.txt) do (
if exist "\\%%A\C:\Windows\Test" (
echo %%A : FOUND >>exists.txt
) else (
echo %%A : MISSING >>missing.txt
)
)
Pause
I have a directory with a number of folders that each include sub-folders. I want to run a batch that searches those folders for the existence of a specific sub-folder called "failed". If there is a file present in this sub-folder, I want it to then move that file to another defined folder.
I've tried to look up usage of "if exist" commands but can't seem to find anything that directly suits my needs.
The names and types of the files to be moved are random, which is why I believed the "if exist" to be the most utile.
Any suggestion?
Read help for paying attention to the /r and /d options and then try this one-liner in the command line
for /d /r %a in (failed) do #echo %a
you will see that this loop iterates over all the directory structure and appends failed to the directory name
then add a check for the actual existence of the directory
if exist %a\nul #echo %a
then add a second loop to iterate over all of the contents of the found failed directory
for %b in (%a\*) do #echo %b
then replace the echo with the required move command
move %b %destination%\%~nxb
and there you go, with a simple single line
for /d /r %a in (failed) do #if exist %a\nul #for %b in (%a\*) do #move %b %destination%\%~nxb
or, translated into a bat file and spiced it up a bit
pushd %sourceroot%
for /d /r %%a in (failed) do (
if exist %%a\nul (
for %%b in (%%a\*) do (
move %%b %destination%\%%~nxb
)
)
)
popd
you'll have to figure out a way to rename the destination file to avoid name clashes.
I have written the following batch script, which runs another batch script on a directory, or, with addition of a flag, on a directory tree and then on an equivalent directory or directory tree on a different drive (Z:). No matter which option I choose, it outputs the error "The system cannot find the path specified." It does do what it's supposed to if I do it on just one directory, even though it gives the error. It doesn't work successfully on a directory tree. I've run it without #echo off to try understand where its failing, without success. The directory which it's trying to change into does exist.
#echo off
set origdir=%CD%
if X%~f1==X (
echo Please input a directory.
goto done
)
chdir /d %~f1
for %%X in (myotherscript.bat) do (set FOUND=%%~$PATH:X)
if not defined FOUND (
echo myotherscript is not in your PATH
)
if X%2==X/R (
goto recursive
) else ( goto single )
:recursive
for /d /r %%G in (.) do call myotherscript
echo Z:%~p1
chdir /d "Z:%~p1"
for /d /r %%G in (.) do call myotherscript
goto ended
:single
call myotherscript
echo Z:%~p1
chdir /d "Z:%~p1"
call myotherscript
goto ended
:ended
chdir /d origdir
goto done
:done
pause
Here is "myotherscript" Yes, purge does exist.
#echo off
if exist "D:\path\to\purge.bat" (
call purge
for %%f in (*.log.*) do call :renameit "%%f"
for %%f in (*.drw.*) do call :renameit "%%f"
for %%f in (*.asm.*) do call :renameit "%%f"
for %%f in (*.prt.*) do call :renameit "%%f"
goto done ) else (
echo Purge does not exist.
goto done )
:renameit
ren %1 *.1
:done
Any help would be appreciated.
Thanks
I'm not sure why this (very old) question got reactivated. But since it has, let's see if we can close this out.
There seem to be two problems here. First:
it outputs the error "The system cannot find the path specified."
This looks like a simple typo on this line:
chdir /d origdir
Without the '%' marks, this is trying to change to a directory literally named origdir, rather than the original directory the script was run from, which would be:
chdir /d %origdir%
Second problem is:
It does do what it's supposed to if I do it on just one directory, even though it gives the error. It doesn't work successfully on a directory tree.
At a guess, this is due to this line:
if X%2==X/R
"IF" is case sensitive. If you tried to run this using /r, it wouldn't see the request for recursion and would always execute single.
For me I got the "The system cannot find the path specified" due to a missing exe that seemed way later in the script. It seems that the pipes in DOS don't always output data in the order of execution. I was used to UNIX where the output from each "echo" command in a script goes in order, so I had added debug output in the .bat file to try to tell me what lines had executed.
The problem is, the error about the file not found was happening in the output log (and screen) way earlier than the echo commands would indicate. So I don't know if the WinXP cmd shell was going a few steps ahead, or it was parsing for the exe to call during startup of the called bat file or what.
It turned out it was in fact a bad path to the .exe I was running from a call'd bat script, but the echo debug statements made me think I was in a way earlier part of the script. Once I added the right path before the exe it all worked
I can't find any references explaining how to loop through a folder passed as an argument and rename each file. All examples I've seen assume the script is running in the folder where files will be renamed or hardcode the folder path in the loop. How can I do this? Here is an example of what I'm trying to do:
for /f %%a in (%1) do call :RenameFiles
:RenameFiles
Rename %%a "new filename"
Goto :EOF
The following adds prefix "renamed-" to every file in the directory specified as a command line argument:
for %%F in (%~1\*) do ren "%%~F" "renamed-%%~nxF"
EDIT The simplest solution to the problem of the infinite loop is to work in two stages:
prepare the list of operations
execute the plan.
-
set OpList=%TEMP%\%~n0%RANDOM%.bat
copy nul "%OpList%"
for %%F in (%~1\*) do echo ren "%%~F" "renamed-%%~nxF" >> "%OpList%"
call "%OpList%"
del "%OpList%"