Hi folk,
This is one of those requirements that seem to get more complicated, every time I find a Windows/cmd shell hack that need a 'work around'. Essentially ... I need to iterate through a specific list of folders in a DOS Shell FOR loop. Here is the loop I came up with:
echo ^ [start for test]
for /F "usebackq " %%f IN (`dir /b /adh "w:\sandbox\tmp\"`) DO (
echo ^ do with file: %%f
rem <do something>
)
echo ^ [for test done]
The main need is to iterate through the hidden directories in the source folder (sandbox\tmp here).
The result is both surprising and frustrating. This is the result from using this FOR instruction on the console command-line.
W:..> for /F "usebackq " %f
IN (`dir /b /adh "w:\sandbox\tmp\"`) DO echo ^ folder = %f
File Not Found <1>
folder = C:\WINDOWS\system32\cmd.exe <2>
folder = any <4>
folder = hidden-folder-01 <3>
folder = hidden-folder-02 <3>
folder = morph <4>
folder = practice-northwind <4>
Which lists five folders. Great! And one cmd.exe at label: <2> and a mystery at label: <1>. I ought to explain the DIR switches I think.
- /b ....... Bare format, just file/folder names.
- /a*dh* ..... Both hidden and directories, tried that exhaustively -- Didn't work.
/a*hd* - /a*d* ...... Items with the Directory attribute (folders).
- /a*h* ...... Hidden files/folder (seemingly).
I have labelled the output and here's what I've worked out so far.
- 'File Not Found' .. .. .. .. .. .. .. I have no idea. I find that the "File Not Found." <1> is an artefact of the /ah switch. When I just say:
IN (dir /b /adh "w:\sandbox\tmp\"
) - C:\WINDOWS\system32*cmd.exe* .. .. Spurious iterator (%%f); this error only shows with FOR /F command version. I want to find hidden directories, and the file set list doesn't return hidden files (d'oh). So, thus far I'm stuck with the usebackq version of FOR /F.
- Hidden Folders .. . .. .. .. .. .. .. These folders are the expected**output**_.
- Directory Folders . .. .. .. .. .. .. These files are spurious results, and not wanted.
The intended result from the CMD line version (or from the sample DOS script) is just the <3> items.
W:..> for /F "usebackq " %f
IN (`dir /b /adh "w:\sandbox\tmp\"`) DO echo ^ folder = %f
folder = hidden-folder-01 <3>
folder = hidden-folder-02 <3>
Unfortunately I 'can' also get just any file in the target folder. I think that's again to do with the HIDDEN switch (/ah).
How can I list only the hidden folders (not files) in a set directory; and iterate over the list like the FOR command?
Thanks in advance / Will