Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- @ECHO OFF
- ::: This approach to scripting a Batch Program that uses Sub-Programs is to Allow relative paths to remain Viable
- ::: even if the Program and Programs Folder do not share the same Directory.
- ::: It allows the main program / program launcher to be moved to any folder on the same drive without causing the Relative Paths
- ::: to be lost.
- SETLOCAL ENABLEEXTENSIONS
- ::: Assign Qualified Folder Path as the Programs Directory from the Current Working Directory.
- Set "Prog_Dir=%~dp0"
- ::: Assign Qualified Filepath to the Main Program
- Set "This_Prog=%~0"
- ::: Perform a test on a paired File to determine if the Main program is Located in the Program Folder
- ::: ReadMeYOURPROGRAMSNAME.bat does not need to contain data for this to work, However clear instructions not to Rename/delete or
- ::: 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.
- IF NOT EXIST "%Prog_Dir%ReadMeYOURPROGRAMSNAME.bat" (
- CALL :Path_Finder
- )
- ::: Assign pathway for each file in your Programs Folder/s to a variable.
- ::: (Variable Name Is the Filename, Variable content is the fully qualified path to that file.)
- ::: To Call, Start, type or otherwise access any file in your Directory, expand the Filename: %SubprogramName%
- ::: To use this approach, all Filenames in all subdirectories must be Unique.
- :Set_paths
- FOR /R "%Prog_Dir%" %%a IN (*.*) DO (
- Set "%%~nxa=%%~dpa"
- )
- ::: Your Programs Script Begins Here.
- ::: END SCRIPT
- EXIT
- ::: Recursively search the users profile for a companion file to determine the filepath for the Programs Folder
- ::: If companion file is not found in Userprofile, Searches the Drive the Program is Located on.
- ::: Once found, the path for the Game Folders actual Location is Set to Prog_Dir and Script returns to Set_Paths
- :Path_Finder
- Set "readme=ReadMeYOURPROGRAMSNAME.bat"
- ::: search the Current Users Profile for the Program Folder (Quick Search)
- FOR /R "%USERPROFILE%" %%b IN (*.bat) DO (
- IF "%%~nxb"=="%readme%" (
- Set "Prog_Dir=%%~dpb"
- GOTO :Set_paths
- )
- ) 2>nul
- ::: assign Drive the Program is on to a Variable to search for the program Folder (Longer more Thorough Search)
- ::: Only Executes if Companion File not located in the Current Users Profile
- Set "R_D=%~d0"
- :Search_root
- FOR /R "%R_D%" %%c IN (*.bat) DO (
- IF "%%~nxc"=="%readme%" (
- Set "Prog_Dir=%%~dpc"
- GOTO :Set_paths
- )
- ) 2>nul
- GOTO :EOF
- ::: The below script will only execute if The Program and Program Folder are on different Drives.
- ECHO Program Folder could not be located. The Program must Be on the same Drive as the Programs Folder
- pause
- exit
Add Comment
Please, Sign In to add comment