Path inclusion in Dir

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Message
Author
drgt
Posts: 160
Joined: 21 Sep 2010 02:22
Location: Greece

Path inclusion in Dir

#1 Post by drgt » 05 Oct 2011 22:55

How to get the path included in the results for each file when executing a dir /b command?

aGerman
Expert
Posts: 4678
Joined: 22 Jan 2010 18:01
Location: Germany

Re: Path inclusion in Dir

#2 Post by aGerman » 07 Oct 2011 15:10

Depends on how to use DIR /B. Do you execute it across the current working directory?

Regards
aGerman

drgt
Posts: 160
Joined: 21 Sep 2010 02:22
Location: Greece

Path inclusion in Dir

#3 Post by drgt » 07 Oct 2011 23:09

Either way.

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

trebor68
Posts: 146
Joined: 01 Jul 2011 08:47

Re: Path inclusion in Dir

#4 Post by trebor68 » 07 Oct 2011 23:35

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

drgt
Posts: 160
Joined: 21 Sep 2010 02:22
Location: Greece

Re: Path inclusion in Dir

#5 Post by drgt » 07 Oct 2011 23:58

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?

aGerman
Expert
Posts: 4678
Joined: 22 Jan 2010 18:01
Location: Germany

Re: Path inclusion in Dir

#6 Post by aGerman » 08 Oct 2011 08:39

Prepend @echo off to the code.

Regards
aGerman

drgt
Posts: 160
Joined: 21 Sep 2010 02:22
Location: Greece

Re: Path inclusion in Dir

#7 Post by drgt » 08 Oct 2011 10:02

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?

aGerman
Expert
Posts: 4678
Joined: 22 Jan 2010 18:01
Location: Germany

Re: Path inclusion in Dir

#8 Post by aGerman » 08 Oct 2011 10:26

Write an echo on as next line.

Regards
aGerman

trebor68
Posts: 146
Joined: 01 Jul 2011 08:47

Re: Path inclusion in Dir

#9 Post by trebor68 » 08 Oct 2011 15:05

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

drgt
Posts: 160
Joined: 21 Sep 2010 02:22
Location: Greece

Path inclusion in Dir

#10 Post by drgt » 09 Oct 2011 14:56

Thank you!

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

dbenham
Expert
Posts: 2461
Joined: 12 Feb 2011 21:02
Location: United States (east coast)

Re: Path inclusion in Dir

#11 Post by dbenham » 09 Oct 2011 16:30

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

drgt
Posts: 160
Joined: 21 Sep 2010 02:22
Location: Greece

Path inclusion in Dir

#12 Post by drgt » 10 Oct 2011 01:23

@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?

trebor68
Posts: 146
Joined: 01 Jul 2011 08:47

Re: Path inclusion in Dir

#13 Post by trebor68 » 10 Oct 2011 05:22

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

drgt
Posts: 160
Joined: 21 Sep 2010 02:22
Location: Greece

Re: Path inclusion in Dir

#14 Post by drgt » 10 Oct 2011 10:53

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.

trebor68
Posts: 146
Joined: 01 Jul 2011 08:47

Re: Path inclusion in Dir

#15 Post by trebor68 » 11 Oct 2011 16:18

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

Post Reply