0

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.

1 Answer 1

1
  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.

5
  • 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
    Commented Jan 30, 2016 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. Commented Jan 30, 2016 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. Commented Jan 31, 2016 at 1:15
  • @OTTERMATIC: I updated the answer to explain "delims=". Commented Jan 31, 2016 at 1:15
  • Everything makes perfect sense now. Thanks for all your help!
    – OTTERMATIC
    Commented Jan 31, 2016 at 4:02

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .