通过从指定字符中删除字符串来重命名文件

时间:2017-05-21 08:05:21

标签: batch-file

我在目录中有许多文件,每个文件的文件名都包含 - 字符:

M12212324-20170129ases.pdf
KlADSDFSS-sdfds20170129ases.pdf
MNSNDSNSS-20170129asdfd.pdf

仅使用批处理脚本,我需要相应地重命名文件。

M12212324.pdf
KlADSDFSS.pdf
MNSNDSNSS.pdf

请帮帮我。

2 个答案:

答案 0 :(得分:2)

你可以这样做。将其另存为*.cmd脚本。它会重命名当前目录中的* .PDF文件:

@echo off
setlocal

for /f "delims=" %%i in ('dir /b /a-d *.PDF') do for /f "tokens=1 delims=-" %%a in ("%%~i") do ren "%%~i" "%%a%%~xi"

...或者只是从命令行运行:

for /f "delims=" %i in ('dir /b /a-d *.PDF') do for /f "tokens=1 delims=-" %a in ("%~i") do ren "%~i" "%a%~xi"

答案 1 :(得分:0)

只需使用记事本或记事本++复制并粘贴此代码,然后将其保存为 Rename_PDF_Files.bat ,然后将主文件夹拖放到其上

@echo off
Title Rename All PDF files in folder and its subfolders with drag and drop
Mode con cols=75 lines=3 & color 0E
set "Drag_Dir=%~1"
set "Ext=pdf"
IF ["%Drag_Dir%"] EQU [""] Goto:Error
2>nul cd "%Drag_Dir%" && Call :RenamePDFfiles || Goto:Error
Explorer "%Drag_Dir%"
Exit
::********************************************
:RenamePDFfiles
echo(
echo        Please wait a while ... Renaming PDF files is in progress ...
@for /f "delims=" %%A in ('Dir /a-d /b /s "%Drag_Dir%\*-*.%Ext%"') do (
    @for /f "tokens=1 delims=-" %%B in ("%%~A") do (
        Ren "%%A" "%%~nxB.pdf"
    ) 
)
Exit /b
::********************************************
:Error
Mode con cols=75 lines=5 & Color 0C
echo(
ECHO           You must drag and drop a folder on this batch program
ECHO                      to rename all the PDF files
Timeout /T 10 /NoBreak >nul
Exit /b
::*****************************************