Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- @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.
- @echo off
- rem "enabledelayedexpansion" allows variables to be evaluated at runtime in loops. See "help set" for more info.
- setlocal enabledelayedexpansion
- set XMLs_src=.\mod\store\XMLs
- set XMLs_dest=.\mod\0.1.2\map\data
- set maps_src=.\mod\store\models
- set maps_dest=.\mod\0.1.2\sky\stuff
- rem "2>NUL" redirects errors to a black hole. This basically cuts out the need for "if exist %XMLs_dest% rmdir %XMLs_dest%.
- rmdir /q /s "%XMLs_dest%" 2>NUL
- rmdir /q /s "%maps_dest%" 2>NUL
- mkdir "%XMLs_dest%"
- mkdir "%maps_dest%"
- rem similar to "for (x=1; x<=15; x++)" in other languages
- for /L %%X in (1,1,15) do (
- rem "call" is like executing a function. "rn" is the argument to that function -- an empty variable we wish the :rnd subroutine to populate.
- call :rnd rn
- rem "!rn!" is like %rn% but evaluation is delayed until each loop iteration. Remember the "enabledelayedexpansion" dealio above?
- call :xml !rn!
- call :map !rn!
- )
- copy "%XMLs_dest%\*.*" "%XMLs_src%" >NUL
- 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.
- echo d | xcopy /q /f /e /y "%maps_dest%\*" "%maps_src%" >NUL
- echo Done.
- rem "goto :EOF" means goto end-of-file. Basically, "stop here and return control."
- goto :EOF
- :rnd varname
- set c=0
- 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%.
- for /d %%I in (%maps_src%\*) do set /a c+=1 >NUL
- 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%".
- set /a %1=%RANDOM% * %c% / 32768 + 1 >NUL
- rem Stop here and return control.
- goto :EOF
- :xml randomnumber
- set c=0
- 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".
- for /f %%I in ('dir /b /o:n "%XMLs_src%"') do (
- set /a c+=1 >NUL
- rem Here's another delayed expansion.
- if !c!==%1 (
- 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.
- echo %XMLs_src%\%%I -^> %XMLs_dest%\%%I
- move "%XMLs_src%\%%I" "%XMLs_dest%" >NUL
- rem Once the target of the sequence has been reached, no sense in continuing to loop. Go ahead and stop here and return control.
- goto :EOF
- )
- )
- goto :EOF
- :map randomnumber
- set c=0
- for /f %%I in ('dir /b /o:n "%maps_src%"') do (
- set /a c+=1 >NUL
- if !c!==%1 (
- echo %maps_src%\%%I -^> %maps_dest%\%%I
- echo d | xcopy /q /f /e /y "%maps_src%\%%I" "%maps_dest%\%%I" >NUL
- rmdir /q /s "%maps_src%\%%I"
- goto :EOF
- )
- )
- goto :EOF
- rem On an unrelated note...
- :: 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