file - batch file name renumber
问题描述
I have a batch file that creates a .txt
file with Date as name, e.g. 11-09-2018.txt
.
There is a new requirement came is to append -n
number every time it runs.
Like:
- the 1st run of batch file will create the file with name 11-09-2018-1.txt
,
- the 2nd run will create 11-09-2018-2.txt
,
- the 3rd run will create 11-09-2018-3.txt
.
Below is my current code for creating batch file with Date in name :
@echo off
for /F "tokens=2" %%i in ('date /t') do set mydate=%%i
set mydate=%mydate:/=-%
set filename=%mydate%.txt
Then using %filename%
where ever I want.
Any help would be great. Thanks.
解决方案
如果已存在具有该名称的文件,请在名称后附加一个计数器 - 增加计数器。
@echo off
for /F "tokens=2" %%i in ('date /t') do set "mydate=%%i"
set "mydate=%mydate:/=-%"
set "cnt=1"
:loop
set "filename=%mydate%-%cnt%.txt"
if exist "%filename%" (set /a "cnt+=1" & goto :loop)