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?
How to do recursive dir listing in first level only
Moderator: DosItHelp
Re: How to do recursive dir listing in first level only
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
)
Re: How to do recursive dir listing in first level only
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.
Dave Benham
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
Re: How to do recursive dir listing in first level only
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?
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?