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 Answers1

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.