使用批处理文件重命名文件

时间:2013-10-02 14:57:15

标签: batch-file

我想使用批处理文件按日期重命名文件。现在我的挑战是,如果文件名存在,它应该在最后的每个文件上附加(1)..(2)..(3)。

ren c:\LogFiles\*.log BackupLog-%date:~-4,4%%date:~-7,2%%date:~-10,2%.log

我还想知道是否有可能

  • 按照前一个日期重命名文件,即当前日期的-1(如果我今天执行批处理文件,则按照昨天的日期重命名。
  • 根据创建的日期重命名文件。

如果无法使用批处理文件,请协助使用power shell

1 个答案:

答案 0 :(得分:1)

试试这个问题:

@echo off &setlocal
set "logpath=c:\LogFiles"
for /f "delims=" %%a in ('dir /b /a-d /od "%logpath%\*.log"') do call:doit "%logpath%\%%~a"
goto:eof

:doit
setlocal
set "npre=BackupLog-%date:~-4,4%%date:~-7,2%%date:~-10,2%"
:loop
if defined fcnt (set "nname=%npre% (%fcnt%)%~x1") else set "nname=%npre%%~x1"
set /a cnt+=1
set "fcnt=00%cnt%"
set "fcnt=%fcnt:~-3%"
if exist "%logpath%\%nname%" goto:loop
echo ren "%~1" "%nname%"
ren "%~1" "%nname%"
endlocal
exit /b

如果您还有其他问题,请new questions

相关问题