Search code examples
loopsrecursioncmdmove

Batch move many .tif files from their directories to respective subdirectories (all named “R20m”)


I am trying to move many .tif images in a batch file. I have a lot of folders with subfolders that all have the same folder structure. I one of this folders is one .tif file that always has a similar name (it contains "SCL" in the name). In that folder there is also a subfolder that always has a name "R20m". I want to move all of these .tif images to their respective R20m subfolder. I am stuck with the syntax. This is what I have so far:

for /r %%g in (*_SCL_*.tif) do Move %%g "%%~dp\R20m"

I do not know how to combine the current drive and/or path of the for loop with the R20 folder name.


Solution

  • You are very close. You just omitted the for loop variable name to be expanded. Does this work?

    for /r %%g in (*_SCL_*.tif) do Move "%%~g" "%%~dpgR20m\"
    

    Check to see that the new directory exists.

    for /r %%g in (*_SCL_*.tif) do (IF EXIST "%%~dpgR20m\" (Move "%%~g" "%%~dpgR20m\"))