简体   繁体   中英

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM