Help for running a script on all matching subdirectories.

Discussion in 'Software' started by ghadley_00, Jan 11, 2013.

  1. ghadley_00

    ghadley_00 Private E-2

    HI,

    I have a directory with multiple folders, all of which contains 2 additional levels of subdirectories (1 folder only per level). In the deepest folder there are several uniquely named folders, one of which will contain the word GRTR. I would like to run the same batch file on each of these GRTR folders. I am guessing that can be done using for /r or for /d, but my attempts are not working out. Any suggestions on how to do this would be greatly appreciated.

    Thanks!

    -GH
     
  2. foogoo

    foogoo Major "foogoo" Geek

    What do you want to do with the GRTR folders exactly? Read a file from there, move them??
     
  3. GermanOne

    GermanOne Guest

    What about /d and /r ;)
    Code:
    @echo off &setlocal
    
    for /d /r "c:\path\to\directory" %%i in (*grtr*) do (
      set "folder=%%i"
      call :proc_folder
    )
    
    pause
    goto :eof
    
    
    :proc_folder
    
    echo Do your stuff with
    echo "%folder%"
    echo here.
    echo ~~~
    
    goto :eof
    
     
  4. ghadley_00

    ghadley_00 Private E-2

    Thank you for the responses.

    This solution seems to be very close. The only problem I am having now is that after the code finds & processes the majority of the folders, the %folder% string starts becoming expanding folders that do not exist as follows:

    Actual folder C:\test\folder1\GRTR

    script generated

    C:\test\folder1\GRTR
    then
    C:\test\folder1\folder1\GRTR
    then
    C:\test\folder1\folder1\folder1\GRTR
    then
    C:\test\folder1\folder1\folder1\folder1\GRTR

    And so on, until I manually broke the loop.

    I do not understand why it worked for 90 % of the folders, then started doing this. The next folder in the directory listing did contain a folder called GRTR, so I am not sure what's going on.

    Also, is there a way to setup the batch file to take in a command line input for "c:\path\to\directory"

    Thank you again for the great suggestion.

    -GH
     
  5. ghadley_00

    ghadley_00 Private E-2

    Please disregard my previous message. There was something wrong with the specific folder where the problem occurred. The code worked as you suggested.

    If it is possible, is there a way to setup the batch file to take in a command line input for "c:\path\to\directory"

    Thank you again!

    -GH
     
  6. GermanOne

    GermanOne Guest

    SET /P is made to assign a variable from an user input.

    Change the beginning as follows
    Code:
    @echo off &setlocal
    
    :inloop
    set "directory="
    set /p "directory=Enter the path: "
    if not exist "%directory%" goto inloop
    
    for /d /r "%directory%" %%i in (*grtr*) do (
      set "folder=%%i"
      call :proc_folder
    )
    
    ... etc.
     

MajorGeeks.Com Menu

Downloads All In One Tweaks \ Android \ Anti-Malware \ Anti-Virus \ Appearance \ Backup \ Browsers \ CD\DVD\Blu-Ray \ Covert Ops \ Drive Utilities \ Drivers \ Graphics \ Internet Tools \ Multimedia \ Networking \ Office Tools \ PC Games \ System Tools \ Mac/Apple/Ipad Downloads

Other News: Top Downloads \ News (Tech) \ Off Base (Other Websites News) \ Way Off Base (Offbeat Stories and Pics)

Social: Facebook \ YouTube \ Twitter \ Tumblr \ Pintrest \ RSS Feeds