CHDIR: Difference between revisions

From QB64 Phoenix Edition Wiki
Jump to navigation Jump to search
No edit summary
Tag: Reverted
No edit summary
Tags: Undo Reverted
Line 15: Line 15:
{{PageExamples}}
{{PageExamples}}
''Example 1:'' The following code is Windows-specific:
''Example 1:'' The following code is Windows-specific:
{{CodeStart}}
{{CodeStart}} '' ''
{{Cl|CHDIR}} "C:\"      'change to the root drive C (absolute path)
{{Cl|CHDIR}} "C:\"      'change to the root drive C (absolute path)
{{Cl|CHDIR}} "DOCUME~1" 'change to "C:\Documents and Settings" from root drive (relative path)
{{Cl|CHDIR}} "DOCUME~1" 'change to "C:\Documents and Settings" from root drive (relative path)  
{{Cl|CHDIR}} "..\"      'change back to previous folder one up
{{Cl|CHDIR}} "..\"      'change back to previous folder one up '' ''
{{CodeEnd}}
{{CodeEnd}}
:''Details:'' '''QB64''' can use long or short (8.3 notation) file and path names.
:''Details:'' '''QB64''' can use long or short (8.3 notation) file and path names.
Line 24: Line 24:


''Example 2:'' Using the Windows API to find the current program's name and root path. The PATH$ is a shared function value.
''Example 2:'' Using the Windows API to find the current program's name and root path. The PATH$ is a shared function value.
{{CodeStart}}
{{CodeStart}} '' ''
{{Cl|_TITLE}} "My program"
{{Cl|_TITLE}} "My program"
{{Cl|PRINT}} TITLE$
{{Cl|PRINT}} TITLE$
Line 49: Line 49:
{{Cl|ELSE}} TITLE$ = "": PATH$ = ""
{{Cl|ELSE}} TITLE$ = "": PATH$ = ""
{{Cl|END IF}}
{{Cl|END IF}}
{{Cl|END FUNCTION}}
{{Cl|END FUNCTION}} '' ''
{{CodeEnd}}
{{CodeEnd}}
: '''Note:''' The program's [[_TITLE]] name may be different from the actual program module's file name returned by Windows.
: '''Note:''' The program's [[_TITLE]] name may be different from the actual program module's file name returned by Windows.

Revision as of 17:56, 22 January 2023

The CHDIR statement changes the program's location from one working directory to another by specifying a literal or variable STRING path.


Syntax

CHDIR path$


Description

  • path$ is the new directory path the program will work in.
  • path$ can be an absolute path (starting from the root folder) or relative path (starting from the current program location).
  • If path$ specifies a non-existing path, a "Path not found" error will occur.
  • A QB64 SHELL statement cannot use "CD " or "CHDIR " + path$ to change directories.


Examples

Example 1: The following code is 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: QB64 can use long or short (8.3 notation) file and path names.


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.


See also



Navigation:
Main Page with Articles and Tutorials
Keyword Reference - Alphabetical
Keyword Reference - By usage
Report a broken link