我有一个Windows目录,其中包含名称类似的文件夹:
Lot 1 Lot 2 Lot 5 Lot A Block A Block C
每个文件夹都包含PDF测量图。文件名无关紧要。我的目标是在唯一的文件夹中为每个PDF加上父文件夹名称的编辑版本。与其将前缀Lot 1_
添加到文件夹Lot 1
中文件的文件名中,我不希望将L1_
添加到该文件名中。
当前,我有一个批处理文件,它将在提示符下输入的所有前缀添加到当前文件夹中的所有PDF。这意味着我必须进入每个文件夹并运行批处理脚本。这很乏味,因为我有数千个要处理。
这是代码的样子:
Set /p prefix="Enter Prefix to add to file names: "
FOR /f "delims=" %%F IN ('DIR /a-d /b *.PDF') DO (RENAME "%%F" %prefix%_"%%F")
我一直在试图创建一个批处理文件来遍历所有带有Lot和名称的空格的文件夹,现在为止,我所能做的就是使用一个for循环来获取所有Lot
无论编号的名称并输出它们到文本文件。
这是该代码的样子:
for /f "delims=" %%F in ('dir /A:D /b Lot*') do echo %%F >> folders.txt
然后我得到带有以下内容的文本文件:
Lot 1 Lot 2 Lot 5 Lot A
我也只能输出文件夹名称的数字部分。该代码:
for /f "tokens=2" %%F in ('dir /A:D /b Lot*') do echo %%F >> folders2.txt
然后我得到带有以下内容的文本文件:
1 2 5 A
我感觉我与最后一个非常接近,但是我想做的是代替回显folders2.txt
,我想输入另一个for循环,在该循环中我将Lot %%F
中的每个文件重命名并添加前缀L%%F
和文件名的其余部分。
有人有什么想法吗?
这怎么办:
@echo off
rem // Switch to the target directory:
pushd "D:\Target" && (
rem /* Loop through all directories whose names begin with `Lot` + SPACE;
rem the `findstr` portion further filters the directory names so that
rem they must begin with `Lot` + SPACE + a character other than SPACE: */
for /F "tokens=1*" %%C in ('
dir /B /A:D "Lot *" ^| findstr /I "^Lot [^ ]"
') do (
rem /* Loop through all `*.pdf` files in the currently iterated directory;
rem the `findstr` portion further filters the file names so that they must
rem not equal `L` + a character other than `_` + something containing a `_`
rem + `.pdf`, because such files are considered as already renamed: */
for /F "delims=" %%F in ('
dir /B /A:-D "%%~D\*.pdf" ^| findstr /I /V "^L[^_].*_.*\.pdf$"
') do (
rem // Actually rename the currently iterated file:
ECHO ren "%%~C %%~D\%%F" "L%%~D_%%F"
)
)
rem // Return to original working directory:
popd
)
测试正确的输出后,从ECHO
命令中删除大写的ren
!