英文:
Renaming multiple files using Batch script
问题
我理解你的要求,以下是翻译的部分:
我有许多以这种格式命名的文件:
```none
D:\images\AAA_BBB\image\whatever1.jpg
D:\images\AAA_BBB\image\whatever2.jpg
D:\images\FFF_EEE_CCC\image\asdf1.jpg
D:\images\FFF_EEE_CCC\image\asdf2.jpg
D:\images\WWW_XXX_YYY_ZZZ\image\someimage1.jpg
D:\images\WWW_XXX_YYY_ZZZ\image\someimage2.jpg
我想移动和重命名这些文件,使用绝对文件路径的子串,并删除原始文件,使结果看起来像下面的内容,以“0”开头:
D:\images\AAA\AAA_0.jpg
D:\images\AAA\AAA_1.jpg
D:\images\EEE\EEE_0.jpg
D:\images\EEE\EEE_1.jpg
D:\images\YYY\YYY_0.jpg
D:\images\YYY\YYY_1.jpg
请注意,带下划线的文件夹中的标记数量可以是任意长度。
以下尝试只会给我输出从前面开始的第二个标记。使用“Tokens=-2”来获取倒数第二个标记不会输出任何内容。
@echo off & setlocal EnableDelayedExpansion
PushD "D:\images" || Exit /B
For /F "EOL=? Delims=" %%G In ('Dir /A:D /B 2^>NUL
^| %SystemRoot%\System32\findstr.exe /I /R "^[^_][^_]*_[^_][^_]*_[^_][^_]*_"'
) Do For /F "Tokens=2 EOL=? Delims=_" %%H In ("%%G") Do (
Set "i=-1"
For /F "EOL=? Delims=" %%I In ('Dir "%%G\image" /A:-D /B') Do (
Set /A i += 1
SetLocal EnableDelayedExpansion
Ren "%%G\image\%%I" "%%H_!i!%%~xI" && (
If Not Exist "%%H\." MD "%%H"
Move /Y "%%G\image\%%H_!i!%%~xI" "%%H"
)
EndLocal
)
)
PopD
然后我使用了不同的逻辑,尝试两个for
循环来获取最后一个标记,然后是倒数第二个标记,但它没有起作用。
@echo off & setlocal EnableDelayedExpansion
PushD "D:\images" || Exit /B
For /F "EOL=? Delims=" %%G In ('Dir /A:D /B 2^>NUL
^| %SystemRoot%\System32\findstr.exe /I /R "^[^_][^_]*_[^_][^_]*_[^_][^_]*_"'
) Do (
Set "dirName=%%G"
Set "lastSegment="
For %%H In ("!dirName:_=" "!") Do (
Set "secondLastSegment=!lastSegment!"
Set "lastSegment=%%~H"
)
Set "segment=%%~xG"
Set "i=-1"
For /F "EOL=? Delims=" %%I In ('Dir "%%G\image" /A:-D /B') Do (
Set /A i += 1
SetLocal EnableDelayedExpansion
Ren "%%G\image\%%I" "!secondLastSegment!_!i!!segment!" && (
If Not Exist "!secondLastSegment!\." MD "!secondLastSegment!"
Move /Y "%%G\image\!secondLastSegment!_!i!!segment!" "!secondLastSegment!"
)
EndLocal
)
)
PopD
英文:
I have a lot of files in this format:
D:\images\AAA_BBB\image\whatever1.jpg
D:\images\AAA_BBB\image\whatever2.jpg
D:\images\FFF_EEE_CCC\image\asdf1.jpg
D:\images\FFF_EEE_CCC\image\asdf2.jpg
D:\images\WWW_XXX_YYY_ZZZ\image\someimage1.jpg
D:\images\WWW_XXX_YYY_ZZZ\image\someimage2.jpg
I'd like to move and rename these files using a substring of the absolute file path and remove the original files so that the results will look something like the below, starting with 0
.
D:\images\AAA\AAA_0.jpg
D:\images\AAA\AAA_1.jpg
D:\images\EEE\EEE_0.jpg
D:\images\EEE\EEE_1.jpg
D:\images\YYY\YYY_0.jpg
D:\images\YYY\YYY_1.jpg
Note that the number of tokens in the folder with the underscores can be any length.
The following attempt, would only give me the 2nd token starting from the front. Using "Tokens=-2" to get the 2nd last token wouldn't output anything.
@echo off & setlocal EnableDelayedExpansion
PushD "D:\images" || Exit /B
For /F "EOL=? Delims=" %%G In ('Dir /A:D /B 2^>NUL
^| %SystemRoot%\System32\findstr.exe /I /R "^[^_][^_]*_[^_][^_]*_[^_][^_]*_"'
) Do For /F "Tokens=2 EOL=? Delims=_" %%H In ("%%G") Do (
Set "i=-1"
For /F "EOL=? Delims=" %%I In ('Dir "%%G\image" /A:-D /B') Do (
Set /A i += 1
SetLocal EnableDelayedExpansion
Ren "%%G\image\%%I" "%%H_!i!%%~xI" && (
If Not Exist "%%H\." MD "%%H"
Move /Y "%%G\image\%%H_!i!%%~xI" "%%H"
)
EndLocal
)
)
PopD
Then I used different logic by trying two for
loops to get the last token, and then the 2nd last token, but it didn't work.
@echo off & setlocal EnableDelayedExpansion
PushD "D:\images" || Exit /B
For /F "EOL=? Delims=" %%G In ('Dir /A:D /B 2^>NUL
^| %SystemRoot%\System32\findstr.exe /I /R "^[^_][^_]*_[^_][^_]*_[^_][^_]*_"'
) Do (
Set "dirName=%%G"
Set "lastSegment="
For %%H In ("!dirName:_=" "!") Do (
Set "secondLastSegment=!lastSegment!"
Set "lastSegment=%%~H"
)
Set "segment=%%~xG"
Set "i=-1"
For /F "EOL=? Delims=" %%I In ('Dir "%%G\image" /A:-D /B') Do (
Set /A i += 1
SetLocal EnableDelayedExpansion
Ren "%%G\image\%%I" "!secondLastSegment!_!i!!segment!" && (
If Not Exist "!secondLastSegment!\." MD "!secondLastSegment!"
Move /Y "%%G\image\!secondLastSegment!_!i!!segment!" "!secondLastSegment!"
)
EndLocal
)
)
PopD
答案1
得分: 1
这应该可以工作:
@echo off
setlocal EnableDelayedExpansion
pushd "D:\images" || exit /B
for /D %%d in (*_*) do (
set "oldName=%%d"
for %%b in (".!oldName:_=!.") do for %%c in ("%%~Nb") do set "newName=%%~Xc"
set "newName=!newName:~1!"
md "!newName!"
set "i=-1"
for %%f in ("%%d\image\*.jpg") do (
set /A i+=1
move "%%~f" "!newName\!newName!_!i!%%~Xf"
)
)
您的示例中有许多不必要或多余的代码...
- 一般来说,当您只想处理文件或文件夹时,无需使用
for /F
命令;普通的for
或for /D
比for /F
更简单,而且速度更快。 for /D %%d in (*_*) do
命令处理至少有一个下划线的所有文件夹;我不明白为什么要使用复杂的正则表达式来使用findstr
...- 查看我的技巧,使用
~Name
和~eXtension
FOR 命令参数修改器提取_倒数第二个名称_。 - 如果您只处理每个源文件夹一次,那么只需要创建相应的目标文件夹_一次_。您不需要在_每个文件_中检查是否存在这样的文件夹。如果目标文件夹以前可能存在,您只需为每个源文件夹检查一次即可...
英文:
This should work:
@echo off
setlocal EnableDelayedExpansion
pushd "D:\images" || exit /B
for /D %%d in (*_*) do (
set "oldName=%%d"
for %%b in (".!oldName:_=.!") do for %%c in ("%%~Nb") do set "newName=%%~Xc"
set "newName=!newName:~1!"
md "!newName!"
set "i=-1"
for %%f in ("%%d\image\*.jpg") do (
set /A i+=1
move "%%~f" "!newName!\!newName!_!i!%%~Xf"
)
)
Your examples have a lot of unneeded or redundant code...
- In general, it is not necessary to use a
for /F
command when you just want to process files or folders; plainfor
orfor /D
are simpler and works faster thanfor /F
. - A
for /D %%d in (*_*) do
command process all folders with at least one underscore; I don't see why you use afindstr
with a complicated regex... - See my trick to extract the second last name using
~Name
and~eXtension
FOR command parameter modifiers. - If you process each source folder once you just need to create the corresponding target folder once. You don't need to check if such a folder exist in each file. If the target folder may previously exist, you can check it just once per source folder...
答案2
得分: 0
基于您提供的信息,这是否对您有所帮助?
@Echo Off
SetLocal EnableExtensions DisableDelayedExpansion
Set "BaseDir=D:\images";
PushD "%BaseDir%" 2>NUL || Exit /B
For /F "EOL=? Delims=" %%G In ('Dir "*_*" /A:D-L /B /O:N 2^>NUL | %SystemRoot%\System32\findstr.exe /IR "[^_][^_]*_[^_][^_]*"') Do (
PushD "%%G"
For /F "Tokens=1,* Delims=:&" %%H In ('Dir "image\*.jpg" /A:-D-L /B 2^>NUL | %SystemRoot%\System32\findstr.exe /INR "\.jpg$"') Do (
For /F "Delims=_" %%J In ("%%G") Do (
If Not Exist "%BaseDir%\%%J\." MD "%BaseDir%\%%J"
Move /Y "%BaseDir%\%%G\image\%%~I" "%BaseDir%\%%J\%%J_%%H%%~xI"
) 1>NUL
)
PopD
)
PopD
请注意,您可能有空目录需要清理!
英文:
Based upon your provided information, does this help you out?
@Echo Off
SetLocal EnableExtensions DisableDelayedExpansion
Set "BaseDir=D:\images"
PushD "%BaseDir%" 2>NUL || Exit /B
For /F "EOL=? Delims=" %%G In ('Dir "*_*" /A:D-L /B /O:N 2^>NUL
^| %SystemRoot%\System32\findstr.exe /IR "[^_][^_]*_[^_][^_]*"') Do (
PushD "%%G"
For /F "Tokens=1,* Delims=:" %%H In ('Dir "image\*.jpg" /A:-D-L /B 2^>NUL
^| %SystemRoot%\System32\findstr.exe /INR "\.jpg$"') Do (
For /F "Delims=_" %%J In ("%%G") Do (
If Not Exist "%BaseDir%\%%J\." MD "%BaseDir%\%%J"
Move /Y "%BaseDir%\%%G\image\%%~I" "%BaseDir%\%%J\%%J_%%H%%~xI"
) 1>NUL
)
PopD
)
PopD
Please be aware that you may have empty directories which you may wish to clean up too!
答案3
得分: 0
@ECHO Off
SETLOCAL ENABLEDELAYEDEXPANSION
SET "sourcedir=u:\images"
FOR /f %%e IN ('DIR /b /ad "%sourcedir%*_*"') DO (
rem %%e has first-level subdirectory name - must have at least one "_"
SET "L2=%%e"
FOR %%o IN (!L2:_^= !) DO SET "destsub=!prevpart!"&SET "prevpart=%%o"
FOR /f %%y IN ('dir /b /a-d "%sourcedir%\%%e\image\*.jpg"') DO (
MD "%sourcedir\!destsub!" >nul 2>NUL
SET "needtomove=Y"
FOR /L %%u IN (0,1 9999) DO IF DEFINED needtomove IF NOT EXIST "%sourcedir\!destsub!\!destsub!_%%u.jpg" move "%sourcedir%\%%e\image\%%y" "%sourcedir%\!destsub!\!destsub!_%%u.jpg" >nul&SET "Needtomove="
)
)
GOTO :EOF
英文:
@ECHO Off
SETLOCAL ENABLEDELAYEDEXPANSION
SET "sourcedir=u:\images"
FOR /f %%e IN ('DIR /b /ad "%sourcedir%\*_*"') DO (
rem %%e has firstlevel subdirectory name - must have at least one "_"
SET "L2=%%e"
FOR %%o IN (!L2:_^= !) DO SET "destsub=!prevpart!"&SET "prevpart=%%o"
FOR /f %%y IN ('dir /b /a-d "%sourcedir%\%%e\image\*.jpg"') DO (
MD "%sourcedir%\!destsub!" >nul 2>NUL
SET "needtomove=Y"
FOR /L %%u IN (0,1 9999) DO IF DEFINED needtomove IF NOT EXIST "%sourcedir%\!destsub!\!destsub!_%%u.jpg" move "%sourcedir%\%%e\image\%%y" "%sourcedir%\!destsub!\!destsub!_%%u.jpg" >nul&SET "Needtomove="
)
)
GOTO :EOF
Always verify against a test directory before applying to real data.
Find each subdirectory that contains _
. Find the second-last _
-separated element by replacing _
s with spaces and using a for
. For each *.jpg
in the ssubdirectory\image, try to make its destination (2>nul suppresses error messages like "Directory already exists") then try to find the first available namein the destination directory (since there is no guarantee that stray files don't already exist in the destination) and make the move.
答案4
得分: 0
以下是修复了问题的代码:
@echo off
setlocal EnableExtensions DisableDelayedExpansion
rem // 使用 `pushd`/`popd` 通常不需要在使用 `setlocal/`endlocal` 时:
cd /D "%~dp0images" || exit /B
for /F "eol=| delims=" %%G in ('
rem/ // `findstr` 过滤器太强了,不再需要 ^(简单的 `dir` 足够^)^^! ^& ^
2>nul dir /B /A:D-H-S-L "*_*"
') do (
set "dirName=%%G"
set "lastSegment="
rem // 切换到延迟扩展以仅在必要时启用它以避免丢失 `!` 符号:
setlocal EnableDelayedExpansion
for %%H in ("!dirName:_" "!"^) do for %%J in ("!lastSegment!"^) do (
endlocal
set "secondLastSegment=%%~J"
set "lastSegment=%%~H"
setlocal EnableDelayedExpansion
)
rem /* 现在每个源目录仅创建一次目标目录
rem (检查目录是否存在时,请附加 `\*` 而不是 `\.`): */
if not exist "!secondLastSegment!\*" md "!secondLastSegment!"
endlocal
set "targetDir=%%G\image"
rem // 由于 `%%~xG` 将为空白,`segment` 将为空,因此没有用:
rem/ set "segment=%%~xG"
set /A "i=-1"
rem /* `dir` 现在应指定文件名掩码以排除非图像文件
rem (现在指定了排序顺序;隐藏、系统和链接项现在被排除): */
for /F "eol=| delims=" %%I in ('cd /D "%%G\image" && dir /B /A:-D-H-S-L /O:DN "*.jpg" "*.png"') do (
set /A "i+=1"
set "fileName=%%I"
rem // 变量 `segment` 为空;请改用 `%%~xI`:
set "fileExt=%%~xI"
setlocal EnableDelayedExpansion
set "newFileName=!secondLastSegment!_!i!!fileExt!"
rem/ ren "!targetDir!\!fileName!" "!newFileName!" & (
rem 目标目录在此时已创建。
rem/ move /Y "!targetDir!\!newFileName!" "!secondLastSegment!"
rem/ )
rem /* 不必先重命名然后移动,单个移动操作就足够了
rem (请注意变量 `segment` 为空,因此请改用 `%%~xI`):
> nul move /Y "!targetDir!\!fileName!" "!secondLastSegment!\!newFileName!"
rem // 清理潜在的空子目录:
2>nul rd "!targetDir!"
endlocal
)
rem // 清理潜在的空源目录:
2>nul rd "%%G"
)
endlocal
exit /B
如果需要进一步的解释或帮助,请告诉我。
英文:
<!-- language-all:lang-cmd -->
There are several issues in your code, which I want to eliminate:
- Your
findstr
filter is too strict, hence it would only let the last two images in your example list be processed. Let me recommend to completely remove it and use the directory mask*_*
instead. - The target directory does not need to be attempted to become created per each image file; one attempt per source directory is sufficient.
- Checking a directory for existence by
if exist
results in a wrong result when appending\.
to the path (when it points to a file rather than a directory, it is also reported as existing); instead\*
should be appended, so files are reported as not existing. - You should specify a file name mask like
*.jpg
and/or*.png
for the image files to be processed in order to filter out unintended ones. - Perhaps you should specify a sort order for the image files to be processed, because otherwise, the file system determines the order, which may not be what you expect.
- Variable
segment
will be empty, because%%~xG
is going to be blank (based on your examples). You most probably meant%%~xI
. - Although technically working, first renaming the current image file and then moving it into another sub-directory is not necessary; a single movement operation is enough.
- After movement of image files, empty sub-directories may remain, which should become cleaned up.
- When using
setlocal
for environment localisation, usage ofpushd
is mostly not necessary, becausesetlocal
comprises the current working directory also. Remember that lack ofpopd
may leave unwantedpushd
stack entries behind, in contrast tosetlocal
, becauseendlocal
always becomes implicitly executed when a batch script is terminated. - Delayed variable expansion should not be globally enabled, because file/directory names containing
!
or^
in their names might cause issues.
Here is your code with said issues fixed (without touching the intrinsic methodology):
@echo off
setlocal EnableExtensions DisableDelayedExpansion
rem // Usage of `pushd`/`popd` is usually not needed when using `setlocal/`endlocal`:
cd /D "%~dp0images" || exit /B
for /F "eol=| delims=" %%G in ('
rem/ // The `findstr` filter was too strong and is unnecessary ^(a simple `dir` is enough^)^^! ^& ^
2^> nul dir /B /A:D-H-S-L "*_*"
') do (
set "dirName=%%G"
set "lastSegment="
rem // Toggle delayed expansion to have it enabled only when necessary to not lose `!`-symbols:
setlocal EnableDelayedExpansion
for %%H in ("!dirName:_=" "!") do for %%J in ("!lastSegment!") do (
endlocal
set "secondLastSegment=%%~J"
set "lastSegment=%%~H"
setlocal EnableDelayedExpansion
)
rem /* Creation of the target directory is now done once per source directory
rem (to check whether a directory exists, append `\*` rather than `\.`): */
if not exist "!secondLastSegment!\*" md "!secondLastSegment!"
endlocal
set "targetDir=%%G\image"
rem // Since `%%~xG` is going to be blank, `segment` will be empty and is therefore useless:
rem/ set "segment=%%~xG"
set /A "i=-1"
rem /* The `dir` should specify the file name mask(s) to exclude non-image files
rem (a sort order is now specified; hidden, system and linked items are now excluded): */
for /F "eol=| delims=" %%I in ('cd /D "%%G\image" ^&^& dir /B /A:-D-H-S-L /O:DN "*.jpg" "*.png"') do (
set /A "i+=1"
set "fileName=%%I"
rem // Variable `segment` is empty; use `%%~xI` instead:
set "fileExt=%%~xI"
setlocal EnableDelayedExpansion
set "newFileName=!secondLastSegment!_!i!!fileExt!"
rem/ ren "!targetDir!\!fileName!" "!newFileName!" && (
rem The target directory has already been created at this point.
rem/ move /Y "!targetDir!\!newFileName!" "!secondLastSegment!"
rem/ )
rem /* Instead of renaming and then moving, a single move operation suffices
rem (note that variable `segment` is empty, hence use `%%~xI` instead): */
> nul move /Y "!targetDir!\!fileName!" "!secondLastSegment!\!newFileName!"
rem // Clean up potentially empty sub-directory:
2> nul rd "!targetDir!"
endlocal
)
rem // Clean up potentially empty source directory:
2> nul rd "%%G"
)
endlocal
exit /B
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论