2

我必须编写一个脚本,将文件复制到备份文件夹。如果文件名已经被占用,它将附加在索引上。我似乎无法让它工作。变量 tmpfile 似乎从来没有携带索引计数器。我究竟做错了什么?脚本如下。

谢谢,肖恩

set srcdir=%~dp0
set oldxladir=%srcdir%OldXLAs

if not exist %oldxladir% mkdir %oldxladir%

set pathstart=C:\Documents and Settings\
set username=%USERNAME%
set pathend=\Application Data\Microsoft\Excel\XLSTART\
set pattern=groovy*.xla

set xladir=%pathstart%%username%%pathend%
set xlapattern="%pathstart%%username%%pathend%%pattern%"

for %%f in (%xlapattern%) do (
    set filename=%%~nxf
    set srcfile=%xladir%%filename%
    set destfile=%oldxladir%\%filename%
    set tmpfile=%destfile%

    set /a index=1
    :loop
    if exist tmpfile (
        set /a index+=1
        echo %index%
        set tmpfile=%destfile%%index%
        goto :loop
    )

    echo %tmpfile%
    move "%srcfile%" "%tmpfile%"
)
4

1 回答 1

2
SETLOCAL ENABLEDELAYEDEXPANSION
set srcdir=%~dp0
set oldxladir=%srcdir%OldXLAs

if not exist %oldxladir% mkdir %oldxladir%

set pathstart=C:\Documents and Settings\
set username=%USERNAME%
set pathend=\Application Data\Microsoft\Excel\XLSTART\
set pattern=groovy*.xla

set xladir=%pathstart%%username%%pathend%
set xlapattern="%pathstart%%username%%pathend%%pattern%"

for %%f in (%xlapattern%) do (
    set filename=%%~nxf
    set srcfile=%xladir%!filename!
    set destfile=%oldxladir%\!filename!
    set tmpfile=!destfile!

    set /a index=1
    :loop
    if exist !tmpfile! (
        set /a index+=1
        echo !index!
        set tmpfile=!destfile!!index!
        goto :loop
    )

    echo !tmpfile!
    move "!srcfile!" "!tmpfile!"
)
于 2013-10-01T14:03:43.053 回答