为什么文件名已部分重命名?
Why the file name has renamed partially?
我尝试使用批处理脚本重命名所有文件名。但是当我使用下面的脚本时,文件名与以前的文件名部分改变了。
批次:
@ECHO OFF
Title Renamer
color 1a
setlocal enabledelayedexpansion
set HR=%time:~0,2%
set HR=%Hr: =0%
set HR=%HR: =%
echo Filenames are renaming by this user %USERNAME% in %ComputerName% @ %USERDOMAIN%
ECHO.
ECHO.
set /p input=Enter Your Path:
set /p ext=Ext:
set /p rename=Rename with:
cd /d %input%
set i=0
FOR %%F IN (%ext%) DO (
set /a i=i+1
ren %%F %rename%_!i!%ext%
)
ECHO Filenames are renaming by this user %USERNAME% in %ComputerName% @ %USERDOMAIN% on Date: %date:~7,2%-%date:~4,2%-%date:~10,4% Time: %HR%:%time:~3,2%:%time:~6,2% Source Folder: %input% Extension: %ext% Rename with: %rename% >> renamed_by-%USERNAME%-%ComputerName%_%date:~10,4%-%date:~4,2%-%date:~7,2%_%HR%%time:~3,2%%time:~6,2%.log
输出:
Enter Your Path: d:\pathtochange\
Ext: *.xhtml
Rename with: chapter
输入文件名是:
chap_01_intro.xhtml
chap_01.xhtml
chap_02.xhtml
chap_03.xhtml
chap_04_intro.xhtml
chap_04.xhtml
chap_05.xhtml
但是执行脚本后,输出显示
chapter_1.xhtml
chapter_2ntro.xhtml
chapter_3.xhtml
chapter_4.xhtml
chapter_5.xhtml
chapter_6ntro.xhtml
chapter_7.xhtml
但我想更改文件名
chapter_1.xhtml
chapter_2.xhtml
chapter_3.xhtml
chapter_4.xhtml
chapter_5.xhtml
chapter_6.xhtml
chapter_7.xhtml
为什么改成这样?
如何通过用户提示找到文件名重命名?
这似乎可能是由于您的 ren
命令中的目标名称包含通配符(来自 `%ext%)。
我建议让用户只输入扩展名(即 xhtml
而不是 *.xhtml
),然后仅在 FOR
命令中添加必要的通配符。
或者,您可以在 ren
命令中使用 %%~xF
而不是 %ext%
,这应该会达到相同的效果。
这是一个完整的示例:
set /p "input= Enter your path: "
set /p "suffix= Enter the suffix extension you want to search and rename (i.e. .TXT) : "
rem :: remove dot in suffix
set suffix=%suffix:.=%
set /p "prefix= Enter the word you want to use to rename: "
setlocal enabledelayedexpansion
set increment=0
FOR /R %input% %%a in (*.%suffix%) do (
set /a increment+=1
echo rename %%a %prefix%_!increment!%%~xa
)
endlocal
注意:去掉rename
前面的echo
命令就可以了
@echo off
rem :: set undo to true if you need to generate a reverse batch file.
set "undo=true"
set "logfile=true"
for /F "tokens=2 delims==" %%a in ('wmic OS Get localdatetime /value') do set "dt=%%a"
set "YY=%dt:~2,2%" & set "YYYY=%dt:~0,4%" & set "MM=%dt:~4,2%" & set "DD=%dt:~6,2%"
set "HH=%dt:~8,2%" & set "Min=%dt:~10,2%" & set "Sec=%dt:~12,2%"
set /p "input= Enter your path: "
set /p "suffix= Enter the suffix extension you want to search and rename (i.e. .TXT) : "
rem :: remove dot in suffix
set suffix=%suffix:.=%
set /p "prefix= Enter the word you want to use to rename: "
set "timestamp=%YYYY%%MM%%DD%-%HH%%Min%%Sec%"
if /i "%undo%"=="true" (
echo @echo off
echo rem :: user: %USERNAME%
echo rem :: Date: %DATE% %TIME%
echo rem :: Source folder: %input%
echo rem :: Extension: %suffix:.=%
echo rem :: renamed to: %prefix%
echo rem :: to undo file renaming: undo-renaming-%timestamp%.bat
)>undo-renaming-%timestamp%.bat
setlocal enabledelayedexpansion
set increment=0
(
for /r %input% %%a in (*.%suffix%) do (
set /a increment+=1
echo The file: "%%a" was renamed to "%prefix%_!increment!%%~xa"
rename "%%a" "%prefix%_!increment!%%~xa"
if "%undo%"=="true" (
echo rename "%%~dpa%prefix%_!increment!%%~xa" "%%~nxa"
)>>undo-renaming-%timestamp%.bat
)
)>reverse-rename-%timestamp%.log
if /i "%logfile%" neq "true" del reverse-rename-%timestamp%.log
endlocal
注意:在最后的脚本中,我们可以将场景推得更远,检查是否存在Undo文件,然后自动恢复。
我尝试使用批处理脚本重命名所有文件名。但是当我使用下面的脚本时,文件名与以前的文件名部分改变了。
批次:
@ECHO OFF
Title Renamer
color 1a
setlocal enabledelayedexpansion
set HR=%time:~0,2%
set HR=%Hr: =0%
set HR=%HR: =%
echo Filenames are renaming by this user %USERNAME% in %ComputerName% @ %USERDOMAIN%
ECHO.
ECHO.
set /p input=Enter Your Path:
set /p ext=Ext:
set /p rename=Rename with:
cd /d %input%
set i=0
FOR %%F IN (%ext%) DO (
set /a i=i+1
ren %%F %rename%_!i!%ext%
)
ECHO Filenames are renaming by this user %USERNAME% in %ComputerName% @ %USERDOMAIN% on Date: %date:~7,2%-%date:~4,2%-%date:~10,4% Time: %HR%:%time:~3,2%:%time:~6,2% Source Folder: %input% Extension: %ext% Rename with: %rename% >> renamed_by-%USERNAME%-%ComputerName%_%date:~10,4%-%date:~4,2%-%date:~7,2%_%HR%%time:~3,2%%time:~6,2%.log
输出:
Enter Your Path: d:\pathtochange\
Ext: *.xhtml
Rename with: chapter
输入文件名是:
chap_01_intro.xhtml
chap_01.xhtml
chap_02.xhtml
chap_03.xhtml
chap_04_intro.xhtml
chap_04.xhtml
chap_05.xhtml
但是执行脚本后,输出显示
chapter_1.xhtml
chapter_2ntro.xhtml
chapter_3.xhtml
chapter_4.xhtml
chapter_5.xhtml
chapter_6ntro.xhtml
chapter_7.xhtml
但我想更改文件名
chapter_1.xhtml
chapter_2.xhtml
chapter_3.xhtml
chapter_4.xhtml
chapter_5.xhtml
chapter_6.xhtml
chapter_7.xhtml
为什么改成这样?
如何通过用户提示找到文件名重命名?
这似乎可能是由于您的 ren
命令中的目标名称包含通配符(来自 `%ext%)。
我建议让用户只输入扩展名(即 xhtml
而不是 *.xhtml
),然后仅在 FOR
命令中添加必要的通配符。
或者,您可以在 ren
命令中使用 %%~xF
而不是 %ext%
,这应该会达到相同的效果。
这是一个完整的示例:
set /p "input= Enter your path: "
set /p "suffix= Enter the suffix extension you want to search and rename (i.e. .TXT) : "
rem :: remove dot in suffix
set suffix=%suffix:.=%
set /p "prefix= Enter the word you want to use to rename: "
setlocal enabledelayedexpansion
set increment=0
FOR /R %input% %%a in (*.%suffix%) do (
set /a increment+=1
echo rename %%a %prefix%_!increment!%%~xa
)
endlocal
注意:去掉rename
前面的echo
命令就可以了
@echo off
rem :: set undo to true if you need to generate a reverse batch file.
set "undo=true"
set "logfile=true"
for /F "tokens=2 delims==" %%a in ('wmic OS Get localdatetime /value') do set "dt=%%a"
set "YY=%dt:~2,2%" & set "YYYY=%dt:~0,4%" & set "MM=%dt:~4,2%" & set "DD=%dt:~6,2%"
set "HH=%dt:~8,2%" & set "Min=%dt:~10,2%" & set "Sec=%dt:~12,2%"
set /p "input= Enter your path: "
set /p "suffix= Enter the suffix extension you want to search and rename (i.e. .TXT) : "
rem :: remove dot in suffix
set suffix=%suffix:.=%
set /p "prefix= Enter the word you want to use to rename: "
set "timestamp=%YYYY%%MM%%DD%-%HH%%Min%%Sec%"
if /i "%undo%"=="true" (
echo @echo off
echo rem :: user: %USERNAME%
echo rem :: Date: %DATE% %TIME%
echo rem :: Source folder: %input%
echo rem :: Extension: %suffix:.=%
echo rem :: renamed to: %prefix%
echo rem :: to undo file renaming: undo-renaming-%timestamp%.bat
)>undo-renaming-%timestamp%.bat
setlocal enabledelayedexpansion
set increment=0
(
for /r %input% %%a in (*.%suffix%) do (
set /a increment+=1
echo The file: "%%a" was renamed to "%prefix%_!increment!%%~xa"
rename "%%a" "%prefix%_!increment!%%~xa"
if "%undo%"=="true" (
echo rename "%%~dpa%prefix%_!increment!%%~xa" "%%~nxa"
)>>undo-renaming-%timestamp%.bat
)
)>reverse-rename-%timestamp%.log
if /i "%logfile%" neq "true" del reverse-rename-%timestamp%.log
endlocal
注意:在最后的脚本中,我们可以将场景推得更远,检查是否存在Undo文件,然后自动恢复。