You are not logged in.
Pages: 1
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:
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
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
pauseNB -- 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
Yep it's a bit of a workaround, but that's what I expected.
Your code works beautifully. Thanks for the help bluesxman ![]()
Offline
Hello choz. Is this what your after?...
for /F %A in ('dir /b') do @echo %~dpnxA
Offline
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 %%~dpnxATry it and see.
Online
Your right.
Slight alternative;
pushd c:\ & (for /F "tokens=*" %%A in ('dir /A-D /B') do @echo %~dpnxA) & popd
Offline
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.
Offline
Pages: 1