How to use parameters passed to a batch file in a for loop? - for-loop

Please consider the following...
This works:
for /f %%A in ('Dir /b *ipc10*.JPG') do copy %%A "%cd%\!sd!"
The goal from the cli> file.bat *ipc9* or *ipc10* or *ipc6* or etc.
... to get passed to the for command, instead of hard-coding each argument\parameter within the batch file.
I'm able to use echo %1, echo %2, etc. However %1 nor %%1, will work as expected within that parenthesized command.
i.e.
for /f %%A in ('Dir /b %1 OR %2 .JPG') do copy %%A "%cd%\!sd!"
for /f %%A in ('Dir /b %%1 OR %%2 .JPG') do copy %%A "%cd%\!sd!"
... will not expand to the intended contents
Please advise.

Related

Getting Parent Directory for each file from Dir Output

I have a directory with multipe levels of folders.
I am completely new to writing batch files and I am writing my first one.
Stuck for ages on trying to
find all files in the directory including sub-folder
get parent directory for each file
save as variable like %parent.filename%
I have been searching here:
https://learn.microsoft.com/en-us/previous-versions/windows/it-pro/windows-xp/bb490909(v=technet.10)
And on Google but unfortunately I am stuck.
So now I managed to save the full path of each file as variable, but I want %Folder.FileName% to return the parent directory only, not the full path.
This is the code I have been testing in the command prompt.
For /F %A in ('Dir Linkedin /A-D /s /b /o') do SET Folder.%~nxA=%~pA
EDIT
I also saw this thread
And tried this code:
FOR /F %A in ('Dir Linkedin /A-D /s /b /o') do ECHO %~nxA %~pA >>Paths.txt
FOR /F "tokens=1,2" %A in (Paths.txt) do SET Parent.%A=%~nB
But %~nxB doesn't return any value... I expected it to get the last string of the path.
FOR /F %A in ('Dir Linkedin /A-D /s /b /o') do ECHO %~nxA %~pA. >>Paths.txt
FOR /F "tokens=1,2" %A in (Paths.txt) do SET Parent.%A=%~nB
Note the extra .
The path provided by the ~p modifier terminates in \ so adding . to this means "the directory name itself as though it was a filename"
As a one-line command (within a batch, decorated by standard palaver)
#ECHO OFF
SETLOCAL
FOR /F %%A in ('Dir test* /A-D /s /b /o') do FOR /F %%S in ("%%~pA.") do SET Parent.%%~nxA=%%~nS
set parent.
GOTO :EOF
I used the filemask test* to better suit my system.
I can't imagine you'd voluntarily perpetually re-type the command, so the format for use within a batch file is shown.
I would suggest you do this as a single nested For loop from the Command Prompt and with no output file:
For /F "Delims=" %A In ('Dir /B/S/A-D "Linkedin" 2^>NUL')Do #For %B In ("%~pA.")Do #Set "Folder.%~nxA=%~nxB"
From a batch-file, perhaps this would help you out:
#Echo Off
Rem Remove any existing Folder. variables
For /F "Tokens=1*Delims==" %%A In ('Set Folder. 2^>NUL')Do Set "%%A="
Rem Set the new variables
For /F "Delims=" %%A In ('Dir /B/S/A-D "Linkedin" 2^>NUL')Do For %%B In ("%%~pA.")Do Set "Folder.%%~nxA=%%~nxB"
Rem View any returned variables
Set Folder. 2>NUL&&Pause

Batch rename with outcome of other batch

I'm trying to create a batch file, which scans documents for a barcode, and rename the file to the barcode.
I use two batchfiles for this. One of the batchfiles (test.bat) executes the exe that scans for the barcode:
#echo off
zbarimg.exe --raw -D -q %1
In the second batchfile, I want it to scan every file in the directory with a *.tif extension, scan for the barcode, and rename it to the barcodenumber. The second batch (rename.bat) looks like this:
for /f "tokens=*" %%a in ('dir /b *.tif') do (test.bat %%a)
My main question is, how can I incorporate a rename command, that renames the files to the outcome of test.bat.
In short, this is the proces:
1. there is a file called test.tif
2. the file is being scanned for a barcode (i.e. 123456789)
3. test.tif is renamed to 123456789.tif
I am wondering how I can capture the outcome of test.bat in a variable, so I can call that when I use the rename command?
Thanks!
The way to capture the output from test.bat is the same you used to capture the output from dir command: via a for /F command.
The simplest solution is eliminate the test.bat file and insert the zbarimg.exe program in the same Batch file:
for /f "tokens=*" %%a in ('dir /b *.tif') do (
for /F %%b in ('zbarimg.exe --raw -D -q %%a') do (
ren "%%a" "%%b.tif"
)
)
However, if you want to keep test.bat file, just do this:
for /f "tokens=*" %%a in ('dir /b *.tif') do (
for /F %%b in ('test.bat %%a') do (
ren "%%a" "%%b.tif"
)
)
The awkward way cmd has to get the result into a variable is to use a FOR loop.
SETLOCAL ENABLEDELAYEDEXPANSION
SET "BCNAME="
for /f "tokens=*" %%a in ('dir /b *.bat') do (
FOR /F "usebackq tokens=*" %%b IN (`CALL wc "bc.bat"`) DO (SET "BCNAME=%%~b")
echo REN "%%~a" "!BCNAME!.tif"
)
When it produces the correct REN command, remove echo.
Also, this makes no plan for multiple .tiff files to have the same barcode result.

Batch script for loop

I have a problem with for loop in batch script.
When I try:
for /f "delims=;" %g in ('dir') do echo %g%
i see this
'dir' is not recognized as an internal or external command,
operable program or batch file.
Did I miss somethig? Why windows command doesn't work?
Don't do that. You get all kinds of problems iterating over dir output with for /f. Instead just use
for %g in (*) do #echo %g
In your question you stated echo %g% which is wrong (the trailing % will be returned literally).
When the command is directly typed into command prompt, use:
for /f "delims=" %g in ('dir /B') do echo %g
When you are using for within a batch file you need double-% for its variable:
for /f "delims=" %%g in ('dir /B') do echo %%g
The option delims=; makes no sense as dir does not give a semicolon-separated list, so I deactivated delims.
The /B switch changes the dir output to bare format (no headers ad footers, only files and dir.s).
It looks like the %comspec% variable has been nulled or changed and it can't find cmd.exe
Test this to see the error, and remove line 2 for it to work fine, on a working machine.
#echo off
set comspec=c:\aaa
for /f "delims=;" %%g in ('dir') do echo %%g
pause
Something that is very interesting is that the path to cmd.exe is cached.
The two scripts below are the same with only the order of the commands changed
This batch file will fail with both for commands
#echo off
set comspec=c:\aaa
for /f "delims=;" %%g in ('dir') do echo %%g
pause
set "comspec=%windir%\System32\cmd.exe"
for /f "delims=;" %%g in ('dir') do echo %%g
pause
and this batch file will work with both for commands.
#echo off
set "comspec=%windir%\System32\cmd.exe"
for /f "delims=;" %%g in ('dir') do echo %%g
pause
set comspec=c:\aaa
for /f "delims=;" %%g in ('dir') do echo %%g
pause

Batch file / add file names to command

Looking for a way to run a command, but insert the path/filenames of all mp4 files from the working directory (where the script is run) to the command.
vlc.exe c:\path\filename1.mp4 c:\path\filename2.mp4
I have the following code, but the "%%~A" is only inserting one path/filename at a time instead of adding every path filename from the folder.
set dir=C:\Users\Administrator\Desktop\1
for /f "delims=" %%A in ('dir /b "%dir%\*.*"') do ("C:\Program Files (x86)\VideoLAN\VLC\vlc.exe" "%%~A" --sout=#transcode{vcodec=mp2v,vb=800,acodec=mpga,ab=128,channels=2,samplerate=44100}:http{mux=ts,dst=:8080/abc} --sout-keep --loop)
An elementary solution: first try
set "dir=C:\Users\Administrator\Desktop\1"
dir /b /a:-d "%dir%\*.*"
The same result (filenames only) as in:
set "dir=C:\Users\Administrator\Desktop\1"
for /f "delims=" %%A in ('dir /b /a:-d "%dir%\*.*"') do #echo "%%~A"
So add full path as follows:
set "dir=C:\Users\Administrator\Desktop\1"
for /f "delims=" %%A in ('dir /b /a:-d "%dir%\*.*"') do #echo "%dir%\%%~A"
Eventually, replace #echo "%dir%\%%~A" with your command...

How to execute any .exe by using a batch file?

I know that I can start an exe by doing:
start "" /b filename.exe
But that requires me to know the name of filename.exe, how could I do that for any general file ending with .exe? I tried the obvious wildcard implementation:
start "" /b *.exe
Windows, however, gives me an error saying it cannot find "*.exe" file.
if you plan to run inside a batch file you can do in this way:
for %%i in (*.exe) do start "" /b "%%i"
if you want to skip a particular file to be executed:
for %%i in (*.exe) do if not "%%~nxi" == "blabla.exe" start "" /b "%%i"
if is necessary to check also the subfolders add the /r parameter:
for /r %%i in (*.exe) do start "" /b "%%i"
From cmd run this to the folder that has all the exe you wish to run:
for %x in (*.exe) do ( start "" /b "%x" )
Hoep it helps
for /f "delims=" %%a in ('dir /b /s "*.exe"') do (
start %%a
)
You should first use dir command to find all exe files, and then execute it.
In a bat file add this line
FOR /F "tokens=4" %%G IN ('dir /A-D /-C ^| find ".exe"') DO start "" /b %%G
This execute every .exe file in your current directory. same as
*.exe
would have done if * were supported on batch.
If you want to execute it directly from a command line window, just do
FOR /F "tokens=4" %G IN ('dir /A-D /-C ^| find ".exe"') DO start "" /b %G
Don't blame their codes for space issue. You should know how to use double quotation marks.
for /f "delims=" %%a in ('dir /b /s *.exe') do (
start "" "%%a"
)

Resources