Search code examples
batch-filecmdsubdirectory

run batch file in all subfolders


ok so i have a simple batch file someone created for me to basically count all files in current folder and then create 3 folders like so

Photos\2017_11_01\3
Photos\2017_11_01\10
Photos\2017_11_01\30

code is as follows

@echo off
:: get total number of files
for /f %%A in ('dir /b *.jpg ^| find /v /c ""') do set /A total=%%A

:: create subfolders and calculate skip
for %%A in (3 10 30) Do (
  mkdir "%%A"
  if %%A neq 30 (
    Set /A "skip%%A=total/25/%%A
  ) Else (
    Set /A "skip%%A=total/%%A
  )
)

set Counter=0
for %%f in (*.jpg) do call :p "%%f"
goto :EOF

:p
set /a "Counter+=1,X=Counter%%skip3,Y=Counter%%skip10,Z=Counter%%skip30"
if %X%==0 copy %1 "3"
if %Y%==0 copy %1 "10"
if %Z%==0 copy %1 "30"
goto :eof  

Is copies files from the current folder into the newly created folders and has worked like a charm since MAY this year. Trouble is its very time consuming and have to do this on a daily basis or weekly. However i havent done it for 2 months so no i have nearly 60 folders to go through which would take me all day.

My Question Can this be modified so that i can run the batch file in the "Photos" folder and have it run in each subfolder recursively.

I don't want to automate it daily for a couple of reasons. so being able to run the batch file in multiple subfolder at once would be a massive improvement.


Solution

  • I suggest using your working script as is and build a wrapper:

    @echo off
    for /d %%a in (20*) do (
      echo processing %%a
      pushd "%%a"
      call "%~dp0yourcurrentscript.bat"
      popd
    )