I'm trying to figure out a way to execute commands on multiple files that exist in multiple folders. From what I understand, a for loop is the best way to do this in CMD (I'm using Windows.) I'm running into some issues though, and I'll use one of the operations I need to do as an example.

For this specific command, I'm trying to make folders for multiple files, within multiple folders. Here's a visual:

Folder A  
    File 1.avi  
    File 2.avi  
Folder B  
    File 3.avi  
    File 4.avi

I did some searching around, and I came up with this command:

for /f "delims=*" %a in ('dir *.avi /b /s') do md "%~na"

Which I scrapped together from here. This is almost right -- it makes a folder for each file contained within the multiple folders. The problem is, I need those new folders to be contained within the same folders as the files. This specific command would put the new folders in the place as the original folders, rather than within them.

Does anyone have any ideas? Any help would be appreciated. Thank you.

up vote 1 down vote accepted
  1. A standard debugging technique is to insert the echo command into scripts and even compound/complex commands.  If you do

    for /f "delims=*" %a in ('dir *.avi /b /s') do @echo md "%~na"
    

    you’ll get the output

    "file 1"
    "file 2"
    "file 3"
    "file 4"
    

    Notes:

    • The @ prevents the echo commands themselves from displaying, so you see only their output.
    • "delims=…" tells for how to parse the lines of output from the dir *.avi /b /s command.  I don’t know why the answer you linked to suggests "delims=*".  But the default behavior is to break lines apart at spaces, so, if your directory and/or file names contain spaces (as you indicated), you should use "delims=" (specifying that there are no delimiters) to get this to work.
  2. If you type for /? or help for, you’ll get documentation on the for command.  Down in the fifth page, you’ll see

    In addition, substitution of FOR variable references has been enhanced.
    You can now use the following optional syntax:
    
        %~I         - expands %I removing any surrounding quotes (")
                         ︙ 
        %~pI        - expands %I to a path only
        %~nI        - expands %I to a file name only
                         ︙ 
    
    The modifiers can be combined to get compound results …
                             ︙ 
    

    which explains why %~na is getting you just the file name of the *.avi files whose full names are in %a.  Now try

    for /f "delims=" %a in ('dir *.avi /b /s') do @echo md "%~pa"
    

    and you’ll get

    "the_current_directory\Folder A\"
    "the_current_directory\Folder A\"
    "the_current_directory\Folder B\"
    "the_current_directory\Folder B\"

    From which we can conclude that you want to do

    for /f "delims=" %a in ('dir *.avi /b /s') do md "%~pa%~na"
    

    to create the file 1 and file 2 directories under Folder A, and the file 3 and file 4 directories under Folder B.   And, as @dave_thompson_085 points out, you can combine %~pa%~na into %~pna.

  • Perfect! This is exactly what I needed, and very well explained. One small question, about the "delims=*" part. I actually ended up needing that, because without that part, the command would try to make the top level folders into new folders -- basically, just copy-paste the folders that were already there. When I put that part back into the command, it goes into the folders and works properly. Just so I fully understand what's actually going, any idea why this is? – OTTERMATIC Jan 30 '16 at 7:21
  • 1
    As for/? goes on to say you can combine multiple pieces in one substitution, here %~pna . Also, did you really test dirnames containing a space like @OTTERMATIC's without "delims="? For me (on 8.1) that discards all of the path after the space. – dave_thompson_085 Jan 30 '16 at 9:46
  • @dave_thompson_085: No, I used FolderA\File1.avi, etc., and edited the output to correspond to the filenames in the question.  (Hangs head in shame.)  You’re absolutely right — the answer I presented doesn’t actually work as described; I’ve fixed it. – G-Man Jan 31 '16 at 1:15
  • @OTTERMATIC: I updated the answer to explain "delims=". – G-Man Jan 31 '16 at 1:15
  • Everything makes perfect sense now. Thanks for all your help! – OTTERMATIC Jan 31 '16 at 4:02

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.