command line list files in subfolders but not current folder

1

1

I have a batch file that gives me a directory listing of all files in current and subfolders. I would like to change this to list all files that are in subfolders but NOT the current folder. Currently I am using this command below which successfully works to list all the files in the current folder AND subfolders.

cd "c:\temp"
dir  /s/b *.doc>c:\temp\mylist.txt

This produces results as follows:

c:\temp\test8.doc
c:\temp\test9 (2).doc
c:\temp\test9.doc
c:\temp\012015\blah_012340.doc
c:\temp\032016\blah_124756.doc
c:\temp\042016\blah_125230.doc
c:\temp\052016\blah_052647.doc

I would like my results to not show what is directly in c:\temp, so my desired results would be:

c:\temp\012015\blah_012340.doc
c:\temp\032016\blah_124756.doc
c:\temp\042016\blah_125230.doc
c:\temp\052016\blah_052647.doc

Jose

Posted 2016-11-08T22:44:08.200

Reputation: 13

Why don't you use tree /f for better visual quality? – None – 2016-11-08T22:49:44.667

I'm not an expert on PowerShell but I think you can do it easily on PowerShell using -include and -exclude switches of dir (alias). – None – 2016-11-08T22:51:22.870

I don't think tree wil work as I need the list dumped into a list, which I then use for my next function in the batch file. Powershell may work but I don't know powershell either. I guess I can try and find some examples that might work and go from there. – Jose – 2016-11-08T23:23:36.897

Answers

0

this should work

for /f "delims=" %5  in ('dir /a:d /b') do dir /b /s  "%5"

Here's the breakdown for this one liner.

for /f is used to loop a list of items.

"delims=" sets the delimiters to none so it treats each line as a single phrase. By default spaces will be treated as a delimiter.

%5 is the variable.

in ('dir /a:d /b') this gets the list of directories of the target folder.

do dir /b /s "%5" here you define your action do followed by the command and then the variable %5 as an argument.

Please note that when you run this from a batch file you must use %%5

for /f "delims=" %%5 in ('dir /a:d /b') do dir /b /s "%%5"

Hope this helps.

Thank you

Zalmy

Posted 2016-11-08T22:44:08.200

Reputation: 821

Looks like this will work as soon as I figure out what I am messing up. I am not too sure if the %5 was something I am supposed to modify so I left as is. I added the code you listed to my batch.

The results that it outputted did exclude the files that were in the "root" directory that I am searching. However it only listed one sub-directory, and in this test there were 3 sub-directories.

As it stands right now the pertinent part of my script is reading as follows:

cd "c:\temp"
for /f "delims=" %%5  in ('dir /a:d /b') do dir /b /s  "%%5">%list%
 – Jose  – 2016-11-09T13:59:43.900

Since it treats as different commands so you have to use >>. in your case for /f "delims=" %%5 in ('dir /a:d /b') do dir /b /s "%%5">>%list% – Zalmy – 2016-11-09T14:02:05.193