Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Batch file to list all folders in a directory and output to txt with

Tags:

batch-file

I am trying to create a batch file to parse a directory Z:\ (not including sub folders) which is a mapped network drive to give all folders that name includes "COMPANY_*" and output those folder names with the the full path to a text file.

The text file will be saved to a program folder location which is referenced when you run the program.

For example

Dir:

  • Z:\Company_001
  • Z:\def
  • Z:\Comapny_002
  • Z:\Company_101

Text file:

  • Z:\Company_001
  • Z:\Company_002
  • Z:\Company_101

I started to have a go but dont know what I am doing and need to run this batch on 10 computers so do not want to cause any problems.

dir "Z:\" /b >d:\test.txt
FOR /F "delims=" %%a in (test.txt) do @echo Z:\%%a>>output.txt
del "d:\test.txt"
start C:\Windows\System32\notepad.exe "d:\output.txt"
pause
like image 477
user1607914 Avatar asked Aug 17 '12 21:08

user1607914


People also ask

How do I export a folder list to a text file?

Right-click that folder and select Show more options. Click Copy File List to Clipboard on the classic menu. You'll still need to paste the copied list into a text file. Launch Run, type Notepad in the Open box, and click OK.

How do I get a list of folders in a folder?

You can use the DIR command by itself (just type “dir” at the Command Prompt) to list the files and folders in the current directory.

How do you get a list of all files in a directory and its subdirectories?

The ls command is used to list files or directories in Linux and other Unix-based operating systems. Just like you navigate in your File explorer or Finder with a GUI, the ls command allows you to list all files or directories in the current directory by default, and further interact with them via the command line.


2 Answers

cd Z:\
for /D %%A IN ("COMPANY_*") DO echo "Z:\%%A">>D:\output.txt

You can add a call to open the file with notepad if you want to. What you have now would work.

like image 89
Egor Avatar answered Nov 11 '22 18:11

Egor


It looks like you can get the folders that you want with no intermediate filtering:

dir Z:\Company_* /b /a:d >output.txt
like image 30
stark Avatar answered Nov 11 '22 16:11

stark