2

I want to store absolute path of all the files present under a folder into a text file, let's say temp.txt.

I am using this command for this task:

dir /s/b/a-d > D:\my work\temp.txt

The above command is working fine when I am redirecting to a file name written hard coded into the batch file.

But when I am using a variable as shown in below code, I am not getting output redirected to the file temp.txt.

@echo off
setlocal enabledelayedexpansion
REM I am running this script from D:\my work\
set origDir= %cd%
cd C:\
set temporaryFile="!origDir!\temp.txt"
dir /s/b/a-d > !temporaryFile!

Why is output of DIR not written into D:\my work\temp.txt?

Mofi
  • 38,783
  • 14
  • 62
  • 115
Jatin
  • 1,663
  • 4
  • 20
  • 33
  • 1
    your quotation is nor right, change `set origDir= %cd%` to `set "origDir=%cd%"` (note space behind equal sign) and `set temporaryFile="!origDir!\temp.txt"` to `set "temporaryFile=!origDir!\temp.txt"` – elzooilogico Nov 03 '17 at 12:26

2 Answers2

1

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.

Mofi
  • 38,783
  • 14
  • 62
  • 115
1

%0 is a special variable which refers to the running script. If you want to refer to the location which holds that script you can always do so at any time by using it's expanded form for drive and path %~dp0, which ends with a back slash, D:\my work\

With that in mind and regardless of your current directory you should safely be able to use this line within your script:

Dir/B/S/A-D C:\>"%~dp0temp.txt"

Notes, I've enclosed your output file name in double quotes to protect the included space. Because you are running the Dir command against your system drive it may also be optionally advised to exclude junctions and system files, Dir/B/S/A-D-L-S, from the output too.

Compo
  • 30,301
  • 4
  • 20
  • 32