Get filenames from directory path or .txt file - Windows

Currently I have a FileListGenerator.bat which looks like so:

dir /b /s >>FilesDirectoryList.txt

Returning a list of file directories looking like.

C:\Users\Ben\Desktop\Customers\Customer1
C:\Users\Ben\Desktop\Customers\Customer2
C:\Users\Ben\Desktop\Customers\FileListGenerator.bat
C:\Users\Ben\Desktop\Customers\FilesDirectoryList.txt
C:\Users\Ben\Desktop\Customers\Customer1\Analysys Mason
C:\Users\Ben\Desktop\Customers\Customer1\More
C:\Users\Ben\Desktop\Customers\Customer1\Other
C:\Users\Ben\Desktop\Customers\Customer1\Analysys Mason\Crook _ Hatchet blk white font.psd
C:\Users\Ben\Desktop\Customers\Customer1\More\Crook _ Hatchet lot.png
C:\Users\Ben\Desktop\Customers\Customer1\More\Crook _ Hatchet midd.png
C:\Users\Ben\Desktop\Customers\Customer1\Other\Crook _ Hatchet bigger.png
C:\Users\Ben\Desktop\Customers\Customer1\Other\Crook _ Hatchet botton final.png
C:\Users\Ben\Desktop\Customers\Customer2\Analysys Mason
C:\Users\Ben\Desktop\Customers\Customer2\More
C:\Users\Ben\Desktop\Customers\Customer2\Other
C:\Users\Ben\Desktop\Customers\Customer2\Analysys Mason\LiberalHand-Bld.otf
C:\Users\Ben\Desktop\Customers\Customer2\Analysys Mason\LiberalHand-Rg.ttf
C:\Users\Ben\Desktop\Customers\Customer2\Other\Crook _ Hatchet new font.png

Is there a way to either run a script against the .txt file or in command line to return just the file names?

3

3 Answers

Try this in command line:

for /r %a in (*) do @echo %~nxa >>FilesDirectoryList.txt

In batch file (need to double the percentage signs):

for /r %%a in (*) do @echo %%~nxa >>FilesDirectoryList.txt

Based on answer here

13

If you need to display ONLY a certain filetype like .txt, .doc, .dll, .exe ..etc, you can use dir command and adjust the parameters as you need.

Here is a simple example: Suppose I need to display a list of text files names only in a directory. I can use this command

dir *.txt /b

and it'll display something like this :

file1.txt

file2.txt

file3.txt

file4.txt

...etc

you can use it in a batch file as is, (something like this):

@Echo off
:: display a list of *.txt files
dir *.txt /b

or you can expand the code as you wish ( something like this):

@Echo off
:: save a list of *.txt files into another text file inside C:
dir *.txt /b > C:\results.txt

It depends on your goal and how you want to achieve it.

You can learn more about DIR command line from here : (v=ws.11).aspx

In Windows PowerShell, you can do this.

Just the file names with full path:

(gci -r | ? {!$_.PsIsContainer}).FullName | Out-File test.txt -Force

Just the file names showing the name and extension only:

(gci -r | ? {!$_.PsIsContainer}).Name | Out-File test.txt -Force
6

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

You Might Also Like