SS64 Discussion Forum

You are not logged in.

#1 2010-01-10 21:47:19

choz
New Shoes
Registered: 2009-12-21
Posts: 7

DIR - getting it to display the full path without using the /s switch?

Small problem I'm having here, hoping there's a solution. smile


When using the /s switch (recurse subdirectories) with the DIR command, we get the full path of the files in the output:

dir "C:\folder\*" /s/b
C:\folder\file1.txt
C:\folder\file2.exe
C:\folder\file3.zip
C:\folder\file4.doc
C:\folder\file5.dll

However, without the /s switch, we only get the file names:

dir "C:\folder\*" /b
file1.txt
file2.exe
file3.zip
file4.doc
file5.dll

Is there any way to get it to output the full file paths WITHOUT using the /s switch?

The /s switch is not good when you only want the files in a certain directory. In my example above, if I were to have more directories in the "C:\folder\" directory the output would look something like this:

dir "C:\folder\*" /s/b
C:\folder\file1.txt
C:\folder\file2.exe
C:\folder\file3.zip
C:\folder\file4.doc
C:\folder\file5.dll
C:\folder\folder2\somefile.txt
C:\folder\folder2\somefile.exe
C:\folder\folder3\somefile.zip
C:\folder\folder3\somefile.doc
C:\folder\folder3\anotherfolder\anotherfile.dll

All I want is the original files in the main folder (without recursing subdirectories) and giving me the full file paths.

Any way to do this?

Last edited by choz (2010-01-10 21:49:34)

Offline

#2 2010-01-11 03:21:51

bluesxman
Sun Fire
From: Leeds, UK
Registered: 2006-12-29
Posts: 701

Re: DIR - getting it to display the full path without using the /s switch?

Yeah that's quite annoying and has caught me out once or twice. To my knowledge, there's no way to achieve the result using purely the DIR command.  I just use the indispensable FOR command:

@echo off

set "yourDir=C:\folder"

echo:List only files:
for %%a in ("%yourDir%\*") do echo %%~fa

echo:List only directories:
for /d %%a in ("%yourDir%\*") do echo %%~fa

echo:List directories and files in one command:
for /f "usebackq tokens=*" %%a in (`dir /b "%yourDir%\*"`) do echo %yourDir%\%%~a

pause

NB -- using "%%~fa" on the last command might seem like a better idea, but it doesn't seem to work in the way one would expect.

Last edited by bluesxman (2010-01-11 03:26:05)

Online

#3 2010-01-11 04:15:12

choz
New Shoes
Registered: 2009-12-21
Posts: 7

Re: DIR - getting it to display the full path without using the /s switch?

Yep it's a bit of a workaround, but that's what I expected.

Your code works beautifully. Thanks for the help bluesxman wink

Offline

#4 2010-01-11 09:51:49

Drewfus
8088
From: Australia
Registered: 2010-01-10
Posts: 31

Re: DIR - getting it to display the full path without using the /s switch?

Hello choz. Is this what your after?...

for /F %A in ('dir /b') do @echo %~dpnxA

Offline

#5 2010-01-11 10:00:24

bluesxman
Sun Fire
From: Leeds, UK
Registered: 2006-12-29
Posts: 701

Re: DIR - getting it to display the full path without using the /s switch?

Drewfus wrote:

Hello choz. Is this what your after?...

for /F %A in ('dir /b') do @echo %~dpnxA

That's (almost) fine if the directory you want to work against is the current one.  The "almost" refers to the fact that if there are spaces in any of the file names, you'll only see the name as far as the first space.

Fixing that bit is easy (and I have done below).  The big problem comes about when you do something like this:

@echo off
cd /d %temp%
for /F "tokens=*" %%A in ('dir /b c:\') do @echo %%~dpnxA

Try it and see.

Online

#6 2010-01-11 10:27:27

Drewfus
8088
From: Australia
Registered: 2010-01-10
Posts: 31

Re: DIR - getting it to display the full path without using the /s switch?

Your right.
Slight alternative;

pushd c:\ & (for /F "tokens=*" %%A in ('dir /A-D /B') do @echo %~dpnxA) & popd

Offline

#7 2010-01-11 19:03:00

choz
New Shoes
Registered: 2009-12-21
Posts: 7

Re: DIR - getting it to display the full path without using the /s switch?

Thanks for the reply Drewfus smile

The code bluesxman posted seems to be working for me so I will stick with that. But it's nice to know there are other solutions.

Offline

Board footer

Powered by FluxBB