Get filenames from directory path or .txt file - Windows
Solution 1
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 : https://technet.microsoft.com/en-us/library/cc755121(v=ws.11).aspx
Solution 2
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
Related videos on Youtube
Ben Allington
Updated on September 18, 2022Comments
-
Ben Allington over 1 year
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?
-
SimonS about 7 yearsin PowerShell, it would be:
gci . | select FullName | out-file .\somefile.txt -force
-
Ben Allington about 7 yearsthat just returns the top level of file names in need subfolders too
-
SimonS about 7 yearsthen you just need to add
-r
togci .
so it'sgci . -r | [...]
the-r
switch means recursive (btw: the.
means the current directory, you can also put a path in there instead of the.
- same for the.
inout-file
-
-
Ben Allington about 7 yearswould i just add dir to the start to run from current directory?
-
Andy about 7 yearsit should be ran, as is, from the top directory you want to search down from. Much as you did with your
dir
command originally. -
Ben Allington about 7 yearsdir(for /r %a in (*) do @echo %~nxa) >>test.txt just returns : Volume in drive C has no label, Directory of C:\......
-
Andy about 7 yearsRemove the dir command section. Its not necessary. I have updated the post with how to output it to a file. Just copy that into command line and run it. i.imgur.com/CLpLkS3.png
-
Ben Allington about 7 yearsfor /r %a in (*) do @echo %~nxa >>test.txt doesn't create the .txt file
-
Andy about 7 yearsIs it possible UAC is preventing creation of the file at the root of your C:\ drive? Have you tried running it in your user folder?
-
Ben Allington about 7 yearsJust ran it from my desktop and still wont create :(
-
Andy about 7 yearsI must admit I am confused. I have tried it on my own system and it worked. What version of Windows are you running?
-
Ben Allington about 7 yearswindows 10 is what im currently using
-
Andy about 7 yearsAh - then accept my apologies - I have tested this under Windows 7 and currently do not have a Windows 10 system to hand to test (office is W7 only).
-
Andy about 7 yearsOne note - is this in a batch file? If so you may need to double up the percentage signs:
for /r %%a in (*) do @echo %%~nxa >>FilesDirectoryList.txt
-
Ben Allington about 7 yearsAhh ok ill try that now
-
Ben Allington about 7 yearsStill no joy, I may have to test on my computer at home as its running windows 7
-
Ben Allington about 7 yearsAm I able to make this into a .bat file? or something similar to be run on click?
-
Admin about 7 years@BenAllington You can put into a .ps1 file. (These are more advanced than batch files.) Of course, if you are running a very old version of Windows PowerShell, you need to open it as an admin and issue the following command:
Set-ExecutionPolicy RemoteSigned
This commend needs to be run once in a computer's lifetime. -
SimonS about 7 yearstheres no need for the foreach, i'd rather go with my approach in the comments.
-
Admin about 7 years@SimonS Your approach in the comments is flawed. It does not exclude the folders as the OP requested. The OP wants "just the file names" not folder names. In addition, your approach adds a blank line, a "FullName" line and then a "--------" line.
-
SimonS about 7 yearsI don't see a problem with the fullname ----, that's just PowerShell standard. if you don't want that i'd do it like this
(gci . -r | ? {!$_.PsIsContainer}).Name | out-file .\xyz.txt -force
-
Admin about 7 years@SimonS Very well. I'll update my answer. I upvoted your comment.