I'm trying to write a windows batch script which can select all .bmp
files within directories named beginning with a certain string.
Example of the directory structure:
c:\root
|- images
|- 2020-08-19
|- 001-ABC
| |- firstfile.bmp
| |- secondfile.bmp
|
|- 002-DEF
|- thirdfile.bmp
|- fourthfile.bmp
I would like to be able to execute a command (in this case: convert to JPG) on all .bmp
files inside folders which are named beginning with 001-
So, in the above example, the following files would be selected for execution:
c:\root\images\2020-08-10\001-ABC\firstfile.bmp
c:\root\images\2020-08-10\001-ABC\secondfile.bmp
I was able to find the desired folders but could not figure out how to do the rest:
forfiles /P c:\root\images /S /M 001-*
How can I find all .bmp
files within these folders and then execute the conversion?
(note: I already know the execution command)
For example, use an outer for /d loop to iterate all needed folders, and use an inner for loop to iterate .bmp
files:
@echo off
set "_root_=c:\root\images\2020-08-10"
set "_prefix_=001-"
for /d %%A in ("%_root_%\%_prefix_%*") do (
for %%B in ("%%A\*.bmp") do (
echo processing '%%B'...
)
)
Update.
Or the same as one-liner:
forfiles /p "c:\root\images\2020-08-10" /m "001-*" /s /c "cmd /c @for %A in ("@path\*.bmp") do @echo processing '%A'..."
@ECHO OFF
SETLOCAL
SET "sourcedir=U:\sourcedir"
SET "dirprefix=001-"
SET "datestring=2020-08-19"
FOR /f "tokens=*delims=" %%a IN (
'dir /s /b /a-d "%sourcedir%\*.bmp" ^|find /i "%sourcedir%\%datestring%\%dirprefix%"'
) DO (
ECHO process "%%~a"
)
GOTO :EOF
You would need to change the setting of sourcedir
to suit your circumstances. The listing uses a setting that suits my system.
Processing is: Perform a dir
listing subdirectories of all '.bmp' files in basic form (which lists all of the required files in absolute format), then filter for the required strings using for
and make the filter case-insensitive using /i
.
Then process the resulting list using for/f
with delims=
to assign the entire line to %%a
.
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.