Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- REM copy this text into a text file and change the file ending to .bat
- REM this batch creates a shortcut onto your desktop to another batch file named play-new.bat in the same directory as this.
- REM the target directory will be randomly chosen from the subdirectory where this batch is operating in
- REM the name of the shortcut will be the same as the chosen subdirectory
- @echo off
- REM listing all direct subdirectories without recursion into the temp file
- dir /b /a:d > tmp1.txt
- REM preparing to count the number of listed directories
- set count=1
- for /f "tokens=*" %%a in (tmp1.txt) do (
- if exist tmp2.txt (
- echo A >> tmp2.txt
- ) else (
- echo A > tmp2.txt
- )
- )
- REM you might think that this preparation is useless, but it's not. folders include often symbols that are interpreted
- REM differently with delayedexpansion, that's why I only write a single char into the new file.
- REM and counting only works with delayedexpansion.
- REM counting
- SETLOCAL ENABLEDELAYEDEXPANSION
- set count=0
- for /f "tokens=*" %%a in (tmp2.txt) do (
- set /A count = !count! + 1
- )
- echo !count!>tmp2.txt
- endlocal
- set /P line=< tmp2.txt
- echo number of available folders: %line%
- set /A maxval=%line%
- set minval=1
- set /A chosen=%RANDOM% %% %maxval% + %minval%
- echo chosen number %chosen% out of %maxval%
- REM reading in a new line until the randomly generated number is reached. i.e. selecting the folder with number "chosen"
- (for /L %%i in (1,1,%chosen%) do set /P "result=")< tmp1.txt
- set path=%CD%\%result%
- echo %path%
- set "par=%path%"
- :loop
- for /f "delims=\ tokens=1,*" %%A in ("%par%") do (
- set "folder=%%A"
- set "par=%%B"
- REM the loop splits at the first \ it finds (delims=\) %%A is the first part, %%B is the second part.
- REM if %%B is empty, %%A will be the deepest subfolder. this is done because all folders are stored in absolute, not relative.
- REM in hindsight I could count the symbols of the origin path and just reduce the path by that many ... well, this works, so be it.
- )
- if NOT "%par%"=="" goto loop
- echo Anime: %folder%
- REM creating a vbs script, this is for Windows Powershell
- REM this script creates the shortcut on the desktop
- set SCRIPT="%RANDOM%-%RANDOM%-%RANDOM%-%RANDOM%.vbs"
- echo %SCRIPT%
- echo Set oWS = WScript.CreateObject("WScript.Shell")>> %SCRIPT%
- echo sLinkFile = "%USERPROFILE%\Desktop\%folder%.lnk">> %SCRIPT%
- REM path to the new shortcut including name,
- REM %userprofile% is a hard coded symbol link in windows like %temp% or %appdata%
- echo Set oLink = oWS.CreateShortcut(sLinkFile)>> %SCRIPT%
- REM shortcut file is created at that point and attributes are changed below afterwards
- echo oLink.TargetPath = "%CD%\play-new.bat">> %SCRIPT%
- echo oLink.Arguments = "-sub">> %SCRIPT%
- REM adding arguments has to be seperated because the path it self needs the quotation marks to work,
- REM but the command won't add anything outside the quotation marks, I tried escaping but with no success. this is easier.
- echo oLink.WorkingDirectory = "%path%">> %SCRIPT%
- echo oLink.Save>> %SCRIPT%
- REM save and execute
- c:\windows\system32\cscript.exe /nologo "%SCRIPT%"
- REM cleanup
- del %SCRIPT%
- del tmp1.txt
- del tmp2.txt
- del tmp3.txt
Add Comment
Please, Sign In to add comment