大量重命名文件?

时间:2015-02-14 03:38:00

标签: windows batch-file file-rename

我有一个包含46个不同文本文件的文件夹,从001.txt到046.txt,我需要添加另一个文件,比如说,30。有没有办法将所有文件从030.txt重命名为046 .txt上升一个数字,所以新的030.txt有空位? (在Windows 7上运行)

3 个答案:

答案 0 :(得分:2)

您可以使用PowerShell,它是Windows 7中内置的:

46..30|Rename-Item -Path {'{0:000}.txt'-f$_} -NewName {'{0:000}.txt'-f($_+1)}

答案 1 :(得分:1)

@ECHO Off
SETLOCAL
SET "sourcedir=U:\sourcedir\t w o"
SET "insertat="
SET /p "insertat=Insert at which number ? "
IF NOT DEFINED insertat GOTO :EOF 
SET /a insertat1=1%insertat%
SET /a howmany=1
SET /p "howmany=Insert How many ? [%howmany%]"
IF "%howmany%"=="0" GOTO :EOF 


FOR /f "delims=" %%a IN (
 'dir /b /o-n /a-d "%sourcedir%\*.txt" '
 ) DO (
 CALL :isnum %%~na
 IF NOT DEFINED notnumber SET /a maxnum=1%%~na&GOTO insert
)
ECHO maxnum NOT found
GOTO :eof

:insert
SET /a newnum=maxnum + howmany
IF EXIST "%sourcedir%\%maxnum:~1%.txt" ECHO(REN "%sourcedir%\%maxnum:~1%.txt" %newnum:~1%.txt
SET /a maxnum -=1
IF %maxnum% GEQ 1%insertat% GOTO insert 

GOTO :EOF

:: Determine whether %* is purely numeric
:isnum
SET "notnumber=%~2"
IF DEFINED notnumber GOTO :EOF 
SET "notnumber=9%~1"
FOR /l %%z IN (0,1,9) DO CALL SET "notnumber=%%notnumber:%%z=%%"
GOTO :eof

我也可以发布这个。

它会自动找到最高的文件编号并允许插入任意数量的插槽( Enter 的默认值为1)

您需要更改sourcedir的设置以适合您的具体情况。

为了测试目的,所需的REN命令仅为ECHO在您确认命令正确后,将ECHO(REN更改为REN以实际重命名文件。

答案 2 :(得分:0)

这是一个简单的纯批处理解决方案

@echo off
setlocal enableDelayedExpansion
set increment=1
set "start=030"

for /f %%F in (
  'dir /b /a-d /o-n ???.txt^|findstr /rx "[0-9][0-9][0-9]\.txt"'
) do if "%%~nF" geq "%start%" (
  set /a new=1%%~nF+increment
  ren %%F !new:~1!%%~xF
)
相关问题