-3

So, I have a large file tree that is something like this

\foo\foo1\foo\files\archive

\foo\foo2\foo\files\archive

\foo\foo3\foo\files\archive

\foo\foo4\foo\files\archive

my aim is to be able to loop through into each of the "\files" folders, and move the files in that folder to the "\archive" folder if they are older than 6 days old

Ideally a .bat script would be best. I'm a real novice when it comes to this kind of stuff and usually just use robocopy to perform these kinds of tasks but since the file structure is so dense writing 50+ lines of code to do this seems like the long way round.

I need to be able to drill down into 16 folders that each contain another 7 folders which contain the target files and the archive location and then decide if the target files are past the max age and move them into a subdirectory which lives inside the target location if they exceed the max age.

aschipfl
  • 33,626
  • 12
  • 54
  • 99
Andy_P
  • 1
  • 1
  • I'm not trying to delete anything. I need to be able to drill down into 16 folders that each contain another 7 folders which contain the target files and the archive location and then decide if the target files are past the max age and move them into a subdirectory which lives inside the target location if they exceed the max age. Sorry if I did not make that clear. – Andy_P Mar 09 '17 at 00:23
  • 2
    So use `move` instead of `del`. – SomethingDark Mar 09 '17 at 00:25
  • If I was to do this with and explicit 'target' and 'source' command. Like i normally would using robocopy I would need to write 112 lines of code to achieve this. If i cant find another way I will do it this way but I'm sure there's a better way of handling this. – Andy_P Mar 09 '17 at 00:29
  • also parent folder names are prone to change in this file tree so something using explicit directories isnt ideal. – Andy_P Mar 09 '17 at 00:32
  • As long as you have a programmatic way to find the destination folder you should be ok with [`forfiles`](https://ss64.com/nt/forfiles.html). Look at the answer of the link @SomethingDark gave you and try to understand it by reading the [documentation](https://ss64.com/nt/forfiles.html), you'll see that's the way to go ;-) – J.Baoby Mar 09 '17 at 07:27

2 Answers2

0
for /f "delims= " %%t in ('robocopy . . /njh /njs')do set "Tab=%%t"
pushd D:\
for /d /r %%i in (archive.?)do ( 
  pushD %%i
  for /f "Tokens=3 delims=%tab%" %%n in ('robocopy /L .. %%~ni /minage:6 
  ')do echo move ..\%%n
  popD
)
popD
pieh-ejdsch
  • 206
  • 1
  • 6
0

If I understood you right, you could try the following code snippet:

@echo off
setlocal EnableExtensions DisableDelayedExpansion

rem // Define constants here:
set "_ROOT=D:\"
set "_SRC=files"
set "_DST=archive"
set /A "_AGE=7"

for /D %%A in ("%_ROOT%\*") do (
    for /D %%B in ("%%~A\*") do (
        for /D %%C in ("%%~B\*") do (
            robocopy "%%~C\%_SRC%" "%%~C\%_SRC%\%_DST%" "*.*" /MOV /IS /MINAGE:%_AGE%
        )
    )
)

endlocal
exit /B

The same can be achieved by using the forfiles command instead of robocopy:

@echo off
setlocal EnableExtensions DisableDelayedExpansion

rem // Define constants here:
set "_ROOT=D:\"
set "_SRC=files"
set "_DST=archive"
set /A "_AGE=7"

for /D %%A in ("%_ROOT%\*") do (
    for /D %%B in ("%%~A\*") do (
        for /D %%C in ("%%~B\*") do (
            forfiles /P "%%~C\%_SRC%" /M "*" /D -%_AGE% /C "cmd /C if @isdir==FALSE 2> nul mkdir 0x22%_DST%0x22 & move /Y @file 0x22%_DST%0x22"
        )
    )
)

endlocal
exit /B
aschipfl
  • 33,626
  • 12
  • 54
  • 99