CWD$: Difference between revisions

From QB64 Phoenix Edition Wiki
Jump to navigation Jump to search
(Created page with "{{DISPLAYTITLE:_CWD$}} The _CWD$ function returns the current working directory path as a string value without a trailing path separator. {{PageSyntax}} : {{Parameter|workingDirectory$}} = _CWD$ {{PageDescription}} * By default, the initial working directory path is usually the same as the directory of the executable file run. * The current working directory can be changed with the CHDIR or SHELL command; CHDIR sets it, _CWD$ returns it. * Path retu...")
 
No edit summary
 
(9 intermediate revisions by the same user not shown)
Line 1: Line 1:
{{DISPLAYTITLE:_CWD$}}
{{DISPLAYTITLE:_CWD$}}


The [[_CWD$]] function returns the current working directory path as a string value without a trailing path separator.
The [[_CWD$]] function returns the current working directory path as a string value without a trailing path separator (see also '''Availability''' below).




{{PageSyntax}}  
{{PageSyntax}}
: {{Parameter|workingDirectory$}} = [[_CWD$]]
: {{Parameter|workingDirectory$}} = [[_CWD$]]


Line 14: Line 14:
* The current working directory string can be used in [[OPEN]] statements and [[SHELL]] commands that deal with files.
* The current working directory string can be used in [[OPEN]] statements and [[SHELL]] commands that deal with files.
* Works in Windows, macOS and Linux. [[_OS$]] can be used by a program to predict the proper slash separations in different OS's.
* Works in Windows, macOS and Linux. [[_OS$]] can be used by a program to predict the proper slash separations in different OS's.
=== Errors ===
* If an error occurs while obtaining the working directory from the operating system, [[ERROR Codes|error code]] 51 (Internal Error) will be generated.




{{PageErrors}}
{{PageAvailability}}
* If an error occurs while obtaining the working directory from the operating system, [[ERROR Codes|error code]] 51 (Internal Error) will be generated.
<!-- QB64 = a version or none, QBPE = a version or all, Platforms = yes or no -->
<gallery widths="48px" heights="48px" mode="nolines">
File:Qb64.png|'''v1.0'''
File:Qbpe.png|'''all'''
File:Apix.png
File:Win.png|'''yes'''
File:Lnx.png|'''yes'''
File:Osx.png|'''yes'''
</gallery>
<!-- additional availability notes go below here -->
* Since '''QB64-PE v4.1.0''' the path is always returned '''with''' a trailing path separator, hence you don't need to add it yourself anymore. The change was implemented to be in line with [[_DIR$]] and [[_FULLPATH$]].
** Old code, which is adding a separator, still works as all supported platforms were tested and proved they doesn't care about multiple consecutive path separators.




{{PageExamples}}
{{PageExamples}}
''Example:'' Get the current working directory, and move around the file system:
''Example:'' Get the current working directory, and move around the file system:
{{CodeStart}} '' ''
{{CodeStart}}
startdir$ = _CWD$
startdir$ = {{Cl|_CWD$}}
{{Cl|PRINT}} "We started at "; startdir$
{{Cl|PRINT}} {{Text|<nowiki>"We started at "</nowiki>|#FFB100}}; startdir$
{{Cl|MKDIR}} "a_temporary_dir"
{{Cl|MKDIR}} {{Text|<nowiki>"a_temporary_dir"</nowiki>|#FFB100}}
{{Cl|CHDIR}} "a_temporary_dir"
{{Cl|CHDIR}} {{Text|<nowiki>"a_temporary_dir"</nowiki>|#FFB100}}
{{Cl|PRINT}} "We are now in "; _CWD$
{{Cl|PRINT}} {{Text|<nowiki>"We are now in "</nowiki>|#FFB100}}; {{Cl|_CWD$}}
{{Cl|CHDIR}} startdir$
{{Cl|CHDIR}} startdir$
{{Cl|PRINT}} "And now we're back in "; _CWD$
{{Cl|PRINT}} {{Text|<nowiki>"And now we're back in "</nowiki>|#FFB100}}; {{Cl|_CWD$}}
{{Cl|RMDIR}} "a_temporary_dir"
{{Cl|RMDIR}} {{Text|<nowiki>"a_temporary_dir"</nowiki>|#FFB100}}
{{CodeEnd}}
{{CodeEnd}}
 
{{OutputStart}}
{{OutputStart}}We started at C:\QB64
We started at C:\QB64
We are now in C:\QB64\a_temporary_dir
We are now in C:\QB64\a_temporary_dir
And now we're back in C:\QB64
And now we're back in C:\QB64
Line 40: Line 54:


{{PageSeeAlso}}
{{PageSeeAlso}}
* [[CHDIR]] {{text|(Change the current working directory)}}
* [[CHDIR]] {{Text|(Change the current working directory)}}
* [[RMDIR]] {{text|(Remove a directory in the file system)}}
* [[RMDIR]] {{Text|(Remove a directory in the file system)}}
* [[KILL]] {{text|(Delete a file in the file system)}}
* [[KILL]] {{Text|(Delete a file in the file system)}}
* [[MKDIR]] {{text|(Create a directory in the file system)}}
* [[MKDIR]] {{Text|(Create a directory in the file system)}}
* [[_OS$]] {{text|(returns current OS to program)}}
* [[_OS$]] {{Text|(returns current OS to program)}}
* [[_STARTDIR$]] {{text|(returns path the user called program from)}}  
* [[_STARTDIR$]] {{Text|(returns path the user called program from)}}




{{PageNavigation}}
{{PageNavigation}}

Latest revision as of 23:50, 25 January 2025


The _CWD$ function returns the current working directory path as a string value without a trailing path separator (see also Availability below).


Syntax

workingDirectory$ = _CWD$


Description

  • By default, the initial working directory path is usually the same as the directory of the executable file run.
  • The current working directory can be changed with the CHDIR or SHELL command; CHDIR sets it, _CWD$ returns it.
  • Path returns will change only when the working path has changed. When in C:\ and run QB64\cwd.exe, it will still return C:\
  • The current working directory string can be used in OPEN statements and SHELL commands that deal with files.
  • Works in Windows, macOS and Linux. _OS$ can be used by a program to predict the proper slash separations in different OS's.

Errors

  • If an error occurs while obtaining the working directory from the operating system, error code 51 (Internal Error) will be generated.


Availability

  • Since QB64-PE v4.1.0 the path is always returned with a trailing path separator, hence you don't need to add it yourself anymore. The change was implemented to be in line with _DIR$ and _FULLPATH$.
    • Old code, which is adding a separator, still works as all supported platforms were tested and proved they doesn't care about multiple consecutive path separators.


Examples

Example: Get the current working directory, and move around the file system:

startdir$ = _CWD$
PRINT "We started at "; startdir$
MKDIR "a_temporary_dir"
CHDIR "a_temporary_dir"
PRINT "We are now in "; _CWD$
CHDIR startdir$
PRINT "And now we're back in "; _CWD$
RMDIR "a_temporary_dir"
We started at C:\QB64
We are now in C:\QB64\a_temporary_dir
And now we're back in C:\QB64


See also

  • CHDIR (Change the current working directory)
  • RMDIR (Remove a directory in the file system)
  • KILL (Delete a file in the file system)
  • MKDIR (Create a directory in the file system)
  • _OS$ (returns current OS to program)
  • _STARTDIR$ (returns path the user called program from)



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