![](/img/trans.png)
[英]Windows 7 Batch - Create subfolder, then find files with certain text in file name and move those files in the newly created subfolder
[英]Windows Batch - Find image files inside folders that have a certain name
我正在嘗試編寫一個 windows 批處理腳本,它可以 select 以某個字符串開頭的目錄中的所有.bmp
文件。
目錄結構示例:
c:\root
|- images
|- 2020-08-19
|- 001-ABC
| |- firstfile.bmp
| |- secondfile.bmp
|
|- 002-DEF
|- thirdfile.bmp
|- fourthfile.bmp
我希望能夠對以001-
開頭的文件夾中的所有.bmp
文件執行命令(在這種情況下:轉換為 JPG)
因此,在上面的示例中,將選擇執行以下文件:
c:\root\images\2020-08-10\001-ABC\firstfile.bmp
c:\root\images\2020-08-10\001-ABC\secondfile.bmp
我能夠找到所需的文件夾,但無法弄清楚如何執行 rest:
forfiles /P c:\root\images /S /M 001-*
如何在這些文件夾中找到所有.bmp
文件,然后執行轉換?
(注:我已經知道執行命令)
例如,使用外部for /d循環來迭代所有需要的文件夾,並使用內部for循環來迭代.bmp
文件:
@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'...
)
)
更新。
或與單線相同:
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
您需要更改sourcedir
的設置以適應您的情況。 該清單使用適合我的系統的設置。
處理是:執行一個dir
以基本形式列出所有“.bmp”文件的子目錄(它以絕對格式列出所有必需的文件),然后使用for
過濾所需的字符串,並使用/i
使過濾器不區分大小寫。
然后使用for/f
和delims=
處理結果列表,將整行分配給%%a
。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.