T3RRYT3RR0R

Batch Relative Path AutoCorrect

Jan 13th, 2020
305
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Batch 2.64 KB | None | 0 0
  1. @ECHO OFF
  2.  
  3. ::: This approach to scripting a Batch Program that uses Sub-Programs is to Allow relative paths to remain Viable
  4. ::: even if the Program and Programs Folder do not share the same Directory.
  5. ::: It allows the main program / program launcher to be moved to any folder on the same drive without causing the Relative Paths
  6. ::: to be lost.
  7.  
  8. SETLOCAL ENABLEEXTENSIONS
  9.  
  10. ::: Assign Qualified Folder Path as the Programs Directory from the Current Working Directory.
  11.  
  12. Set "Prog_Dir=%~dp0"
  13.  
  14. ::: Assign Qualified Filepath to the Main Program
  15.  
  16. Set "This_Prog=%~0"
  17.  
  18. ::: Perform a test on a paired File to determine if the Main program is Located in the Program Folder
  19. ::: ReadMeYOURPROGRAMSNAME.bat does not need to contain data for this to work, However clear instructions not to Rename/delete or
  20. ::: remove the file won't hurt. It should be located in the top level of your Programs Folder, where the program is meant to be.
  21.  
  22. IF NOT EXIST "%Prog_Dir%ReadMeYOURPROGRAMSNAME.bat" (
  23. CALL :Path_Finder
  24. )
  25.  
  26. ::: Assign pathway for each file in your Programs Folder/s to a variable.
  27. ::: (Variable Name Is the Filename, Variable content is the fully qualified path to that file.)
  28. ::: To Call, Start, type or otherwise access any file in your Directory, expand the Filename: %SubprogramName%
  29. ::: To use this approach, all Filenames in all subdirectories must be Unique.
  30.  
  31. :Set_paths
  32.  
  33. FOR /R "%Prog_Dir%" %%a IN (*.*) DO (
  34. Set "%%~nxa=%%~dpa"
  35. )
  36.  
  37. ::: Your Programs Script Begins Here.
  38.  
  39. ::: END SCRIPT
  40. EXIT
  41.  
  42.  
  43. ::: Recursively search the users profile for a companion file to determine the filepath for the Programs Folder
  44. ::: If companion file is not found in Userprofile, Searches the Drive the Program is Located on.
  45. ::: Once found, the path for the Game Folders actual Location is Set to Prog_Dir and Script returns to Set_Paths
  46.  
  47. :Path_Finder
  48.  
  49. Set "readme=ReadMeYOURPROGRAMSNAME.bat"
  50.  
  51. ::: search the Current Users Profile for the Program Folder (Quick Search)
  52.  
  53. FOR /R "%USERPROFILE%" %%b IN (*.bat) DO (
  54. IF "%%~nxb"=="%readme%" (
  55.     Set "Prog_Dir=%%~dpb"
  56.     GOTO :Set_paths
  57.     )
  58. ) 2>nul
  59.  
  60. ::: assign Drive the Program is on to a Variable to search for the program Folder (Longer more Thorough Search)
  61. ::: Only Executes if Companion File not located in the Current Users Profile
  62.  
  63. Set "R_D=%~d0"
  64.  
  65. :Search_root
  66.  
  67. FOR /R "%R_D%" %%c IN (*.bat) DO (
  68. IF "%%~nxc"=="%readme%" (
  69.     Set "Prog_Dir=%%~dpc"
  70.     GOTO :Set_paths
  71.     )
  72. ) 2>nul
  73.  
  74. GOTO :EOF
  75.  
  76. ::: The below script will only execute if The Program and Program Folder are on different Drives.
  77.  
  78. ECHO Program Folder could not be located. The Program must Be on the same Drive as the Programs Folder
  79. pause
  80. exit
Add Comment
Please, Sign In to add comment