I'm trying to pass files one by one(I have to dot that since executable only accepts one file at a time). So, in my batch I have follwoing:
FOR /F %file IN ('dir /b /s *.css') DO CALL myExecutable.exe %file
I should see out files in same directory but nothing happens, no errors are displayed either.
Am I missing something here?
You have several mistakes in your example:
FOR parameter name is a single letter only
CALL is used to call another batch file or a subroutine in the existing batch file, not executables
the FOR parameter should be referenced with two %, when in batch file
you need to use a non-space delimiter, if the directory you run this command in or any subdirectory, or if any of the files has a space in the name
With these in mind, here's the right command you should be using:
for /f "usebackq delims=|" %%f in (`dir /b /s *.css`) do myexecutable.exe "%%f"
Here's my answer to a similar SO question, where I give more details on using FOR to process all files in a directory.
Related
I'm currently trying to convert a lot of files into another format using an executable. When running the command on cmd, it goes as follows :
executable_name.exe -e "directory\filename.old_format" "directory\filename.new_format"
How can i write a bat so that it can reuse the directory, filename, and format name so that it can be re-executed like the example above, only with a changed file format? Also, how can i do this with multiple files?
for /f "delims=" %%b in ('dir /b /s /a-d "directory\*.old_format"') do executablename.exe -e "%%b" "%%~dpnb.new_format"
as a batch line should accomplish this task, in general.
See for /? from the prompt for documentation about the hieroglyphs or hundreds of examples on SO.
Note that I've used /s on the dir command. This scans subdirectories, too.
If you want to scan just one directory,
Omit the /s from the dir command.
Use "dirname\%%b" in place of "%%b" to specify the source file
Use `"dirname2%%~nb.new_format" to specify the destination
so i'm trying to take one file at a time out of a directory that contains 2000 files and going up and move it to a different directory to be worked on by the rest of my script. the script is below. Right now i know the gswin64 line works when it is alone and i specify it a filename but not i'm sure with the variable yet. But when i run this it will copy all the files in directory input to directory working before it runs the rest of the script. How do i make it do one at a time then process the script before copying the rest of the files?
for /F "delims=" %%I in ('dir "H:\documents\gs\input\*.*" /A-d /B /O:-D') do (move "H:\documents\gs\input\%%I" "H:\documents\gs\working"&goto filemoved)
:filemoved
Your original code would attempt to execute move "H:\documents\gs\input\*.*" "H:\documents\gs\working" which is why all of th files were moved. What you need to do is execute a directory list command with the switch parameters and supply that to the for /f. This form of dir will show names only. so you need to include the source directory name in the move command. You would also need to have the "delims=" option to ensure that filenames containing separators are processed correctly, and the /a-d directory switch to ensure that directory names are not included. Since you are sorting in reverse-date order, the newest file will be selected first. After the first file is moved, you need to abort the for loop otherwise it will continue processing the entire list, transferring every file. The easy way here is to simply goto a label on the next line.
You've also used a *.* filemask, which will process all files, regardless of extension. Since you appear to want to process only .pdf files, you should probably change that filemask to suit.
for /F "delims=" %%x in ('dir H:\documents\gs\working\*.pdf /b') do set "FileName=%%x"
Again, you need to execute a dir command if you are using a for /f. There's no apparent reason why you wouldn't use the far simpler
for %%x in (H:\documents\gs\working\*.pdf) do set "FileName=%%x"
In either case, you probably want only the name part of the file, so FileName should be set to %%~nx, not %%x.
"C:\program files\gs\gs9.20\bin\gswin64" -o H:\documents\gs\output\"%FileName%" -sDevice=pdfwrite -dFitPage -dFIXEDMEDIA H:\documents\gs\working\"%FileName%"
Unbalanced quotes - the full pathname to the executable needs to be quoted since it contains a separator. Stray spaces after \ will probably need to be removed.
It would be better imho to quote the entire filename, including drive and path rather than filename only.
del /q H:\documents\gs\working\*.*
rm is not a batch command. Note that this command will delete ALL files in H:\documents\gs\working - not just the .pdf files that you appear to be processing.
goto start
The space is required. gotostart is not an inbuilt command.
I need the syntax to call a batch file from the first batch file. The second batch files name changes with the revision. so i have only half of my second batch file name.
How do i search in a particular folder and call the second batch file..`?
Don't know the revision prefix or suffix, but you could try something like this:
for /f "tokens=1" %%n in ('dir /on /l /b /a-d "bat_file_*.bat"') do set latest_bat_file=%%n
It's relying on dir /on to sort by name, so it puts the last entry alphabetically in the variable %latest_bat_file%. You can then call it with:
call "%latest_bat_file%"
This assumes there are no spaces in your bat file names and that the revision is a numeric or alphabetical suffix. If you're using numbers, to avoid sorting problems, prefix your revision names with zero's (e.g. bat_file_001, bat_file_002).
FOR /R <path> will walk the directory tree for you.
FOR /R "%DIR_TO_SEARCH%" %%b IN (matching_*.bat) DO cmd /c "%%~b"
cmd /c will create a new shell instance, which means that if the invoked .BAT file sets environment variables, they won't be changed in calling script. This is usually what people want. If you actually wanted those side effects preserved, you could use call "%%~b" instead.
I want to create a .bat file that will help me to find/search a particular .exe file in folders in a particular location and then run that .exe file.
e.g
search file in particular location e.g."\G:\data" and in this prime location there is many folders and in that folders many sub folders are there. and the .exe file is available in one of these sub-folder and the location I do not know. only I know the name of the .exe file.
bat file need to search that file and then run that one.
below two are not able to find and run the file.
1st program
pushd G:\data-a\test
FOR /F "delims=" %F IN ('dir /S /b autorun.exe') DO SET ExePath="%F"
%ExePath%e
2nd program
for /r G:\data-a\test %a in (autorun.exe) do "%a"
from the above two different program I put it in to notepad for creating two different .bat file and executed one after anther file. but that was unable to locate and run autorun.exe file.
just for example my autorun.exe file exact locations are
G:\data-a\test\test1\t1 and G:\data-a\test\test1\t1
but in G:\data-a\test location there may be several subfolders.
so to run autorun.exe one by one that to find the file in subfolder is difficult. I want to give the path G:\data-a\test in program and all the files will be found in subfolder and then run automatically.
Three liner (use %%F if wrapping inside a batch file).
pushd G:\data
FOR /F "delims=" %F IN ('dir /S /b something.exe') DO SET ExePath="%F"
%ExePath%
or the one liner from the command prompt:
for /r g:\data %a in (filename.exe) do "%a"
used the quotes for %a in case the calling directory path has spaces in it
I was looking for a batch script to identify the newest file in a directory. The examples I found all use FOR /F. When I read the help documentation on FOR, it states that /F opens, reads and processes each file.
Why is /F used in this case? I've used it with large binary files and the script does not seem to slow down so I do not think each file is actually being opened, etc.
I tried using FOR without /F to do the same job and didn't have any luck. Is there a reason for that?
For instance:
FOR %%I IN ('dir "*.AVI" /B /O:D') DO set newestAvi=%%I
does not seem to work. For some reason, newestAvi is equal to "/O:D'" at the end.
If I use
FOR /F "delims=|" %%I IN ('dir "*.AVI" /B /O:D') DO set newestAvi=%%I
then things work.
Thanks,
Dave
I think the relevant bit of the help file is
Finally, you can use the FOR /F command to parse the output of a
command. You do this by making the filenameset between the
parenthesis a back quoted string. It will be treated as a command
line, which is passed to a child CMD.EXE and the output is captured
into memory and parsed as if it was a file. So the following
example:
So, with the /F your command takes the output from
dir "*.AVI" /B /O:D
and parses each line into the command
newestAvi=%%I
which becomes
newestAvi=FileName.AVI
for each file in the current directory. The last value assigned is the one that is left at the end of the for commands execution.