How to do recursive dir listing in first level only

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
machone
Posts: 31
Joined: 01 Oct 2012 18:36

How to do recursive dir listing in first level only

#1 Post by machone » 10 Nov 2012 19:17

Hi,
I'm starting with the following dir command:

dir /s /t:w "constant_string* 01.txt" "constant_string* 01.csv" > list.csv

to catch all instances of txt and csv files which have an " 01" at the end of their filenames, and output the list to a csv file.
However, I need this command to only look in the first subdirectory level from where it's run. For example, if the command is executed in the \main\ directory:

\main\
\main\sublevel01\
\main\sublevel01\sublevel02\
\main\sublevel01\sublevel02\

It should search in the 3 \sublevel01\ directories and ignore the 2 \sublevel02\ directories. Can anyone help with this?

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: How to do recursive dir listing in first level only

#2 Post by foxidrive » 10 Nov 2012 20:36

This might work.


Code: Select all

@echo off
del list.csv 2>nul
for /f "delims=" %%a in ('dir /b /ad') do (
dir  /t:w "%%a\constant_string* 01.txt" "%%a\constant_string* 01.csv" >> list.csv
)

dbenham
Expert
Posts: 2461
Joined: 12 Feb 2011 21:02
Location: United States (east coast)

Re: How to do recursive dir listing in first level only

#3 Post by dbenham » 10 Nov 2012 21:54

The OP code does not produce a CSV. Also, /T:W is the default for a DIR listing.

The following will scan the main folder and the child folders, and produce a CSV file.

Code: Select all

@echo off
>list.csv (
  call :listFiles "\main"
  for /d %%D in ("\main\*") do call :listFiles "%%D"
)
exit /b

:listFiles
for %%F in (
  "%~1\constant_string* 01.txt"
  "%~1\constant_string* 01.csv"
) do echo "%%~tF",%%~zF,"%%~fF"
exit /b


Dave Benham

machone
Posts: 31
Joined: 01 Oct 2012 18:36

Re: How to do recursive dir listing in first level only

#4 Post by machone » 11 Nov 2012 08:57

Thanks foxidrive and dbenham.

Yes, I know it's not technically a csv list. I was just naming it that way because my next step is to open the results in a text editor and clean and restructure them using regex, the end result being an actual comma separated list. I was just naming it that way for clarity on my end.

dbenham, what would I need to change in your code if I wanted to restrict to only the first level of subfolders under the execution folder, or if I wanted to restrict to only the 2nd level?

Post Reply