JohnN
JohnN

Reputation: 1010

Batch script to get a list of all files in multiple subdirectories

I'd like to create a batch script that will create a list of all files in a directory.

So far here's what I have:

@echo off

dir c:\users\documents\* /b > filelist.txt

This works perfectly with one small hitch. The files I want to get for the list are nestled in multiple user directories which are nestled inside two directories (A-L / M-Z). I can get around the two alphabetical directories easily by just writing two lines for this. However, I'm not sure how to get a list of the files inside of the sub-directories.

BONUS: This is an extra bit that I haven't actually researched yet but thought I'd throw it in there. I'm going to try and get this script to attach the "filelist.txt" to an email and send it out automatically.

EDIT: Forgot a much more important bonus problem.

Here's a really pitiful illustration of the file structure.

enter image description here

I'd like to be able to have a list that shows the parent directory to the files so that it would show up something like this in the filelist.txt: "A Name\File1.ext"

EDIT: Using dir /b/s seems to list all subdirectories, files and gives the full path length. Is there any way to only return a list when there is something in a particular one of the "Name directories"? So if everything but "D Name" is empty, it will only return

\D Name\file1.ext
\D Name\file2.ext

Upvotes: 4

Views: 27134

Answers (4)

meir e
meir e

Reputation: 1

Can try:

cd "root_dir"
dir /b /s 

On network can try:

pushd "root_dir"
dir /b /s 
popd "root_dir"

Upvotes: 0

Iury Kozlowsky
Iury Kozlowsky

Reputation: 11

Not sure if this is what you meant in the EDIT but try this

dir /A-D /S /B

Upvotes: 1

Stephan
Stephan

Reputation: 56180

dir has no code to remove a part of it's output. Therefore you have to parse its output with a for loop that does the removal:

@echo off
setlocal enabledelayedexpansion
pushd "c:\users\documents\"
(
  for /r %%a in (*) do (
    set x=%%a
    echo !x:%cd%=!
  )
)>filelist.txt
popd

for sending automatic mails, see our search function, which is well hidden at the top right corner of this site (please don't tell anyone or we will get much less duplicates ;)).

EDIT for I don't suppose there's anyway I can limit how far down in subdirectories this works?
where there is a will, there is a way...

@echo off
setlocal enabledelayedexpansion
set /p lv="max Levels deep [0...n]= "
set /a lv+=2
pushd "c:\users\theo\"
(
  for /r %%a in (*.bat) do (
    set x=%%a
    set x=!x:%cd%=!
    for /f "tokens=1,%lv% delims=\" %%b in ("!x!") do if "%%c"=="" echo !x!
  )
)>filelist.txt
popd

An input of "0" means "in this folder only, no subfolders"; "2" means "in this folder and its subfolders, max. depth = 2"

The inner for checks, if token "%lv%+2" (therefore set /a lv+=2) is empty. If yes, the depth is less or equal %lv% and the string is shown. If it is not empty, the depth is greater than %lv% - no echo.

Upvotes: 5

alroc
alroc

Reputation: 28174

To search all subdirectories recursively, use the /s switch for dir.

dir c:\users\documents\* /b/s > filelist.txt

But there's no way that I'm aware of to send an email from a batchfile without additional programs. If you're not married to the idea of using batchfiles, you can do this easily with PowerShell (and I recommend this method, as PowerShell is the way forward for Microsoft).

Get-ChildItem -File -Recurse | Select-Object -ExpandProperty FullName | Out-File -FilePath c:\filelist.txt;
Send-MailMessage -To YOURADDRESS -From YOURADDRESS -SmtpServer YOURSMTPHOST -Subject Filelist -Attachments c:\filelist.txt;

Upvotes: 4

Related Questions