Show file names without paths

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
mrbob
Posts: 2
Joined: 29 Dec 2014 14:03

Show file names without paths

#1 Post by mrbob » 29 Dec 2014 14:07

I need to just list file names in a maze of subdirectories. so I tried using:

dir /b/s

with /b for bare file name and /s for subdirectories

but even with /b, this gives the file name plus path. How can I get just the file names?

Thank you for your help.

Mr. Bob

Squashman
Expert
Posts: 4488
Joined: 23 Dec 2011 13:59

Re: Show file names without paths

#2 Post by Squashman » 29 Dec 2014 14:47

From a command prompt. Double the percent signs if you run this from a batch file.

Code: Select all

for /F "delims=" %G in ('dir /a-d /b /s') do echo %~nxG

mrbob
Posts: 2
Joined: 29 Dec 2014 14:03

Re: Show file names without paths

#3 Post by mrbob » 29 Dec 2014 15:40

Squashman,

Thank you! It's all gibberish to me, but it does what I needed at the command line.

Should this work in a batch file?

Code: Select all

@Echo off
cd  c:\directory1
for /F "delims=" %%G in ('dir /a-d /b /s') do echo %%~nxG > C:\Users/MrBob\Documents\files.txt
cd  c:\directory2
for /F "delims=" %%G in ('dir /a-d /b /s') do echo %%~nxG >> C:\Users\MrBob\Documents\files.txt


Mr Bob

Squashman
Expert
Posts: 4488
Joined: 23 Dec 2011 13:59

Re: Show file names without paths

#4 Post by Squashman » 29 Dec 2014 16:00

Just put the directory name in with the DIR command.

Code: Select all

@Echo off
for /F "delims=" %%G in ('dir c:\directory1\ /a-d /b /s') do echo %%~nxG > C:\Users/MrBob\Documents\files.txt
for /F "delims=" %%G in ('dir c:\directory2\ /a-d /b /s') do echo %%~nxG >> C:\Users\MrBob\Documents\files.txt

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Show file names without paths

#5 Post by foxidrive » 30 Dec 2014 00:17

Just adding another option.

Code: Select all

@echo off
(
   for /r "c:\directory1" %%a in (*) do echo %%~nxa
   for /r "c:\directory2" %%a in (*) do echo %%~nxa

)>"%userprofile%\Documents\files.txt"

Post Reply