用CMD重命名文件时感叹号问题

发布于 2025-01-29 06:44:28 字数 1565 浏览 2 评论 0原文

我正在使用以下批次将文件随机命名文件中的文件。最初,我遇到了一个问题,非英语字母。我用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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

顾挽 2025-02-05 06:44:28

我仅显示undo = 0的方式。

chcp 65001

SETLOCAL EnableExtensions EnableDelayedExpansion
ECHO Undo mode.
IF NOT EXIST %TranslationFile% (
    ECHO Missing translation file: %TranslationFile%
    GOTO :EOF
)
FOR /F "skip=2 tokens=1,2 delims=/" %%A IN (%TranslationFile%) DO RENAME "%%B"

问题在于循环的中,您读取文件的内容。
但是,使用%% A%% B启用延迟扩展将破坏感叹号。

解决方案是至少暂时禁用延迟扩展。

setlocal DisabledDelayedExpansion
FOR /F "skip=2 tokens=1,2 delims=/" %%A IN (%TranslationFile%) DO RENAME "%%B"
endlocal

I only show the way for undo=0.

chcp 65001

SETLOCAL EnableExtensions EnableDelayedExpansion
ECHO Undo mode.
IF NOT EXIST %TranslationFile% (
    ECHO Missing translation file: %TranslationFile%
    GOTO :EOF
)
FOR /F "skip=2 tokens=1,2 delims=/" %%A IN (%TranslationFile%) DO RENAME "%%B"

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.

setlocal DisabledDelayedExpansion
FOR /F "skip=2 tokens=1,2 delims=/" %%A IN (%TranslationFile%) DO RENAME "%%B"
endlocal
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文