Tiavor

shortcut to other batch with random folder as working dir

Jan 4th, 2022 (edited)
331
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Batch 3.15 KB | None | 0 0
  1. REM copy this text into a text file and change the file ending to .bat
  2.  
  3. REM this batch creates a shortcut onto your desktop to another batch file named play-new.bat in the same directory as this.
  4. REM the target directory will be randomly chosen from the subdirectory where this batch is operating in
  5. REM the name of the shortcut will be the same as the chosen subdirectory
  6.  
  7. @echo off
  8.  
  9. REM listing all direct subdirectories without recursion into the temp file
  10. dir /b /a:d > tmp1.txt
  11.  
  12. REM preparing to count the number of listed directories
  13. set count=1
  14. for /f "tokens=*" %%a in (tmp1.txt) do (
  15.     if exist tmp2.txt (
  16.         echo A >> tmp2.txt
  17.     ) else (
  18.         echo A > tmp2.txt
  19.     )
  20. )
  21. REM you might think that this preparation is useless, but it's not. folders include often symbols that are interpreted
  22. REM differently with delayedexpansion, that's why I only write a single char into the new file.
  23. REM and counting only works with delayedexpansion.
  24.  
  25. REM counting
  26. SETLOCAL ENABLEDELAYEDEXPANSION
  27. set count=0
  28. for /f "tokens=*" %%a in (tmp2.txt) do (
  29.     set /A count = !count! + 1
  30. )
  31. echo !count!>tmp2.txt
  32. endlocal
  33.  
  34. set /P line=< tmp2.txt
  35.  
  36. echo number of available folders: %line%
  37. set /A maxval=%line%
  38. set minval=1
  39. set /A chosen=%RANDOM% %% %maxval% + %minval%
  40. echo chosen number %chosen% out of %maxval%
  41.  
  42. REM reading in a new line until the randomly generated number is reached. i.e. selecting the folder with number "chosen"
  43. (for /L %%i in (1,1,%chosen%) do set /P "result=")< tmp1.txt
  44. set path=%CD%\%result%
  45. echo %path%
  46.  
  47. set "par=%path%"
  48. :loop
  49. for /f "delims=\ tokens=1,*" %%A in ("%par%") do (
  50.     set "folder=%%A"
  51.     set "par=%%B"
  52. REM the loop splits at the first \ it finds (delims=\) %%A is the first part, %%B is the second part.
  53. REM if %%B is empty, %%A will be the deepest subfolder. this is done because all folders are stored in absolute, not relative.
  54. 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.
  55. )
  56. if NOT "%par%"=="" goto loop
  57. echo Anime: %folder%
  58.  
  59. REM creating a vbs script, this is for Windows Powershell
  60. REM this script creates the shortcut on the desktop
  61. set SCRIPT="%RANDOM%-%RANDOM%-%RANDOM%-%RANDOM%.vbs"
  62. echo %SCRIPT%
  63.  
  64. echo Set oWS = WScript.CreateObject("WScript.Shell")>> %SCRIPT%
  65. echo sLinkFile = "%USERPROFILE%\Desktop\%folder%.lnk">> %SCRIPT%
  66. REM path to the new shortcut including name,
  67. REM %userprofile% is a hard coded symbol link in windows like %temp% or %appdata%
  68. echo Set oLink = oWS.CreateShortcut(sLinkFile)>> %SCRIPT%
  69. REM shortcut file is created at that point and attributes are changed below afterwards
  70. echo oLink.TargetPath = "%CD%\play-new.bat">> %SCRIPT%
  71. echo oLink.Arguments = "-sub">> %SCRIPT%
  72. REM adding arguments has to be seperated because the path it self needs the quotation marks to work,
  73. REM but the command won't add anything outside the quotation marks, I tried escaping but with no success. this is easier.
  74. echo oLink.WorkingDirectory = "%path%">> %SCRIPT%
  75. echo oLink.Save>> %SCRIPT%
  76. REM save and execute
  77. c:\windows\system32\cscript.exe /nologo "%SCRIPT%"
  78.  
  79. REM cleanup
  80. del %SCRIPT%
  81. del tmp1.txt
  82. del tmp2.txt
  83. del tmp3.txt
  84.  
  85.  
Add Comment
Please, Sign In to add comment