How to not show file path with dir /b /s command in batch file
By Joseph Russell •
Is there Any way I can bypass displaying file paths when using the dir /b /s command into a file?
Currently, when I run dir /b /s C:\WinPE_amd64 >dir.txt it outputs something like this:
C:\WinPE_amd64\file.txt
C:\WinPE_amd64\secondfile.txt
C:\WinPE_amd64\Folder\AnotherFile.txtWhat I want is something like this:
file.txt
secondfile.txt
AnotherFile.txt 2 1 Answer
Parse dir output with a for /f and use the ~nx modifier to return only name+extension
On the cmd line:
(for /f "delims=" %A in ('dir /B/S "C:\WinPE_amd64"') do @Echo=%~nxA)>dir.txtIn a batch double the percent sign of the for metavariable
@Echo off
( for /f "delims=" %%A in ('dir /B/S "C:\WinPE_amd64"') do Echo=%%~nxA
) >dir.txtIt is a matter of formatting preference (or to avoid overly long lines)
@Echo off
( for /f "delims=" %%A in ( 'dir /B/S "C:\WinPE_amd64"' ) do Echo=%%~nxA
) >dir.txtEDIT a possibly faster alternative is a for /r
(For /r "C:\WinPE_amd64" %A in (*) Do @Echo=%~nxA)>dir.txtAnother alternative wrapping a powershell command
powershell -nop -c "(dir 'C:\WinPE_amd64' -r -file).Name" >dir.txt 3 More in general
"Zoraya ter Beek, age 29, just died by assisted suicide in the Netherlands. She was physically healthy, but psychologically depressed. It's an abomination that an entire society would actively facilitate, even encourage, someone ending their own life because they had no hope. Th…"