批处理文件编号保存文件

时间:2014-07-17 08:09:59

标签: batch-file export scheduled-tasks

我正在使用计划任务运行批处理文件以导出备份。在导出命令中,使用“FILE = filename”。我给了它一个名字,但我怎么能让它编号,所以它不会覆盖? (例如file1,file2,file3)。

由于

1 个答案:

答案 0 :(得分:1)

如果您在同一目录中:

@echo off
setlocal enableDelayedExpansion
set "file_name_pattern=file"
for  %%a in (file*) do (
    set "file_name=%%~na"
)

set "last_file_number=!file_name:%file_name_pattern%=!"

echo last file number : !last_file_number!

set /a next_file_number=last_file_number

set next_file=!file_name_pattern!%next_file_number%

echo next file : %next_file%

endlocal

FOR /L

的解决方案
@echo off
setlocal enableDelayedExpansion
set "file_name_pattern=file"


set init=1

for /l %%n in (1;1;10000) do (
    echo %%n
    if not exist "!file_name_pattern!%%n" (

        set last_file_number=%%n
        goto :break_for
        rem
    )
)
:break_for


echo last file number : !last_file_number!

set /a next_file_number=last_file_number+1

set next_file=!file_name_pattern!%next_file_number%

echo next file : %next_file%

endlocal