The answer on Why is no string output with 'echo %var%' after using 'set var = text' on command line? what is wrong on command line:
set origDir= %cd%
The space after the equal sign is not ignored by command SET. The string value of the environment variable origDir
is therefore defined with a leading space character.
Next the command line
set temporaryFile="!origDir!\temp.txt"
results in your case in string " D:\my work\temp.txt"
with the double quotes being assigned to environment variable temporaryFile
.
So the file name with path enclosed in double quotes is invalid because of beginning with a space character.
A possible code for your task is:
@echo off
setlocal EnableExtensions DisableDelayedExpansion
set "origDir=%CD%"
rem Current directory path usually does not end with a backslash. But on
rem current directory being root of a drive it ends with a backslash which
rem should be removed to correct concatenate it with backslash and file name.
if "%origDir:~-1%" == "\" set "origDir=%origDir:~0,-1%"
set "temporaryFile=%origDir%\temp.txt"
cd /D C:\
dir /A-D /B /S >"%temporaryFile%"
endlocal
The usage of delayed environment variable expansion is not needed here.
The command CD changes the current directory to specified directory on a different drive than current drive only if parameter /D
is also used and the specified directory is not on a network share being referenced with UNC path.
The command PUSHD with enabled command extensions works also for UNC paths and with POPD the initial current directory can be restored.
Well, the command ENDLOCAL restores in batch file above also the initial current directory. This can be seen on running this batch file from within a command prompt window with D:\my work
being current directory. echo %CD%
executed after running the batch file outputs D:\my work
although the batch file changes to root directory of drive C:
. Read this answer for details about the commands SETLOCAL and ENDLOCAL.
Also possible but not recommended:
@echo off
setlocal EnableExtensions DisableDelayedExpansion
set "origDir=%CD%"
if "%origDir:~-1%" == "\" set "origDir=%origDir:~0,-1%"
set "temporaryFile="%origDir%\temp.txt""
cd /D C:\
dir /A-D /B /S >%temporaryFile%
endlocal
The environment variable temporaryFile
holds the file name with full path already enclosed in double quotes and therefore the DIR command line is executed after preprocessing by Windows command interpreter as:
dir /A-D /B /S 1>"D:\my work\temp.txt"
In general it is not advisable to assign a folder or file name already enclosed in double quotes to an environment variable because this can easily result in unexpected behavior if the variable value is concatenated later in batch code with other strings, see for example How to set environment variables with spaces?
But the shortest version for this task is the single command line:
@dir C:\* /A-D /B /S >temp.txt
That single command line does exactly the same as the two batch files with all those commands above. It creates in current directory D:\my work
a file temp.txt
with the names of all files on entire drive C:
with full path for which the current user account has permissions for listing directory entries.
For understanding the used commands and how they work, open a command prompt window, execute there the following commands, and read entirely all help pages displayed for each command very carefully.
cd /?
dir /?
echo /?
endlocal /?
if /?
rem /?
set /?
setlocal /?
Read also the Microsoft article about Using Command Redirection Operators.