Advertisement
rojo

randomly copy 15 folders / paired files

Jan 30th, 2013
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Batch 3.76 KB | None | 0 0
  1. @rem Prefacing a line with a "@" makes that line not echo.  So "@echo off" not only tells every subsequent line not to echo, but itself as well.  If it were just "echo off", the "echo off" itself would still echo.
  2. @echo off
  3. rem "enabledelayedexpansion" allows variables to be evaluated at runtime in loops.  See "help set" for more info.
  4. setlocal enabledelayedexpansion
  5. set XMLs_src=.\mod\store\XMLs
  6. set XMLs_dest=.\mod\0.1.2\map\data
  7. set maps_src=.\mod\store\models
  8. set maps_dest=.\mod\0.1.2\sky\stuff
  9.  
  10. rem "2>NUL" redirects errors to a black hole.  This basically cuts out the need for "if exist %XMLs_dest% rmdir %XMLs_dest%.
  11. rmdir /q /s "%XMLs_dest%" 2>NUL
  12. rmdir /q /s "%maps_dest%" 2>NUL
  13. mkdir "%XMLs_dest%"
  14. mkdir "%maps_dest%"
  15.  
  16. rem similar to "for (x=1; x<=15; x++)" in other languages
  17. for /L %%X in (1,1,15) do (
  18. rem "call" is like executing a function.  "rn" is the argument to that function -- an empty variable we wish the :rnd subroutine to populate.
  19.     call :rnd rn
  20. rem "!rn!" is like %rn% but evaluation is delayed until each loop iteration.  Remember the "enabledelayedexpansion" dealio above?
  21.     call :xml !rn!
  22.     call :map !rn!
  23. )
  24. copy "%XMLs_dest%\*.*" "%XMLs_src%" >NUL
  25. rem This one is tricky.  "xcopy" has no switch to specify whether the target is a directory or a filename.  Unless the target already exists, xcopy will pause execution and ask the user to enter d or f.  Piping xcopy to the output of "echo d" simulates the user choosing "d".  Then ">NUL" makes the whole thing silent.
  26. echo d | xcopy /q /f /e /y "%maps_dest%\*" "%maps_src%" >NUL
  27. echo Done.
  28. rem "goto :EOF" means goto end-of-file.  Basically, "stop here and return control."
  29. goto :EOF
  30.  
  31. :rnd varname
  32. set c=0
  33. rem "for /d %%I in (*)" means for each directory.  "set /a c+=1" means c=c+1.  By the end of the looping, c=number of directories in %maps_src%.
  34. for /d %%I in (%maps_src%\*) do set /a c+=1 >NUL
  35. rem %1 is the argument passed by the caller.  Remember "call :rnd rn" earlier?  So this is basically "set rn=random number between 1 and %c%".
  36. set /a %1=%RANDOM% * %c% / 32768 + 1 >NUL
  37. rem Stop here and return control.
  38. goto :EOF
  39.  
  40. :xml randomnumber
  41. set c=0
  42. rem It seemed that having this line as "for %%I in (%XMLs_src%)" didn't always pass the files in alphabetical order.  Force alphabetical order by getting the file list via "dir /b /o:n".
  43. for /f %%I in ('dir /b /o:n "%XMLs_src%"') do (
  44.     set /a c+=1 >NUL
  45. rem Here's another delayed expansion.
  46.     if !c!==%1 (
  47. rem The ^ (the carat) makes the next character get treated literally, making any special meaning ignored.  So instead of redirecting the output of "echo %XMLs_src%\%%I" to a file called %XMLs_dest%\%%I, it's now simply displaying an arrow.
  48.         echo %XMLs_src%\%%I -^> %XMLs_dest%\%%I
  49.         move "%XMLs_src%\%%I" "%XMLs_dest%" >NUL
  50. rem Once the target of the sequence has been reached, no sense in continuing to loop.  Go ahead and stop here and return control.
  51.         goto :EOF
  52.     )
  53. )
  54. goto :EOF
  55.  
  56. :map randomnumber
  57. set c=0
  58. for /f %%I in ('dir /b /o:n "%maps_src%"') do (
  59.     set /a c+=1 >NUL
  60.     if !c!==%1 (
  61.         echo %maps_src%\%%I -^> %maps_dest%\%%I
  62.         echo d | xcopy /q /f /e /y "%maps_src%\%%I" "%maps_dest%\%%I" >NUL
  63.         rmdir /q /s "%maps_src%\%%I"
  64.         goto :EOF
  65.     )
  66. )
  67. goto :EOF
  68.  
  69. rem On an unrelated note...
  70. :: Interestingly, you can also insert remarks with a double-colon.  The double-colon must be at the beginning of the line with no indentation, and can't exist within a for loop or if statement.  The double-colon is basically a place marker like :rnd or :xml above, but with the name of ":".  Since there is no directive in the script to "call ::" or "goto :" anything after a :: marker gets ignored.  It can't be used in as many places as "rem" but it does have a neater appearance.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement