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

When using the /s switch (recurse subdirectories) with the DIR command, we get the full path of the files in the output:
However, without the /s switch, we only get the file names: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
Is there any way to get it to output the full file paths WITHOUT using the /s switch?dir "C:\folder\*" /b
file1.txt
file2.exe
file3.zip
file4.doc
file5.dll
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:
All I want is the original files in the main folder (without recursing subdirectories) and giving me the full file paths.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
Any way to do this?
Last edited by choz (11 Jan 2010 03:49)
----------------------------
#2 11 Jan 2010 09:21
bluesxman
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:
Code: Select all
@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
Last edited by bluesxman (11 Jan 2010 09:26)
cmd | *sh | ruby | chef
----------------------------
#3 11 Jan 2010 10:15
choz
Yep it's a bit of a workaround, but that's what I expected.
Your code works beautifully. Thanks for the help bluesxman

----------------------------
#4 11 Jan 2010 15:51
Drewfus
Hello choz. Is this what your after?...
Code: Select all
for /F %A in ('dir /b') do @echo %~dpnxA
#5 11 Jan 2010 16:00
bluesxman
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.Drewfus wrote:
Hello choz. Is this what your after?...
Code: Select all
for /F %A in ('dir /b') do @echo %~dpnxA
Fixing that bit is easy (and I have done below). The big problem comes about when you do something like this:
Code: Select all
@echo off
cd /d %temp%
for /F "tokens=*" %%A in ('dir /b c:\') do @echo %%~dpnxA
cmd | *sh | ruby | chef
----------------------------
#6 11 Jan 2010 16:27
Drewfus
Your right.
Slight alternative;
Code: Select all
pushd c:\ & (for /F "tokens=*" %%A in ('dir /A-D /B') do @echo %~dpnxA) & popd
#7 12 Jan 2010 01:03
choz
Thanks for the reply Drewfus

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.