Page 1 of 2

Path inclusion in Dir

Posted: 05 Oct 2011 22:55
by drgt
How to get the path included in the results for each file when executing a dir /b command?

Re: Path inclusion in Dir

Posted: 07 Oct 2011 15:10
by aGerman
Depends on how to use DIR /B. Do you execute it across the current working directory?

Regards
aGerman

Path inclusion in Dir

Posted: 07 Oct 2011 23:09
by drgt
Either way.

I know that if I include the /s switch, I get the path BUT I also get any subdirectories.

Re: Path inclusion in Dir

Posted: 07 Oct 2011 23:35
by trebor68
The command "DIR /b" will only have a result the file name.

With the command "DIR /b /s" will have any file name also the path name included.

Directly is this not possible what you want. But you can use the following code:

Code: Select all

for /f %a in ('dir /b') do echo %cd%\%a

Re: Path inclusion in Dir

Posted: 07 Oct 2011 23:58
by drgt
trebor68 wrote:The command "DIR /b" will only have a result the file name.

With the command "DIR /b /s" will have any file name also the path name included.

Directly is this not possible what you want. But you can use the following code:

Code: Select all

for /f %a in ('dir /b') do echo %cd%\%a



Thanks.

Can we get rid of the echo line and the blank line between each file?

Re: Path inclusion in Dir

Posted: 08 Oct 2011 08:39
by aGerman
Prepend @echo off to the code.

Regards
aGerman

Re: Path inclusion in Dir

Posted: 08 Oct 2011 10:02
by drgt
aGerman wrote:Prepend @echo off to the code.

Regards
aGerman


Thanks.
I noticed
@echo off and
echo off
do the same thing. Also the prompt disappears.

Can we include the echo command in the for line in such a way that when execution finishes the prompt comes back?

Re: Path inclusion in Dir

Posted: 08 Oct 2011 10:26
by aGerman
Write an echo on as next line.

Regards
aGerman

Re: Path inclusion in Dir

Posted: 08 Oct 2011 15:05
by trebor68
If you use the code at the prompt then use this code:

Code: Select all

for /f %a in ('dir /b') do @echo %cd%\%a


In a seperate batch file:

Code: Select all

@echo off
for /f %%a in ('dir /b') do echo %cd%\%%a


When you will save the files with complete path in a text file:

Code: Select all

for /f %a in ('dir /b') do echo %cd%\%a >TestFile.txt

Path inclusion in Dir

Posted: 09 Oct 2011 14:56
by drgt
Thank you!

That redirection to txt file though, only gives the last file.

Re: Path inclusion in Dir

Posted: 09 Oct 2011 16:30
by dbenham
You can switch to >> instead:

> = redirect output to a new file (overwrite any existing file)

>> = redirect output to a file - append to existing file or create file if it doesn't exist yet.

The append in a loop must repeatedly open and close the file. You can speed the process up by enclosing the entire loop in parentheses and redirecting so that it only opens and closes once:

Code: Select all

( for /f %%a in ('dir /b') do echo %cd%\%%a )>TestFile.txt


An alternative to using %cd% is to use the ~f modifier (includes full path)

Code: Select all

( for /f %%a in ('dir /b') do echo %%~fa )>TestFile.txt


You can get the same result using the simplest form of FOR without needing the DIR command:

Code: Select all

( for %%a in (*) do echo %%~fa )>TestFile.txt
One nice feature of this form is you can use multiple wildcard expressions in the same FOR loop. For example, this will capture all .TXT and .DOC files, but ignore other file extensions:

Code: Select all

( for %%a in (*.txt *.doc) do echo %%~fa )>TestFile.txt


Dave Benham

Path inclusion in Dir

Posted: 10 Oct 2011 01:23
by drgt
@Dave

Thanks, however these commands give the following error:

%%a was unexpected at this time.


I got the desired results by replacing %% with % and echo with @echo.


Is there a way to get the number of files at the bottom of testfile.txt?

Re: Path inclusion in Dir

Posted: 10 Oct 2011 05:22
by trebor68
For the prompt all in one row:

Code: Select all

(set num=0) & (for %a in (*) do @(echo %~fa)&(set/a num+=1 >nul)) & (echo !num! files)


For the batch file with writing in the file:

Code: Select all

@echo off
SETLOCAL ENABLEEXTENSIONS ENABLEDELAYEDEXPANSION
if exist Testfile.txt del Testfile.txt
set num=0
for %%a in (*) do (
  echo %%~fa >>Testfile.txt
  set/a num+=1
)
echo !num! files >>Testfile.txt

Re: Path inclusion in Dir

Posted: 10 Oct 2011 10:53
by drgt
trebor68 wrote:For the prompt all in one row:

Code: Select all

(set num=0) & (for %a in (*) do @(echo %~fa)&(set/a num+=1 >nul)) & (echo !num! files)




Thanks.
Can you include the redirection to testfile.txt and review the formula because now it displays
!num! files
instead of a number.

Re: Path inclusion in Dir

Posted: 11 Oct 2011 16:18
by trebor68
At the prompt and save result in a text file (all in one row):

Code: Select all

(set num=0) & (for %a in (*) do @(echo %~fa >>testfile.txt)&(set/a num+=1 >nul)) & (echo %num% files >>testfile.txt)

I will hope that is now all ok.
In Win XP at my systym will all working correct.

File format testfile.txt is:
path\file
path\file
2 files