Windows 如何将静态命名的文件复制到另一个具有递增数字后缀的位置?
我在一个目录中有三个文件,它们通过另一个进程显示:Windows 如何将静态命名的文件复制到另一个具有递增数字后缀的位置?,windows,scripting,batch-file,Windows,Scripting,Batch File,我在一个目录中有三个文件,它们通过另一个进程显示: c:\result\results-a.txt c:\result\results-b.txt c:\result\results-c.txt 每次它们都出现时,我想将它们复制到另一个具有递增数字后缀/前缀的目录中,一旦复制文件,就可以删除它们。每次批处理文件启动时,它都可以以数字0开始(不必扫描目标目录并继续) 例如,第一次出现所有文件时,目标目录可能如下所示: c:\archive\results-a.0000.txt c:\archiv
c:\result\results-a.txt
c:\result\results-b.txt
c:\result\results-c.txt
每次它们都出现时,我想将它们复制到另一个具有递增数字后缀/前缀的目录中,一旦复制文件,就可以删除它们。每次批处理文件启动时,它都可以以数字0开始(不必扫描目标目录并继续)
例如,第一次出现所有文件时,目标目录可能如下所示:
c:\archive\results-a.0000.txt
c:\archive\results-b.0000.txt
c:\archive\results-c.0000.txt
第二次出现时,目标目录将包含:
c:\archive\results-a.0000.txt
c:\archive\results-b.0000.txt
c:\archive\results-c.0000.txt
c:\archive\results-a.0001.txt
c:\archive\results-b.0001.txt
c:\archive\results-c.0001.txt
等等。我很乐意在BASH环境中将其拼接在一起,但我的客户机要求在windowsnt(实际上是windows7)机器上完成。有人能帮我开始吗
[编辑-回答]
多亏了下面的乔伊,这就是我最终编写的代码
@echo off
setlocal enabledelayedexpansion
set Counter=0
:loop
call :test_file %1\results1.txt
call :test_file %1\results2.txt
call :test_file %1\results3.txt
timeout 2 /nobreak >nul
call :movefiles
timeout 2 /nobreak >nul
goto loop
:test_file
timeout 2 /nobreak >nul
if not exist %1 goto :test_file
goto :eof
:lz
set LZ=000%Counter%
set LZ=%LZ:~-4%
goto :eof
:movefiles
for %%f in (C:\test\*.txt) do (
call :lz
move "%%f" "c:\tmp\c-!LZ!-%%~nxf"
)
set /a Counter+=1
goto :eof
对批处理编程的一个很好的介绍。谢谢。这里有一些应该让你开始的东西。将其放入名为
incrementName.bat的文件中,然后连续运行几次
@echo off
goto :start
--------------------------------------------
incrementName.bat
shows how to generate a filename with a monotonically
increasing numeric portion.
Run this several times in succession to see it in action.
Mon, 18 Apr 2011 12:51
--------------------------------------------
:START
setlocal enabledelayedexpansion
call :GETNEXTFILENAME rammalamma.txt
echo Next: %nextname%
@REM copy self to that new name.
copy %0 %nextname%
GOTO END
--------------------------------------------
:GETNEXTFILENAME
@REM this is a subroutine.
@REM %1 is the basename. This logic assumes a 3-character
@REM filename extension.
set fname=%1
set ext=%fname:~-3%
set base=%fname:~0,-4%
set idx=0
:toploop1
@set NUM=00000!idx!
set nextname=%base%.!NUM:~-5!.%ext%
if EXIST !nextname! (
if !idx! GTR 99999 goto:FAIL
set /a idx=!idx! + 1
goto:toploop1
)
)
:Success
goto:EOF
:Fail - overflow
set nextname=%base%.xxxxx.%ext%
goto:EOF
--------------------------------------------
:END
endlocal
你需要几件才能让它起作用
首先,计数器:
set Counter=0
然后是一个用前导零填充值的子程序:
:lz
set LZ=000%Counter%
set LZ=%LZ:~-4%
goto :eof
%LZ:~-4%
是一个保留变量值最后四个字符的子字符串操作。在本例中,这是一个数字,零填充到四位
检查特定位置中的文件的循环:
:loop
if exist c:\result\*.txt call :movefiles
timeout 2 /nobreak >nul
goto loop
我想这本书可读性很好
将文件移开的子例程:
:movefiles
setlocal enabledelayedexpansion
for %%f in (C:\result\*.txt) do (
rem Generate the zero-padded number
call :lz
move "%%f" "some\target\directory\%%~nf.!LZ!%%~xf"
)
endlocal
rem Increment the counter for next use
set /a Counter+=1
goto :eof
把这些拼凑在一起,你就有了
@echo off
setlocal enabledelayedexpansion
set Counter=0
:loop
if exist c:\result\*.txt call :movefiles
timeout 2 /nobreak >nul
goto loop
:lz
set LZ=000%Counter%
set LZ=%LZ:~-4%
goto :eof
:movefiles
for %%f in (C:\result\*.txt) do (
call :lz
move "%%f" "some\target\directory\%%~nf.!LZ!%%~xf"
)
set /a Counter+=1
goto :eof
可以对其进行调整以记住其最后一个值。但是,这仅在批处理文件位于可写位置时才起作用
@echo off
setlocal enabledelayedexpansion
set Counter=0
call :init
:loop
if exist c:\result\*.txt call :movefiles
timeout 2 /nobreak >nul
goto loop
:lz
set LZ=000%Counter%
set LZ=%LZ:~-4%
goto :eof
:movefiles
for %%f in (C:\result\*.txt) do (
call :lz
move "%%f" "some\target\directory\%%~nf.!LZ!%%~xf"
)
set /a Counter+=1
>>%~dpnx0 echo set Counter=%Counter%
goto :eof
:init
请注意,最后一行(:init
)必须以换行符终止(或者更好的两行;在这里的测试中,有时只有一行出现了一些问题)。这实际上是在批处理文件的末尾创建一个子例程,反复设置计数器,直到达到其最后一个值
不过,它并不是很快。最后,每个计数器增量将有一个set
调用,所有这些都将在最初运行。谢谢,我将尝试一下,看看它是如何运行的。@Jamie:我必须承认,这是直接写入编辑器的,所以它未经测试。我使用了最后一个代码块。。。我将发布我在回答中使用的代码。谢谢。»此逻辑假定文件扩展名为3个字符。«。。。因为%~dpn1
和%~x1
太麻烦而无法使用?