Copy a file tree of all files on a hard drive?

I am looking for a batch command that saves a file tree of all files on a hard drive to a .txt document. I am going to put it on the autorun file so whenever I plug the usb drive in a list of all the files on a hard drive will copy.

sort by: active | newest | oldest
AndyGadget8 years ago
@echo off
if exist d:\filelist set usb=d:
if exist e:\filelist set usb=e:
if exist f:\filelist set usb=f:
if exist g:\filelist set usb=g:
if exist h:\filelist set usb=h:
if exist i:\filelist set usb=i:
if exist j:\filelist set usb=j:
echo.
echo About to print a file listing to USB drive %usb% for user %username%.
echo Press ctrl/c to quit.
pause

::This will give you a full listing of c:
dir c: /s /on >%usb%\filelist\%computername%.txt

echo.
echo Done.
echo

Go on then . . . do it! . . .do it ;¬)

Actually, what I wrote before is rubbish. In your final version you've put in the full path so don't need the OPFILE line at all, and it would have worked fine.
NachoMahma8 years ago
. IIRC, you can pipe (>) the output of the dir cmd to a text file. There is a param to only output filenames. Not sure how to recurse through the dirs, but there may be a param for dir that will do it automatically (a la xcopy). Google is your friend.
emuman4evr (author)  NachoMahma8 years ago
Uh care to elaborate with an example of its use?
dir /o /s > output.txt uses the directory command, with the /o (sort alphabetical) and the subdirectory command /s without /s you get the current working directory's files and folders. With it, you get everything. It might take a few minutes. NOTHING will show up on the command window, until its done - then it will return to the command prompt.
if it's taking too long, hit ctrl+c to break the operation The text file will appear in whatever directory you started in (start in the root to see all, or in a directory to see all in that directory). start>run cmd try this: C:\>dir /o /s ...it will output to screen a pile of stuff then C:\>dir /o /s > output.txt ...it will do exactly the same, but print to file, not screen use cd examplefolder... (may need to type it as examp~1 if it's longer than 8 characters or has spaces) C:\examp~1\ C:\examp~1\dir /o /s > outputex.txt C:\examp~1\ will now contain outputex.txt with just THIS folder's files and subdirectories.
emuman4evr (author)  frollard8 years ago
Oh I'm trying to do this inside a batch file that is run from the autorun.inf file on a usb drive.
emuman4evr (author)  emuman4evr8 years ago
This is my current code: @echo off echo dir/C /o /s > output.txt echo @echo but its saving the directory of the usb drive, not the C.
Nearly right. How about . . . @echo off dir c:\ /o /s > e:\output.txt This will do a full directory of C: starting at the top level and create the file on your USB drive (I'm assuming it's E:) at top level. You can change the pathnames / drive letters to suit.
It sounds as if you are planning to do this on several computers If so, how about getting a bit fancier and using . . . dir c:\ /o /s > e:\listings\%computername%.txt This will create a file named after each computer in a directory called 'listings' on your USB drive. (You'll have to create the directory first.)
emuman4evr (author)  AndyGadget8 years ago
Thank you, it worked. However the drive for the usb will probably change from computer to computer. Is there a value that automatically detects what volume letter the usb drive is? On my computer, it generated a 14 mb text file in about a minute. I can tell this process is quite lengthy.
Here we go with the de-luxe version . . .

This will detect which drive letter is assigned to the USB drive and then create a text file with the name of the PC in the directory 'filelist'. In the file will be a listing of the top level of the programs directory and the files in that user's documents directory.

It uses environment variables - type in 'SET |more' to see all the possibilities. Windows assigns these (but they can be changed).
It also uses 2 variables which I've defined, USB and OPFILE.

Also, be aware that '>' means output to a new file, deleting the old one of it already exists. '>>' means output to the end of a file (append).

The batch file looks daunting at first but work through it line by line and it should make sense. Copy and paste it to your USB stick, create the 'filelist' directory and modify it to your requirements. I'll be around if you have any questions.

@echo off
if exist d:\filelist set usb=d:
if exist e:\filelist set usb=e:
if exist f:\filelist set usb=f:
if exist g:\filelist set usb=g:
if exist h:\filelist set usb=h:

echo.
echo About to print a file listing to USB drive %usb% for user %username%.
echo Press ctrl/c to quit.
pause

::This would give you a full listing of c:
::dir c: /s /on >%usb%\filelist\%computername%.txt

::Turn the USB path listing into and environment variable to shorten it
set opfile=%usb%\filelist\%computername%.txt

::Output the username to file.
echo File listing for user %username% >%opfile%
echo. >>%opfile%

::This will list only the first level DIRECTORIES in C:\Program Files
dir "%programfiles%" /ad /on >>%opfile%

::This will list all the files in the users My Documents directory
dir "%userprofile%\my documents" /s /o >>%opfile%

echo.
echo Done.
echo.
 The section:
if exist d:\filelst set usb-d: ...
isn't working for me. Any ideas?
emuman4evr (author)  AndyGadget8 years ago
Thanks, your awesome. Here's the code I ended up using.
@echo off
if exist d:\filelist set usb=d:
if exist e:\filelist set usb=e:
if exist f:\filelist set usb=f:
if exist g:\filelist set usb=g:
if exist h:\filelist set usb=h:
if exist i:\filelist set usb=i:
if exist j:\filelist set usb=j:
echo.
echo About to print a file listing to USB drive %usb% for user %username%.
echo Press ctrl/c to quit.
pause

::This would give you a full listing of c:
dir c: /s /on >%usb%\filelist\%computername%.txt

::Turn the USB path listing into and environment variable to shorten it
set opfile=%usb%\filelist\%computername%.txt

echo.
echo Done.
echo
Everything's working fine.
So you are after a full listing - that's a lot of files!
There was actually a slight mistake in my (and your) code. It didn't show up in mine as the line was commented out but would in yours if you ran it on another computer. The 'dir c: /s . . . ' line should be AFTER the 'set opfile=...' line.
Yours would fail first time around but work the second as once the variable is set it stays set. Swap those over and everything will be fine.
emuman4evr (author)  AndyGadget8 years ago
I thought it something was odd about that. I'm not familiar with batch and you apparently are so I thought your code was correct. Your right, it did work the second time around. I'd select it as best answer but you posted it as a reply not an answer.
since it might take a while to do...putting a pause before the command would give you a chance to nix it if you insert accidentally - or a fancier menu system asking what you want to do... echo off cls pause dir .......
FuyuKitsune8 years ago
Most modern Windows have a tree command :D just add the /f option to copy files also under cmd- tree /f >filename.txt that's it :D
kelseymh8 years ago
I know how to do this (several ways) in Unix/Linux, but I have no idea if equivalent commands exist in DOS. Are you wanting to actually save a list of files, or do you just want to automatically copy a directory tree?

For the first, the Unix command would be ls -r <top-dir> and you can pipe the output to a text file. Does the DOS dir command have a "-r" option?

For the second, the Unix command would be cp -r <top-dir> <dest>. As before, the "-r" option recurses down the tree, copying everything and preserving the directory structure at the destination. Does the DOS copy command have a "-r" option?