使用FOR循环将文件移动到另一个目录

时间:2014-02-06 18:25:03

标签: windows batch-file for-loop scripting directory

我想做什么:

  1. 提示用户输入帐号。
  2. 帐号是他们的文件夹。
  3. 然后提示用户选择另一个目录(带有时间戳的文件夹)。
  4. 然后脚本将启动可执行文件。
  5. 然后会在日志文件中解析findstr
  6. 如果字符串返回它存在,那么脚本将goto :move子例程尝试recursively移动“时间戳”目录用户选择将所有“* .ARC”文件移动到“Media1”文件夹中。
  7. 我遇到的问题是在“子例程”for中运行:move循环命令的最后一步是输出以下错误:

    The filename, directory name, or volume label syntax is incorrect.

    The filename, directory name, or volume label syntax is incorrect.

    我不确定这里发生了什么,因为我可以将它输入到命令提示符(没有!variables!)并让它工作。但是,使用变量似乎不起作用。

    另一个有趣的观点是,我基本上在!variables!“子例程下使用相同的:string,并且它找到了我要求它的确切内容。所以,我认为!variables!没有任何问题?

    我在这里错过了什么吗?

        :main
    
        cls
        echo.
        set /P acct=Please type the 9 digit account number you would like to restore: 
        set acctDir=x:\!acct!
        set acctDir2=media1\Setup\setup.exe /cd
        set log=c:\log.txt
    
    
        echo. Starting on !date! !time! on !Computername! >> !log!
        echo.
        echo The account number you selected is: !acct!
        echo.
    
        goto :user
    
    :user
    
        set /p answer=Is this correct (Y/N)? 
        echo.
    
        if /i !answer!==y goto :yes (
        ) else ( 
            echo.
            echo Ok. Let's try again^^!
            echo.
            pause
            cls
            goto :main
            )
        )
    
    :yes
    
        set c=0
        For /f %%a in ('dir !acctDir! /B /A:D') do (
        set /a c+=1
        echo !c!    %%a
        set dir!c!=%%a
        )
    
        echo. 
        set /p userIn="Select a directory [1-!c!]: "
        set userDir=!dir%userIn%!
    
        echo.
        echo You selected !userDir! for your data retrieval.
        echo.
        goto :string
    
    :execute
    
        echo.
        echo The Data Protector Program will now be initialized...
        start !acctdir!\!userDir!\!acctDir2!
    
        goto :string
    
    
    :string
    
        set sumLog=!acctdir!\!userDir!\SummaryLog.txt
        set succ=finished
        set acctDir3=media1
        set x=x:\!acct!\!userDir!
    
        findstr " .*!succ!" !sumLog!
        if exist errorlevel 0 (
            pause
            goto :move
        ) else (
            goto :eof
        )
    
    :move
    
    
        for /r "!acctdir!\!userDir!\" %%g in (*.ARC) do echo move "%%g" "!acctdir!\!userdir!\!acctdir3!\"
        if exist errorlevel 1 (
            echo Cannot move files. Error occurred.
            pause
        )
    
    
    endlocal
    goto :eof
    

1 个答案:

答案 0 :(得分:0)

这是测试变量的另一个方法。

:move标签后添加以下四行并运行批处理文件 如果有任何空格或非法字符,您将在第一次暂停之前看到它们。

:move

    echo "!acctdir!"
    echo "!userDir!"
    echo "!acctdir3!"
    pause

        for /r "!acctdir!\!userDir!\" %%g in (*.ARC) do echo move "%%g" "!acctdir!\!userdir!\!acctdir3!\"
        pause
相关问题