Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- @ECHO OFF
- setlocal EnableDelayedExpansion
- ::save the current directory it is set to
- ::so we can put it back later
- set "oldCD=%cd%"
- ::if program is started with inputs,
- ::run the commandline version of the program.
- if NOT "%~1"=="" goto commandLine
- ::if the program is NOT started with inputs,
- ::go to the normal version of the program.
- goto beginning
- ::zlib License
- ::
- ::(C) 2019 rerere284
- ::(some functions from stackoverflow where noted)
- ::
- ::This software is provided 'as-is', without any express or implied
- ::warranty. In no event will the authors be held liable for any damages
- ::arising from the use of this software.
- ::
- ::Permission is granted to anyone to use this software for any purpose,
- ::including commercial applications, and to alter it and redistribute it
- ::freely, subject to the following restrictions:
- ::
- ::1. The origin of this software must not be misrepresented; you must not
- :: claim that you wrote the original software. If you use this software
- :: in a product, an acknowledgment in the product documentation would be
- :: appreciated but is not required.
- ::2. Altered source versions must be plainly marked as such, and must not be
- :: misrepresented as being the original software.
- ::3. This notice may not be removed or altered from any source distribution.
- :disclaimer
- call :echoWrap This program is licensed under the zlib license. This program is intended for randomizing media files in games. Please be careful. Randomizing IMPORTANT FOLDERS or YOUR ENTIRE COMPUTER is NOT RECOMMENDED. Running the program again in the same location will run the undo function. The undo function DELETES the files created by the program and restores the old files if they are present in the folder.
- EXIT /B 0
- :beginning
- ::temporarily endlocal so we can write a '!'
- endlocal
- echo/
- call :header Welcome to the file randomizer!
- setlocal EnableDelayedExpansion
- echo by rerere284
- echo/
- call :disclaimer
- echo/
- echo/
- :: TODO:
- ::disclaimer
- ::youtube-dl support???
- ::a bit feature-creep-y
- :: DONE:
- ::undo option
- ::auto detect that it has been done before
- ::files prefixed with [oldfile] of type filetype
- ::option to either undo or back out to main menu
- ::recursive option
- ::dir is always recursive
- ::more of a 'non-recursive option'
- ::non recursive option might be pointless
- ::might help with minecraft resource packs
- ::dir's /S might do this?
- ::yep
- ::option selection
- ::welcome to the file randomizer!
- ::(enter file locations etc.)
- ::check if undo should be offered
- ::offer undo or continue program
- ::either way, display menu for recursive option
- ::echo 1) Option 1
- ::echo 2) Option 2
- ::set /p op=Type option:
- ::if "%op%"=="1" goto op1
- ::if "%op%"=="2" goto op2
- ::progress bar
- ::some feature creep
- ::run via command line
- ::tell people they can drag files into the window to paste the path
- ::sanity check if too many files are going to get randomized
- ::add sufficient notes to the code
- ::inputted paths can lack quotes and still have spaces
- ::get path of old files from user
- ::and check that the path is valid
- call :enterOld
- call :checkOld
- ::ask user if they want the program to check recursively
- call :offerRecurse
- ::get file type of files from user
- ::and check that files of that type exist
- call :enterType
- call :checkType
- ::check if the program has already been run
- ::can't run the program on the same files twice
- ::if the program has been run, go ask the user if they want to undo
- set shouldUndo=false
- call :checkUndo
- if !shouldUndo!==true (
- goto doUndo
- )
- ::if the program hasn't been run,
- ::get path of new files from user
- ::and check that the path is valid
- call :enterNew
- call :checkNew
- ::we have all our data, start the program
- goto start
- :enterNew
- echo/
- echo input or drag in folder containing new files to swap in:
- set /p NEWF=folder:
- ::replace quotes in path with nothing
- set NEWF=%NEWF:"=%
- EXIT /B 0
- :checkNew
- if not exist "%NEWF%" (
- echo directory %NEWF% not found
- call :enterNew
- call :checkNew
- )
- if "%NEWF%"=="%OLDF%" (
- echo please choose a different directory than the one you picked previously.
- call :enterNew
- call :checkNew
- )
- EXIT /B 0
- :enterOld
- echo/
- echo input or drag in folder containing old files to swap out:
- set /p OLDF=folder:
- ::replace quotes in path with nothing
- set OLDF=%OLDF:"=%
- EXIT /B 0
- :checkOld
- if not exist "%OLDF%" (
- echo directory %OLDF% not found
- call :enterOld
- call :checkOld
- )
- EXIT /B 0
- :enterType
- echo/
- echo input file type being handled (png, mp3, map...):
- set /p filetype=type:
- ::replace dots in filetype with nothing
- set filetype=%filetype:.=%
- EXIT /B 0
- :checkType
- ::check that files of that type exist there
- cd /d !OLDF!
- dir /a-d %recurse%*.%filetype% >nul 2>&1 || (
- echo could't find file of type %filetype% in %OLDF%
- call :enterType
- call :checkType
- )
- cd /d "%oldCD%"
- EXIT /B 0
- :offerRecurse
- echo/
- echo Do you want to check recursively?
- echo This will check inside of all folders within the directory.
- :askRecurse
- set /p undoOp=(Y/N):
- if !undoOp!==y goto doRecurseChecks
- if !undoOp!==Y goto doRecurseChecks
- if !undoOp!==n EXIT /B 0
- if !undoOp!==N EXIT /B 0
- goto askRecurse
- :doRecurseChecks
- set "recurse=/S "
- echo will now do checks recursively, checking inside all folders within the directory.
- EXIT /B 0
- :start
- ::you can tell by the comments how much trouble this gave me
- ::for each file in NEWF of the type filetype
- ::add it to a list
- set /a numberr=0
- cd /d %NEWF%
- for /f "delims=" %%f in ('dir /b %recurse%*.%filetype%') do (
- set /a numberr=!numberr!+1
- set list[!numberr!]=%%~ff
- REM set listVar=list[!numberr!]
- REM echo numberr !numberr!
- REM call echo list[!numberr!] %%!listVar!%%
- REM call echo list[!numberr!] %%list[!numberr!]%%
- )
- cd /d "%oldCD%"
- ::for /l %%a in (1, 1, !numberr!) do (
- ::echo %%a
- ::echo !list[%%a]!
- ::)
- ::set current directory to where dir command is being run
- cd /d %OLDF%
- :: Count the number of files
- set oN=0
- for /f "delims=" %%f in ('dir /b %recurse%*.%filetype%') do set /A oN+=1
- :: if there are a lot of files, double check
- if %oN% gtr 100 (
- rem sanity check due to number of files
- echo Are you sure you want to randomize %~1 .%filetype% files in %OLDF% ?
- :askSanity
- set /p undoOp=(Y/N):
- if !undoOp!==y goto doneSanityCheck
- if !undoOp!==Y goto doneSanityCheck
- if !undoOp!==n (
- rem reset current directory and exit
- cd /d "%oldCD%"
- EXIT /B %ERRORLEVEL%
- )
- if !undoOp!==N (
- rem reset current directory and exit
- cd /d "%oldCD%"
- EXIT /B %ERRORLEVEL%
- )
- goto askSanity
- :doneSanityCheck
- )
- ::for each file in OLDF of the type filetype
- ::set a variable to the name of that file
- ::prepend the OLDF file name with some weird string
- ::pick a random file from the NEWF list
- ::copy that file to OLDF, naming it the name of the file
- set oNN=0
- for /f "delims=" %%f in ('dir /b %recurse%*.%filetype%') do (
- set TEMPNAME=%%~nf
- ren "%%f" "[oldfile]!TEMPNAME!.%filetype%"
- rem echo ren !TEMPNAME!.%filetype%
- set /a "rand=(numberr*!random!)/32768+1"
- rem echo rand !rand!
- call set thisFile=%%list[!rand!]%%
- rem echo thisFile !thisFile!
- rem call echo list[!rand!] %%list[!rand!]%%
- rem echo TEMPNAME !TEMPNAME!
- copy /-Y "!thisFile!" "!TEMPNAME!.%filetype%" >nul
- rem echo copy /-Y "!thisFile!" "!TEMPNAME!.%filetype%"
- set /A oI=100*oNN/oN
- call :drawProgressBar oI "randomizing files..."
- set /A oNN+=1
- )
- cd /d "%oldCD%"
- call :drawProgressBar 100 "randomizing files..."
- echo/
- echo/
- echo/
- ::temporarily endlocal so we can print '!'
- endlocal
- call :header Files randomized! Enjoy your randomization!
- setlocal EnableDelayedExpansion
- echo/
- echo old filenames have been prefixed with [oldfile]
- echo if you want this undone, run this program again with the same parameters.
- pause >nul
- EXIT /B %ERRORLEVEL%
- :checkUndo
- cd /d %OLDF%
- rem check for files with the prefix [oldfile] of type filetype
- dir /a-d %recurse%[oldfile]*.!filetype! >nul 2>&1 && set shouldUndo=true
- cd /d "%oldCD%"
- EXIT /B 0
- :doUndo
- echo/
- echo Randomized files of type %filetype% detected. Unable to randomize further.
- echo Undo randomization? Current random files will be replaced by old files.
- :askUndo
- set /p undoOp=(Y/N):
- if !undoOp!==y goto continueUndo
- if !undoOp!==Y goto continueUndo
- if !undoOp!==n goto beginning
- if !undoOp!==N goto beginning
- goto askUndo
- :continueUndo
- ::for each file in OLDF with the prefix
- ::set a variable to the name of that file
- ::assemble directory of version without prefix
- ::if that exists
- ::delete that
- ::rename this file to be version without prefix
- cd /d %OLDF%
- rem Count the number of files
- set oN=0
- for /f "delims=" %%f in ('dir /b %recurse%[oldfile]*.%filetype%') do set /A oN+=1
- set oNN=0
- for /f "delims=" %%f in ('dir /b %recurse%[oldfile]*.%filetype%') do (
- REM endlocal
- REM setlocal DisableDelayedExpansion
- set TEMPNAME=%%~nf
- set NEWPATH=%%~dpf
- REM endlocal
- REM setlocal EnableDelayedExpansion
- set NEWNAME=!TEMPNAME:~9!.%filetype%
- REM set NEWPATH=!NEWPATH:[oldfile]=!
- set NEWPATH=!NEWPATH!!NEWNAME!
- if exist !NEWPATH! (
- REM echo at %%f
- REM echo !NEWNAME! exists
- del /f "!NEWPATH!"
- )
- REM ren "%%f" "!TEMPNAME:[oldfile]=!.%filetype%"
- ren "%%f" "!NEWNAME!"
- set /A oI=100*oNN/oN
- call :drawProgressBar oI "unrandomizing files..."
- set /A oNN+=1
- )
- cd /d "%oldCD%"
- call :drawProgressBar 100 "unrandomizing files..."
- echo/
- endlocal
- call :header Files unrandomized! Enjoy your organization!
- rem Files unrandomized! Enjoy your ____ization!
- rem unrandimization
- rem organization
- rem catagorization
- rem normalization
- rem realization
- rem recatagorization
- rem personalization
- rem sanitization
- rem naturalization
- rem optimization
- rem weird bug, whatever, it works
- call pause >nul
- pause >nul
- EXIT /B %ERRORLEVEL%
- :commandLine
- ::commandline version of the program
- ::if the first input is 'h', display help and exit
- if "%~1"=="/h" goto commandHelp
- if "%~1"=="/H" goto commandHelp
- if "%~1"=="-h" goto commandHelp
- if "%~1"=="-H" goto commandHelp
- if "%~1"=="/?" goto commandHelp
- set paramUndo=false
- set paramRecurse=false
- set paramSilent=false
- FOR %%A IN (%*) DO (
- rem variable to keep track of what the current variable is
- set wasParam=false
- rem check for the undo param
- if "%%A"=="/u" set "paramUndo=true" & set wasParam=true
- if "%%A"=="/U" set "paramUndo=true" & set wasParam=true
- if "%%A"=="-u" set "paramUndo=true" & set wasParam=true
- if "%%A"=="-U" set "paramUndo=true" & set wasParam=true
- rem check for the recurse param
- if "%%A"=="/r" set "paramRecurse=true" & set wasParam=true
- if "%%A"=="/R" set "paramRecurse=true" & set wasParam=true
- if "%%A"=="-r" set "paramRecurse=true" & set wasParam=true
- if "%%A"=="-R" set "paramRecurse=true" & set wasParam=true
- rem check for the silent param
- if "%%A"=="/s" set "paramSilent=true" & set wasParam=true
- if "%%A"=="/S" set "paramSilent=true" & set wasParam=true
- if "%%A"=="-s" set "paramSilent=true" & set wasParam=true
- if "%%A"=="-S" set "paramSilent=true" & set wasParam=true
- rem if the current variable was not a param
- rem that means it is a path or a filetype
- rem set the first one of those that is not defined
- if !wasParam!==false (
- if not defined NEWF (
- set NEWF=%%A
- if not exist "!NEWF!" (
- echo directory !NEWF! not found
- EXIT /B 1
- )
- ) else (
- if not defined OLDF (
- set OLDF=%%A
- if not exist "!OLDF!" (
- echo directory !OLDF! not found
- EXIT /B 1
- )
- ) else (
- if not defined filetype (
- if !paramRecurse!==true set "recurse=/S "
- set filetype=%%A
- cd /d !OLDF!
- dir /a-d !recurse!*.!filetype! >nul 2>&1 || (
- echo could't find file of type !filetype! in !OLDF!
- cd /d "%oldCD%"
- EXIT /B 1
- )
- cd /d "%oldCD%"
- ) else (
- echo extra param %%A
- )
- )
- )
- )
- )
- if not defined NEWF (
- echo pathToNewFiles not given
- EXIT /B 1
- )
- if not defined OLDF (
- echo pathToOldFiles not given
- EXIT /B 1
- )
- if not defined filetype (
- echo fileType not given
- EXIT /B 1
- )
- ::check if you should undo instead of running normally
- ::by checking if the program has been run before
- set shouldUndo=false
- call :checkUndo
- ::if paramUndo and shouldUndo don't match, the program exits
- if !paramUndo!==false (
- if !shouldUndo!==true (
- echo randomized files of type !filetype! already present in !OLDF!
- EXIT /B 1
- )
- rem for each file in NEWF of the type filetype
- rem add it to a list
- set /a numberr=0
- cd /d "!NEWF!"
- for /f "delims=" %%f in ('dir /b !recurse!*.!filetype!') do (
- set /a numberr=!numberr!+1
- set list[!numberr!]=%%~ff
- )
- cd /d "%oldCD%"
- rem for each file in OLDF of the type filetype
- rem set a variable to the name of that file
- rem prepend the OLDF file name with some weird string
- rem pick a random file from the NEWF list
- rem copy that file to OLDF, naming it the name of the file
- cd /d "!OLDF!"
- rem Count the number of files
- set oN=0
- for /f "delims=" %%f in ('dir /b !recurse!*.!filetype!') do set /A oN+=1
- set oNN=0
- cd /d "!OLDF!"
- for /f "delims=" %%f in ('dir /b !recurse!*.!filetype!') do (
- set TEMPNAME=%%~nf
- set OLDPATH=%%~ff
- ren "%%f" "[oldfile]!TEMPNAME!.!filetype!"
- set /a "rand=(numberr*!random!)/32768+1"
- call set thisFile=%%list[!rand!]%%
- copy /-Y "!thisFile!" "!OLDPATH!" >nul
- set /A oI=100*oNN/oN
- if !paramSilent!==false (
- call :drawProgressBar oI "randomizing files..."
- )
- set /A oNN+=1
- )
- cd /d "%oldCD%"
- if !paramSilent!==false (
- call :drawProgressBar 100 "randomizing files..."
- echo/
- echo/
- echo/
- endlocal
- call :header Files randomized! Enjoy your randomization!
- setlocal EnableDelayedExpansion
- echo/
- echo old filenames have been prefixed with [oldfile]
- echo if you want this undone, run this program again with the same parameters.
- )
- EXIT /B %ERRORLEVEL%
- ) else (
- if !shouldUndo!==false (
- echo randomized files of type !filetype! not present in !OLDF!
- EXIT /B 1
- )
- cd /d "!OLDF!"
- rem Count the number of files
- set oN=0
- for /f "delims=" %%f in ('dir /b !recurse![oldfile]*.!filetype!') do set /A oN+=1
- rem for each file in OLDF with the prefix
- rem set a variable to the name of that file
- rem assemble directory of version without prefix
- rem if that exists
- rem delete that
- rem rename this file to be version without prefix
- set oNN=0
- for /f "delims=" %%f in ('dir /b !recurse![oldfile]*.!filetype!') do (
- set TEMPNAME=%%~nf
- set NEWPATH=%%~dpf
- set NEWNAME=!TEMPNAME:~9!.!filetype!
- set NEWPATH=!NEWPATH!!NEWNAME!
- if exist !NEWPATH! (
- del /f "!NEWPATH!"
- )
- ren "%%f" "!NEWNAME!"
- set /A oI=100*oNN/oN
- if !paramSilent!==false (
- call :drawProgressBar oI "unrandomizing files..."
- )
- set /A oNN+=1
- )
- cd /d "%oldCD%"
- if !paramSilent!==false (
- rem draw progress bar at 100% since it is done
- call :drawProgressBar 100 "unrandomizing files..."
- echo/
- endlocal
- call :header Files unrandomized! Enjoy your organization!
- )
- EXIT /B %ERRORLEVEL%
- )
- EXIT /B %ERRORLEVEL%
- :commandHelp
- echo Randomizes a set of files to be another provided set of files of the same type.
- echo/
- echo Usage: randomizeFiles.bat [options] "pathToNewFiles" "pathToOldFiles" fileType
- echo/
- echo -h show this help
- echo -u undo a randomization
- echo -r check recursively
- echo -s silent mode
- echo/
- echo/
- EXIT /B %ERRORLEVEL%
- :header
- ::print inputted text between two bars
- ECHO =================================================
- ECHO %*
- ECHO =================================================
- EXIT /B 0
- REM https://stackoverflow.com/questions/21108380/coding-a-real-time-progress-bar-in-batch
- :drawProgressBar value [text]
- if "%~1"=="" goto :eof
- if not defined pb.barArea call :initProgressBar
- setlocal enableextensions enabledelayedexpansion
- set /a "pb.value=%~1 %% 101", "pb.filled=pb.value*pb.barArea/100", "pb.dotted=pb.barArea-pb.filled", "pb.pct=1000+pb.value"
- set "pb.pct=%pb.pct:~-3%"
- if "%~2"=="" ( set "pb.text=" ) else (
- set "pb.text=%~2%pb.back%"
- set "pb.text=!pb.text:~0,%pb.textArea%!"
- )
- <nul set /p "pb.prompt=[!pb.fill:~0,%pb.filled%!!pb.dots:~0,%pb.dotted%!][ %pb.pct% ] %pb.text%!pb.cr!"
- endlocal
- EXIT /B 0
- REM https://stackoverflow.com/questions/21108380/coding-a-real-time-progress-bar-in-batch
- :initProgressBar [fillChar] [dotChar]
- if defined pb.cr call :finalizeProgressBar
- for /f %%a in ('copy "%~f0" nul /z') do set "pb.cr=%%a"
- if "%~1"=="" ( set "pb.fillChar=#" ) else ( set "pb.fillChar=%~1" )
- if "%~2"=="" ( set "pb.dotChar=." ) else ( set "pb.dotChar=%~2" )
- set "pb.console.columns="
- for /f "tokens=2 skip=4" %%f in ('mode con') do if not defined pb.console.columns set "pb.console.columns=%%f"
- set /a "pb.barArea=pb.console.columns/2-2", "pb.textArea=pb.barArea-9"
- set "pb.fill="
- setlocal enableextensions enabledelayedexpansion
- for /l %%p in (1 1 %pb.barArea%) do set "pb.fill=!pb.fill!%pb.fillChar%"
- set "pb.fill=!pb.fill:~0,%pb.barArea%!"
- set "pb.dots=!pb.fill:%pb.fillChar%=%pb.dotChar%!"
- set "pb.back=!pb.fill:~0,%pb.textArea%!
- set "pb.back=!pb.back:%pb.fillChar%= !"
- endlocal & set "pb.fill=%pb.fill%" & set "pb.dots=%pb.dots%" & set "pb.back=%pb.back%"
- EXIT /B 0
- REM https://stackoverflow.com/questions/21108380/coding-a-real-time-progress-bar-in-batch
- :finalizeProgressBar [erase]
- if defined pb.cr (
- if not "%~1"=="" (
- setlocal enabledelayedexpansion
- set "pb.back="
- for /l %%p in (1 1 %pb.console.columns%) do set "pb.back=!pb.back! "
- <nul set /p "pb.prompt=!pb.cr!!pb.back:~1!!pb.cr!"
- endlocal
- )
- )
- for /f "tokens=1 delims==" %%v in ('set pb.') do set "%%v="
- EXIT /B 0
- REM https://stackoverflow.com/questions/20943733/in-batch-can-i-make-a-line-of-displayed-text-not-split-words-appart-when-it-goes#20944604
- :echoWrap [text]
- rem Get the window width
- set "width="
- for /f "tokens=2 skip=4" %%f in ('mode con') do if not defined width set /a "width=%%f-1"
- set "output="
- rem For each word in input line
- for %%b in (%*) do (
- rem Add the new word
- set "newOutput=!output! %%b"
- if "!output!" equ "" set newOutput=!newOutput: =!
- rem If new word don't exceed window width
- if "!newOutput:~%width%,1!" equ "" (
- rem Keep it
- set "output=!newOutput!"
- ) else (
- rem Show the output before the new word
- echo !output!
- rem and store the new word
- set "output=%%b"
- )
- )
- rem Show the last output, if any
- if defined output echo !output!
- EXIT /B 0
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement