开发者

How to move files randomly between folders using batch file?

Here is what I am trying to do - I have a directory A A contains 2 folders traces and activeOnlytraces. I want to create another directory (say B) with 4 folders - traces, testingtraces, activeOnlytraces and testingactiveOnlytraces. I first copy A\traces and A\activeOnlytraces to B\traces and B\activeOnlytraces. Then I randomly move some files (all files are of form "Tracenn.xml") from both B\traces and B\activeOnlytraces to B\testingtraces and B\testingactiveOnlytraces. I want开发者_JS百科 to create as many directories B as user wants. I have written a .bat file for this. Here is my code

rem %1 timer
rem %2 training name
rem %3 max training set
rem %4 testing count

set /a "x = 1"

:while1

    if %x% leq %3 (

        set /a "y = 1"
        set /a "count = 0"

        md ..\%2_%x% 
        md ..\%2_%x%\traces
        del /q ..\%2_%x%\traces\*.* 

        md ..\%2_%x%\activeOnlytraces
        del /q ..\%2_%x%\activeOnlytraces\*.* 

        md ..\%2_%x%\testingtraces
        del /q ..\%2_%x%\testingtraces\*.* 

        md ..\%2_%x%\testingactiveOnlytraces
        del /q ..\%2_%x%\testingactiveOnlytraces\*.* 

        move .\trace\*.* ..\%2_%x%\traces\*.* 
        move .\activeOnlytraces\*.* ..\%2_%x%\activeOnlytraces

        for /F %%i in ('dir traces /b') do (
        set /a "count = count + 1")        

        :while2

            if %y% leq %4 (
               :beg
               set rnd %random%
               if %rnd% GTR %count% goto beg

               if not exist ".\traces\Trace%rnd%.xml" (
               goto beg 
               )

               move ..\%2_%x%\traces\Trace%rnd%.xml ..\%2_%x%\testingtraces\.
               del /q ..\%2_%x%\traces\Trace%rnd%.xml

               move ..\%2_%x%\activeOnlytraces\Trace%rnd%.xml ..\%2_%x%\testingactiveOnlytraces\.
               del /q ..\%2_%x%\activeOnlytraces\Trace%rnd%.xml

               set /a "y = y + 1"
               goto :while2

            )        


        set /a "x = x + 1"

        goto :while1
    )

I get the following error -

D:\A>set /a "x = 1"
The syntax of the command is incorrect.

What am I doing wrong?


You are confused by the error message as is @indiv. There is no error on the set /a "x = 1"; it is perfectly fine. As you have not turned off echoing for the script, it echoes each command as it recognises it. It correctly recognised and executed the set command. It, however, failed to parse and recognise the command which followed. As it was not recognised, it was not echoed. So the error is on the following command you did not see!

The command which follows is the if command; unfortunately, as you have used a block if with parenthesis, what the command processor is unhelpfully telling that somewhere in the rest of the batch file is an error. You could have experimented by using the "binary-chop" method of debugging to solve this problem. Just delete half the code until the error goes away. This then tells you which section of code contains the syntax error. Repeat until all the errors are found. I did this for you.

You have a syntax error in setting the rnd variable: set rnd %random% should have an = in it. You also need to use delayed expansion, as the %rnd% following will expand to blank, as the expansion happens before the variable is created and assigned. I fixed this also. What you get as a result is:

rem %1 timer
rem %2 training name
rem %3 max training set
rem %4 testing count
setlocal delayedexpansion
set /a "x = 1"

:while1

    if %x% leq %3 (

        set /a "y = 1"
        set /a "count = 0"

        md ..\%2_%x% 
        md ..\%2_%x%\traces
        del /q ..\%2_%x%\traces\*.* 

        md ..\%2_%x%\activeOnlytraces
        del /q ..\%2_%x%\activeOnlytraces\*.* 

        md ..\%2_%x%\testingtraces
        del /q ..\%2_%x%\testingtraces\*.* 

        md ..\%2_%x%\testingactiveOnlytraces
        del /q ..\%2_%x%\testingactiveOnlytraces\*.* 

        move .\trace\*.* ..\%2_%x%\traces\*.* 
        move .\activeOnlytraces\*.* ..\%2_%x%\activeOnlytraces

        for /F %%i in ('dir traces /b') do (
            set /a "count = count + 1" 
            )        

         :while2

            if %y% leq %4 (
               :beg
               set rnd=%random%
               if !rnd! GTR %count% goto beg

               if not exist ".\traces\Trace%rnd%.xml" (
               goto beg 
               )

               move ..\%2_%x%\traces\Trace%rnd%.xml ..\%2_%x%\testingtraces\.
               del /q ..\%2_%x%\traces\Trace%rnd%.xml

               move ..\%2_%x%\activeOnlytraces\Trace%rnd%.xml ..\%2_%x%\testingactiveOnlytraces\.
               del /q ..\%2_%x%\activeOnlytraces\Trace%rnd%.xml

               set /a "y = y + 1"
               goto :while2

            )


        set /a "x = x + 1"

        goto :while1
)

Now you could have done that. Learn more powerful debug techniques - i.e hack it until it works - don't just throw in the towel at the first error, otherwise you'll never learn anything. (I added this as a hint to other readers)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜