Get filenames from directory path or .txt file - Windows

34,709

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

Share:
34,709

Related videos on Youtube

Ben Allington
Author by

Ben Allington

Updated on September 18, 2022

Comments

  • Ben Allington
    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
      SimonS about 7 years
      in PowerShell, it would be: gci . | select FullName | out-file .\somefile.txt -force
    • Ben Allington
      Ben Allington about 7 years
      that just returns the top level of file names in need subfolders too
    • SimonS
      SimonS about 7 years
      then you just need to add -r to gci . so it's gci . -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 . in out-file
  • Ben Allington
    Ben Allington about 7 years
    would i just add dir to the start to run from current directory?
  • Andy
    Andy about 7 years
    it 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
    Ben Allington about 7 years
    dir(for /r %a in (*) do @echo %~nxa) >>test.txt just returns : Volume in drive C has no label, Directory of C:\......
  • Andy
    Andy about 7 years
    Remove 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
    Ben Allington about 7 years
    for /r %a in (*) do @echo %~nxa >>test.txt doesn't create the .txt file
  • Andy
    Andy about 7 years
    Is 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
    Ben Allington about 7 years
    Just ran it from my desktop and still wont create :(
  • Andy
    Andy about 7 years
    I 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
    Ben Allington about 7 years
    windows 10 is what im currently using
  • Andy
    Andy about 7 years
    Ah - 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
    Andy about 7 years
    One 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
    Ben Allington about 7 years
    Ahh ok ill try that now
  • Ben Allington
    Ben Allington about 7 years
    Still no joy, I may have to test on my computer at home as its running windows 7
  • Ben Allington
    Ben Allington about 7 years
    Am I able to make this into a .bat file? or something similar to be run on click?
  • Admin
    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
    SimonS about 7 years
    theres no need for the foreach, i'd rather go with my approach in the comments.
  • Admin
    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
    SimonS about 7 years
    I 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
    Admin about 7 years
    @SimonS Very well. I'll update my answer. I upvoted your comment.