views:

348

answers:

1

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.

  1. '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\")
  2. 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.
  3. Hidden Folders .. . .. .. .. .. .. .. These folders are the expected**output**_.
  4. 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

+2  A: 

In your batch file dir /b /a dh "w:\sandbox\tmp\" should be dir /b /adh "w:\sandbox\tmp\" with no space between /a and dh. With the space the dir command is interpreting dh as a directory or file paremer and is the cause of your file not found error.

When you are running from the command line you are using a different parameter to the dir command dir /b /ah /ad "w:\sandbox\tmp\" so you are getting different output. With /ah and /ad as separate parameters dir seems to interpret that it should show things that are directories or hidden files while /adh will show thing that are both directories and hidden.

Also you don't need to have @ on all of your lines. Just but @echo off at the top of the file and that will disable the command echoing for the entire batch file.

shf301
Hi shf301,Thanks for you comments. I found those points quite earlyon. Unfortunately those changes don't help the problem(s). I took the space out of the /aX command. dir /ah .... doesn't show hidden files or folders. dir /ad .... shows directories dir /adh and dir /ahd .... Shows absolutely NO files or directories.In all cases in the FOR /F loop -- I get a spurious $system/cmd.exe in the list.My leading @ is deliberate, I found it helps me when debugging script 15 years back./w.
will