Windows batch file - how to loop through files in a directory? - for-loop

I have this batch:
for /f %%a IN ('dir /b *.pdf') do call convert.exe %%a
This gets every pdf file thats in the same folder as convert.exe. I want to be able to say where the PDF resides. What do I have to change?
Thanks!

If the directory name can be hardcoded, then it will be
for /f %%a IN ('dir /b /s "Disk:\Your\Directory\Name\*.pdf"') do call convert.exe %%a
Note that this will also return all .pdf files in subdirectories of Disk:\Your\Directory\Name.

Related

Read from a .csv, grab the files from a folder and copy to a new folder

I'm trying to write a batch script to use with Windows command prompt, to read from a .csv filename list, grab and select the files from a folder and copy to a new folder:
for /f "delims=" %%i in (theFile.csv) do (
for /f %%j in ('dir /s /b %theDir%\%%i.*') do (
copy "%%j" "C:\Data"
)
)
This command seems to copy but all the files, what I'm doing wrong?
I suppose you are looking for something this, depending on the structure of your csv file:
#echo off
set "thedir=\\PC\Users\Salvatore\Desktop\BackupUnlock"
for /f "delims=" %%i in ("C:\Users\Salvatore\Desktop\csv1.csv") do for /f %%a in ('dir /b /s /a-d "%%i"') do copy "%%a" "C:\Data" /Y

Get path of found file in for loop

I want to loop through a folder and its subfolders, get all .jpg files and run myapp.exe on the files where I rename the currently found file's extension. myapp.exe takes the full file path as input and a renamed file as output, like so:
myapp.exe C:\images\original\210_551_210-768--RJF3823klzw.jpg -o C:\images\original\210_551_210-768--RJF3823klzw.gif
I now have this to recursively print the full path of *.jpg files.
for /f "delims=" %%i in ('dir /a-d/b/s *.jpg') do echo "%%i" "%%~ni.gif"
However, this prints only the filename for the second parameter, whereas I want to include the path of the found file.
How can I include the path of the output file, the second parameter for myapp.exe?
Pseudocode:
for /f "delims=" %%i in ('dir /a-d/b/s *.jpg') do myapp.exe "%%i" "<FULLPATH><FILNAME>.gif"
If myapp.exe actually converts the file instead of simply changing the extension then you can use metavariable expansion, (see For /? for examples):
For /F "Delims=" %%A In ('Dir /B/S/A-D *.jpg') Do myapp.exe "%%A" "%%~dpnA.gif"
If you're only replacing the extension the you can just use the rename command:
For /F "Delims=" %%A In ('Dir /B/S/A-D *.jpg') Do Ren "%%A" "%%~nA.gif"
In either case you could also change your for loop to:
For /R %%A In (*.jpg)

BATCH - Find most recent file and concatenate it into another file

I have a directory with a list of files, I would like that my batch finds the most recently added file and then copies the content in another file in another directory. So far I did like this:
for /f "delims=" %%x in ('dir /od /a-d /b c:\dir1\*.log') do type %%X >> c\dir2\all.log
The problem is that in this way it outputs for example
ex150422.log 1>>c:\dir2.log
but it doesn't copy the content..
What am I doing wrong?
Thanks!
are you shure it can find the file? add the directory name like you did in dir-command
for /f "delims=" %%x in ('dir /od /a-d /b c:\dir1\*.log') do (
type c:\dir1\%%x>>c:\dir2\all.log
)

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...

copy the recently modified file from source to destination in Batch script

How to copy the recently modified file from source to destination.
Constraint: All the files starting with same name in the source folder.
Example: Source - C:\Source and is having files like sys1239_5241.KNL, sys1234_8741.KNL
So how to copy latest modified files in batch file and modification will happen in minutes not in dates.
for /f "delims=" %%i in ('dir /b /a-d /od "C:\Source\*.KNL"') do set "LatestModifiedFile=%%~i"
echo copy "C:\Source\%LatestModifiedFile%" "X:\destination\path"
..remove echo if it looks good.
The following works well.
for /f "delims=" %%i in ('dir /b /a-d /od "C:\Teste_1\*"') do set "LatestModifiedFile=%%~i"
copy "C:\Teste_1\%LatestModifiedFile%" "C:\Teste_2\"
pause

Resources