2011-04-18 24 views
0

我有一个目录中的三个文件是通过另一个进程显示:如何使用增加的数字后缀将静态命名的文件复制到其他位置?

c:\result\results-a.txt 
c:\result\results-b.txt 
c:\result\results-c.txt 

每次他们都出现的时候,我想将它们复制到另一个目录中增加数字后缀/前缀,一旦文件被复制,他们可以被删除。每次批处理文件启动时,它都可以从数字0开始(它不必扫描目标目录并继续)。

Ex。第一次文件都出现,目标目录可能是这样的:

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环境中将它们拼凑在一起,但我的客户需要在Windows NT(Windows 7,实际上)机器上完成此操作。有人能让我开始吗?

[编辑 - 答案] 感谢乔伊下面,这是我最终编码。

@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 

一个很好的批量编程的介绍。谢谢。

+0

@Joey:err,no。 – Jamie 2011-04-18 17:48:51

回答

2

您需要几件才能使其工作。

  1. 首先,计数器:

    set Counter=0 
    
  2. 然后该垫用前导零值的子程序:

    :lz 
        set LZ=000%Counter% 
        set LZ=%LZ:~-4% 
    goto :eof 
    

    %LZ:~-4%是保留最后一个子操作四个字符的变量值。在这种情况下,这是一个数字,零填充到四个地方。

  3. 一个循环,检查在特定位置中的文件:

    :loop 
        if exist c:\result\*.txt call :movefiles 
        timeout 2 /nobreak >nul 
    goto loop 
    

    相当可读的,这其中,我想。

  4. 是移动的文件远子程序:

    :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)必须以换行符终止(或高2;我有时也会出现一些问题,只有一个在我的测试这里)。这本质上是在批处理文件的末尾创建一个子例程,该例程重复设置计数器,直到达到其最后的值。尽管如此,它并不完全是快速的。每个计数器增量将有一个set调用结束,并且所有这些将在最初运行。

+0

谢谢,我会试试看看它是如何发展的。 – Jamie 2011-04-18 17:48:19

+0

@Jamie:我不得不承认,这是直接写入编辑器的,所以没有经过测试。 – Joey 2011-04-18 17:49:20

+0

你已经做足了让我开始。 – Jamie 2011-04-18 17:51:39

0

下面是应该让你开始的东西。将其放在名为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 
+0

»这个逻辑假定一个3个字符的文件扩展名«因为'%〜dpn1'和'%〜x1'太麻烦了吗? – Joey 2011-04-18 17:58:09

相关问题