From QB64 Wiki
The CHDIR statement changes the program's location from one working directory to another by specifying a literal or variable STRING path.
- CHDIR path$
- Path directory names must use the 8 letter maximum DOS name in Qbasic. QB64 can use long path names.
- path$ is the new directory path the program will work in.
- path$ can be an absolute(starting from root drive) or relative(starting from the present program location) path.
- If path$ specifies a non-existing path, a "Path not found" error will occur.
- A QB64 SHELL statement cannot currently use "CD " or "CHDIR " + path$ to change the directory using DOS.
- WARNING: The new program path location must have the relevant files the program requires to run properly!
Example 1: The following code is MS DOS and Windows-specific:
CHDIR "C:\" 'change to the root drive C (absolute path) CHDIR "DOCUME~1" 'change to "C:\Documents and Settings" from root drive (relative path) CHDIR "..\" 'change back to previous folder one up
- Details: There is an 8 letter path folder name limit in Qbasic. For folder names longer than eight characters use the first 6 letters(remove spaces) with a tilde(~) and a number(normally 1). QB64 can use long or short(8.3 notation) file and path names! Remember that the program location has actually been moved so files in the original location must be accessed using a path.
Example 2: Using the Windows API to find the current program's name and root path. The PATH$ is a shared function value.
_TITLE "My program" PRINT TITLE$ PRINT PATH$ FUNCTION TITLE$ === SHOW CURRENT PROGRAM SHARED PATH$ 'optional path information shared with main module only DECLARE LIBRARY 'Directory Information using KERNEL32 provided by Dav FUNCTION GetModuleFileNameA (BYVAL Module AS LONG, FileName AS STRING, BYVAL nSize AS LONG) END DECLARE FileName$ = SPACE$(256) Result = GetModuleFileNameA(0, FileName$, LEN(FileName$)) '0 designates the current program IF Result THEN 'Result returns the length or bytes of the string information PATH$ = LEFT$(FileName$, Result) start = 1 DO posit = INSTR(start, PATH$, "\") IF posit THEN last = posit start = posit + 1 LOOP UNTIL posit = 0 TITLE$ = MID$(PATH$, last + 1) PATH$ = LEFT$(PATH$, last) ELSE TITLE$ = "": PATH$ = "" END IF END FUNCTION
- Note: The program's _TITLE name may be different from the actual program module's file name returned by Windows!