#1 11 Jan 2010 03:47

choz
Member
Registered: 22 Dec 2009
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 (11 Jan 2010 03:49)

Offline

#2 11 Jan 2010 09:21

bluesxman
Member
From: UK
Registered: 29 Dec 2006
Posts: 1,039

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 (11 Jan 2010 09:26)


cmd | *sh | Ruby | Chef | Puppet | PowerShell | AutoIT3

Offline

#3 11 Jan 2010 10:15

choz
Member
Registered: 22 Dec 2009
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 11 Jan 2010 15:51

Drewfus
Member
From: Australia
Registered: 11 Jan 2010
Posts: 44

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 11 Jan 2010 16:00

bluesxman
Member
From: UK
Registered: 29 Dec 2006
Posts: 1,039

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.


cmd | *sh | Ruby | Chef | Puppet | PowerShell | AutoIT3

Offline

#6 11 Jan 2010 16:27

Drewfus
Member
From: Australia
Registered: 11 Jan 2010
Posts: 44

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 12 Jan 2010 01:03

choz
Member
Registered: 22 Dec 2009
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