且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

使用Windows cmd从文件名中删除字符串

更新时间:2023-02-23 18:18:39

这将满足需要.

请注意在文件为只读的情况下如何使用MOVE/Y,因为重命名无法处理重命名只读文件.

Note how I am using MOVE /Y in case the file is read-only, as Rename cannot handle renaming Read-only files.

SETLOCAL
ECHO OFF

SET "_Regex=^[0-9][0-9]*_.*"
SET "_FileGlob=*_*.Mov"
SET "_FilePath=C:\Path"

FOR %%F IN ("%_FilePath%\%_FileGlob%") DO (
    ECHO.%%~nF | FINDSTR /R "%_Regex%" >NUL && (
        FOR /F "Tokens=1* Delims=_" %%f IN ("%%~nxF") DO (
            MOVE /Y "%%~F" "%%~dpF%%~g"
        )
    )
)
ENDLOCAL