用CMD重命名文件时感叹号问题
我正在使用以下批次将文件随机命名文件中的文件。最初,我遇到了一个问题,非英语字母。我用CHCP 65001
解决了这个问题。但是我在感叹号方面有问题。如果文件名称具有感叹点,则批处理无法更改文件名。
chcp 65001
SETLOCAL EnableExtensions EnableDelayedExpansion
SET PrependOnly=0
SET Undo=0
SET TranslationFile=Translation.txt
IF NOT {%Undo%}=={1} (
ECHO You are about to randomly rename every file in the following folder:
ECHO %~dp0
ECHO.
ECHO A file named %TranslationFile% will be created which allows you to undo this.
ECHO Warning: If %TranslationFile% is lost/deleted, this action cannot be undone.
ECHO Type "OK" to continue.
SET /P Confirm=
IF /I NOT {!Confirm!}=={OK} (
ECHO.
ECHO Aborting.
GOTO :EOF
)
ECHO Original Name/Random Name > %TranslationFile%
ECHO ------------------------- >> %TranslationFile%
FOR /F "tokens=*" %%A IN ('DIR /A:-D /B') DO (
IF NOT %%A==%~nx0 (
IF NOT %%A==%TranslationFile% (
SET Use=%%~xA
IF {%PrependOnly%}=={1} SET Use=_%%A
SET NewName=!RANDOM!-!RANDOM!-!RANDOM!!Use!.sat
ECHO %%A/!NewName!>> %TranslationFile%
RENAME "%%A" "!NewName!"
)
)
)
) ELSE (
ECHO Undo mode.
IF NOT EXIST %TranslationFile% (
ECHO Missing translation file: %TranslationFile%
PAUSE
GOTO :EOF
)
FOR /F "skip=2 tokens=1,2 delims=/" %%A IN (%TranslationFile%) DO RENAME "%%B" "%%A"
DEL /F /Q %TranslationFile%
)
I am using the following batch to randomly name files in a folder. Initially, i had a problem non English letters. I solved this problem with chcp 65001
. But i have problem with exclamation point. If a file name has exclamation point, the batch cannot change file name.
chcp 65001
SETLOCAL EnableExtensions EnableDelayedExpansion
SET PrependOnly=0
SET Undo=0
SET TranslationFile=Translation.txt
IF NOT {%Undo%}=={1} (
ECHO You are about to randomly rename every file in the following folder:
ECHO %~dp0
ECHO.
ECHO A file named %TranslationFile% will be created which allows you to undo this.
ECHO Warning: If %TranslationFile% is lost/deleted, this action cannot be undone.
ECHO Type "OK" to continue.
SET /P Confirm=
IF /I NOT {!Confirm!}=={OK} (
ECHO.
ECHO Aborting.
GOTO :EOF
)
ECHO Original Name/Random Name > %TranslationFile%
ECHO ------------------------- >> %TranslationFile%
FOR /F "tokens=*" %%A IN ('DIR /A:-D /B') DO (
IF NOT %%A==%~nx0 (
IF NOT %%A==%TranslationFile% (
SET Use=%%~xA
IF {%PrependOnly%}=={1} SET Use=_%%A
SET NewName=!RANDOM!-!RANDOM!-!RANDOM!!Use!.sat
ECHO %%A/!NewName!>> %TranslationFile%
RENAME "%%A" "!NewName!"
)
)
)
) ELSE (
ECHO Undo mode.
IF NOT EXIST %TranslationFile% (
ECHO Missing translation file: %TranslationFile%
PAUSE
GOTO :EOF
)
FOR /F "skip=2 tokens=1,2 delims=/" %%A IN (%TranslationFile%) DO RENAME "%%B" "%%A"
DEL /F /Q %TranslationFile%
)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我仅显示
undo = 0
的方式。问题在于循环的
中,您读取文件的内容。
但是,使用
%% A
或%% B
启用延迟扩展将破坏感叹号。解决方案是至少暂时禁用延迟扩展。
I only show the way for
undo=0
.The problem is in the
FOR
loop, you read the content of a file.But using
%%A
or%%B
while delayed expansion is enabled will destroy exclamation marks.The solution is to disable delayed expansion, at least temporarily.