Batch create a Folder in every subfolders and Move all the .jpg files in it

I made this batch file that makes a folder named Pictures and puts all the .jpg files in it.

@echo off
md Pictures
for %%i in ("*.jpg") do (
 move /Y "%%i" "Pictures" )
end

Is there anyway to make the script iterate through subdirectories as well? I want to run the batch file on a root directory and make it work for all subfolders.

3 answers

  • answered 2020-07-04 21:59 coden00b

    Actually I just figured it out

    @echo off
    setlocal
    for /f "usebackq tokens=*" %%a in (`dir /b /a:d`) do (
      rem enter the directory
      pushd %%a
      echo In Directory: %%a
      md Pictures
      for %%i in ("*.jpg") do (
      move /Y "%%i" "Pictures" )
      rem leave the directory
      popd
      )
    endlocal
    

    It does the same thing but it works through subfolders. Took me sometime to figure it out. Anyway, hopefully it'll help others too.

  • answered 2020-07-04 22:23 Compo

    Based upon the answer you've supplied, I would suggest you could do that like this, in a single line :

    @For /D %%G In (*) Do @"%__AppDir__%Robocopy.exe" "%%G" "%%G\Pictures" *.jpg /Mov >NUL 2>&1
    

    As an alternative, because For /D may not pick up all of your directories, (it ignores directories with the hidden and system attributes):

    @For /F "EOL=? Delims=" %%G In ('Dir /B /AD') Do @"%__AppDir__%Robocopy.exe" "%%G" "%%G\Pictures" *.jpg /Mov >NUL 2>&1
    

  • answered 2020-07-05 01:12 Wasif_Hasan

    Try this:

    for /f %%a in ('dir /b /ad "Filepath"') do (
      md "%%~fa\Pictures"
      for %%b in ("*.jpg") do robocopy "%%~fb" "%%~fa\Pictures\" /mov
    )