Programming Field

Current directories in MS-DOS/Windows - DOS/Command Prompt Reference

The current directory is information about the directory primarily used to enable the omission of file path names, allowing for easier referencing of files. The typical process involves setting the directory containing the desired file (or subdirectory) as the ‘current directory’, and accessing and manipulating the files within that directory using the application associated with the current directory.

The term ‘working directory’ is also used interchangeably with ‘current directory’, stemming from the idea of performing operations based on the current directory.

* This page explains the concept of the current directory in MS-DOS and Windows. While Unix-based systems also have a concept of the current directory, some explanations on this page may be specific to MS-DOS/Windows. Please be aware of these distinctions.

Current directory in OS

The mechanism of the current directory is supported at the OS level. The information for the ‘current directory’ is provided for each running application, and it can be obtained or modified using APIs or environment variables. When one application launches another application (the launching program is referred to here as the ‘parent program’ and the launched program as the ‘child program’), the current directory of the parent program at that moment is set as the current directory for the child program.

* Generally, it is not possible to change the current directory of a different application (application space). Therefore, if the child program changes its current directory, it does not affect the current directory of the parent program. Note that when command.com or cmd.exe executes a batch file and that batch file executes another batch file using Call, it does not launch a separate process for the child batch file; instead, the second batch file is executed within the same application space. Therefore, changes to the current directory in the second batch file will affect the original batch file as well.
* When launching a child program, the parent program has the option to explicitly specify the current directory for the child program. In such cases, the current directory of the child program may not necessarily match the current directory of the parent program.

Within the operating system, there are some functions that make use of the current directory. For example, in certain functions within the Win32API, such as the CreateProcess function (commonly used to launch child processes), the SearchPath function (used to find the full path of a file), and various file operation functions (like CreateFile), the current directory might be used as the search or operation target when the specified file name does not include a directory path.

Additionally, when dealing with files or directories other than launching a child program, whether to use files within the current directory depends on the implementation of the application. Handling files without directory paths may result in the application accessing files in the directory where the executed program resides, rather than the current directory. It's advisable to check the documentation for each application for clarification. In cases where there is no specific mention, it's often assumed that the application is leveraging OS-level support, and using a file name without a directory path would refer to the current directory.

Current drives and current directories

Sample of current directory

In MS-DOS and Windows, the current directory exists independently for each drive on the system. In the MS-DOS system, there is a concept of a ‘current drive’, and to change the current directory, including the drive, it is necessary to also change the current drive. (Using INT 21H AH=0EH to set, or AH=19H to get)

In Windows (NT system), due to the existence of paths like network directories where drives may not be present, there is no concept of a current drive; instead, there is a single ‘current directory’ that encompasses all drives, separate from the current directory for each individual drive. However, in consideration of compatibility with MS-DOS, some use the concept of the ‘current drive’ as functions within the C runtime library or VB. These implementations handle the corresponding drive within the ‘single current directory’, and its processing is as follows:

  • Retrieve the current drive → Return the drive portion of the current directory (if the current directory is a network directory or similar, return a message indicating that it cannot be recognized)
  • Set the current drive → Set the ‘current directory for each drive’ as the current directory.

To handle the ‘current directory for each drive’ in Windows, the system maintains environment variables with names in the form of ‘=X:’ (where X is the drive letter), which correspond to the current directory for the respective drives.

* In Windows, to change only the ‘current drive’, you can call SetCurrentDirectory function by specifying a string in the form of ‘X:’ as the argument, where X is the drive letter, containing only the drive letter and a colon. As the result, the ‘single current directory’ will be changed to the ‘(current directory for each drive)’ of the specified drive.

In the MS-DOS prompt or Command Prompt, combining the concepts of the ‘current drive’ and the ‘(current directory for each) drive’, similar to the approach in MS-DOS, is adopted. On the prompt, you can directly input ‘X:’ (where X is the drive letter) to change the current drive, and you can use the Cd or Chdir command to modify the current directory for each drive. Additionally, to obtain the current directory corresponding to each drive (including the current drive), you can use Cd (Chdir). While there isn't a direct method to retrieve the current drive, if you call Cd (Chdir) without specifying options, the path displayed at that time will include the current drive in the current directory path.

[Windows NT series] The current directory is affected by the localization of the environment using Setlocal. After executing Setlocal and changing the current directory, ending the local environment (with Endlocal or ending the batch file) will revert the current directory to its state before localization.

[Windows NT series][Extensions] If Extensions are enabled (see Setlocal) in Command Prompt, you can use the environment variable ‘%CD%’ to retrieve the value of the current directory. Since %CD% can be used as a regular environment variable for read, you can use ‘%CD:~0,1%’ to obtain the drive letter portion of the current directory (i.e. the current drive). However, setting a value to the environment variable ‘CD’ using Set command does not change the current directory.

Samples

Sample 1

cd MyDocs\Addrs
asearch.exe result.log

Moves the current directory to ‘MyDocs\Addrs’ and executes ‘asearch.exe result.log’. The program ‘asearch.exe’ is executed in the changed current directory; therefore, if the program deals with data in the current directory, it will operate on the data within ‘MyDocs\Addrs’ and its subdirectories.

* In the above the path passed to Cd is a relative path, resulting that, in this syntax, if the subdirectory ‘MyDocs\Addrs’ does not exist in the current directory before the change, the attempt to change the current directory will fail.

Sample 2 (Batch file)

@echo off

call :enum_all_drives
exit /b 0

:enum_all_drives
    call :enum_drives_iter A B C D E F G H I J K L M N O P Q R S T U V W X Y Z
    exit /b 0

:enum_drives_iter
    if "%1"=="" exit /b 0
    cd %1: >NUL 2>NUL
    if not errorlevel 1 echo %1
    shift
    goto enum_drives_iter

[Windows NT series][Extensions] This code outputs each drive letter available on the current system, one per line. In this batch file, Cd(Chdir) command is used to check availability for the ‘current directory of the specified drive’. Specifically, using Cd command in the form of ‘cd X:’ outputs the current directory when the drive exists, and if the drive does not exist, it returns an error message and a non-zero exit code. Therefore, using output redirection ‘>’ to suppress all output to the screen and employing ‘if errorlevel’ to evaluate the remaining exit code allows for determining whether the specified drive exists or not. If the exit code is 0, the subsequent Echo command following If statement will be executed, and the drive letter will be output to the screen (standard output).

* By changing the ‘echo %1’ part, you can execute processing for all existing drives (with the drive letter being replaced by %1). Although it's a bit of a detour, you can also adopt the approach of parsing the output of this batch file (let's call it enum_drives.bat) using ‘for /F’ as follows (Using the for command allows you to consolidate the drive letter enumeration process into a single batch file for reuse).

for /F "delims=" %D in ('enum_drives.bat') do foo.exe %D

See also