Dump list of existing folder names in a directory to txt file

Greetings Experts!  After receiving very credible advice, I will be changing the directory structure of my file server slightly to better accomodate our login script and to address security concerns.  I am most familiar with batch files and would ask that you supply your answer with such.

Here is the objective.  I have an existing directory called "Departments" which contains sub-folders named after each department in our company (15 departments in all).  What I would like to do is run a script to dump the names of each folder into a text file so that I can create the folders with the same names in a new directory.  Your help is appreciated.

Not sure if it helps or not but the environment a mix of windows xp/2003 in an AD environment.
Who is Participating?
oBdAConnect With a Mentor Commented:
robocopy.exe can actually do this in one take.
The syntax is a bit different than with usual copy commands, you specify source folder, destination folder (no files in these arguments), and it will by default copy everything in that folder.
Other arguments you need are
/xf *.* - exclude all files (we only want the dirs)
/e - copy subfolders, even if empty
/lev:2 - the level of subfolders to copy (1 is the source folder itself)
You might want to check robocopy.doc in the ResKit installation folder, it's a powerful tool.

Windows Server 2003 Resource Kit Tools
robocopy.exe "O:\ld\Directory\Departments" "N:\ew\Directory\Departments" /xf *.* /e /lev:2

Open in new window

Hello - you can run the following from a run command to output all the folder names to a text file:

Go start, Run, type cmd - ENTER.

dir <drive/sharename>:\ >> c:\folderlist.txt

Example: dir c:\windows >> c:\folderlist.txt

This will output all the folders within the windows directory to a text file called folderlist.txt in your c:\ drive
Train for your Pen Testing Engineer Certification

Enroll today in this bundle of courses to gain experience in the logistics of pen testing, Linux fundamentals, vulnerability assessments, detecting live systems, and more! This series, valued at $3,000, is free for Premium members, Team Accounts, and Qualified Experts.

You can also use XCOPY with the /T and /E switches to copy a folder structure without copying any files.
samiam41Author Commented:
I like the xcopy option.  I ran the /? to dump the syntax commands but wasn't sure if there was a way to copy only the top level folders, not any of the subdirectories.  I also don't want to copy any data.

jamesEE- your option seemed to be the easiest but how do I create the folders once I have that txt file?
samiam41Author Commented:
I thought this would do just the top level folders.  Why am I getting the subdirectories?
c:\>xcopy /t \\server\folder \\server\newfolder

Open in new window

Hi again! You said you wanted to dump the folder names into a text file - well thats what the command I suggested will do for you. As for recreating them - it's gonna be a case of File, New, Folder...

Unless you go with samiam41's rather neat (I thought) suggestion to copy the folders without the content, and then delete any subdirectories you dont need. This would probably be your best option..
samiam41Author Commented:
jamesEE-  Thanks for the update.  Shift-3 came up with the xcopy suggestion and I am trying to perfect it.  The problem is that where I am copying the folders from, that directory has an enormous amount of subfolders which will be a pain in the @ss to go through.  I was hoping the /t option would do just the top level but no dice (or I am doing it wrong).

Also, can I use the mkdir option with the text file containing the folder names?
samiam41Author Commented:
Dang that is AWESOME!  Just finished that task in under a second.  
samiam41Author Commented:
You are unbelievable!  Seriously, you have to be some kind of prodigy.  Are you a contractor, just curious?  Thanks so much for the help, again!  Take care.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.