| 1. |
Solve : how to list all folders in sub-directories? |
|
Answer» Hello hoping someone can help me with what I should think is a simple batch script I'm trying to create. I don't think I'm explaining what I want very clearlyYes. Try this. Some us a lists of folders and files as input. Some a new list of items you want as output. Ans wait. The best experts are busy doing other stuff. batch Run in top folder (called a in this example) Code: [Select]@echo off if exist list.txt DEL list.txt for /f "delims=" %%A in ('dir /b /ad') do dir /b /ad %%A >> list.txt list.txt Code: [Select]2 3 6 7 Geek, he or she explained just fine. Use quote marks like this if the folder names might have spaces @echo off if exist list.txt del list.txt for /f "delims=" %%A in ('dir /b /ad') do dir /b /ad "%%A" >> list.txt Quote from: Salmon Trout on December 07, 2018, 12:35:47 PM Geek, he or she explained just fine.I'm sorry . You are forgiven It looked a bit strange at first look, but you can puzzle it out. What you gave was good advice. Sorry for being snippy.Thank you so much for all the replies and help I’ve been out all day and late now so will try your suggestions in the morning 👍Hi, tried the suggestion and works exactly as hoped you guys are great, I'd be interested in a little explanation on what parts of the code do what as to hopefully get a better understanding so can try and work other coding out myself rather than have to ask for help. Quote from: mrwonker on December 08, 2018, 05:04:51 AM Hi, tried the suggestion and works exactly as hoped you guys are great, I'd be interested in a little explanation on what parts of the code do what as to hopefully get a better understanding so can try and work other coding out myself rather than have to ask for help. @echo off Turn off automatic echoing of each command LINE to the console. if exist list.txt del list.txt If list.txt exists, delete it. By implication, if it does not exist, do nothing. for /f "delims=" %%A in ('dir /b /ad') do dir /b /ad "%%A" >> list.txt The FOR /F command processes each line of e.g. a command output or text file, in this case the output of dir /b /ad (executed in the same folder that the batch is in) and puts each line, one, by one, into the FOR "metavariable" %%A. Each line is the name of a folder under the folder a. So each time around the LOOP, the metavariable %%A expands to the name of a folder. We do a dir /b /ad on each of these folders, which gives the names of the sub folders CONTAINED within one level below. We echo that output to list.txt, using the >> (append) operator. You can find full explanations of batch commands on many websites, one of the best is SS64.com. https://ss64.com/nt/for_f.html thanks for your explanation, what I'm not understanding and would like to get my head round is which part of the script is telling it to scan only the subfolder as I'd be quite interested in modifying this so that it could instead of scanning the subfolders scan the subfolders-subfolders so in my example would result in 4 8 I'd hoped I could have worked this out myself but after some time trying a few different things, I'm not having much luck. |
|