move folders older than 60 days

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
glob5
Posts: 34
Joined: 23 Nov 2010 00:47

move folders older than 60 days

#1 Post by glob5 » 03 Mar 2013 19:04

Need a batch script that move folders with no update activity have been done on them more than 2 months ago. No new files or folders have been added to them, no files have been updated.

Ocalabob
Posts: 79
Joined: 24 Dec 2010 12:16
Location: Micanopy Florida

Re: move folders

#2 Post by Ocalabob » 03 Mar 2013 21:38

Greetings glob5,
Please define "2 months ago". More details are needed.
Is Robocopy an option for you?

Best wishes!

glob5
Posts: 34
Joined: 23 Nov 2010 00:47

Re: move folders

#3 Post by glob5 » 03 Mar 2013 21:48

no such activity as explained for more than last 60 days. any tool and method that works the best.

Ocalabob
Posts: 79
Joined: 24 Dec 2010 12:16
Location: Micanopy Florida

Re: move folders

#4 Post by Ocalabob » 03 Mar 2013 22:41

glob5,
Fill in the details to suit your requirements.

Code: Select all

@echo off
Robocopy [Source] [Destination] /s /move /minage:60
pause


Follow up questions regarding Robocopy would not be on topic in this forum but possibly one of the regulars here will provide a batch only solution.

Best wishes!

(Edited to add /s switch to command line.)
Last edited by Ocalabob on 04 Mar 2013 09:04, edited 1 time in total.

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: move folders older than 60 days

#5 Post by foxidrive » 03 Mar 2013 23:13

OcalaBob, Robocopy is indeed on topic here. It's an external program just like findstr and find.exe are and is very useful to boot.

Maybe in this case Robocopy wouldn't move the entire folder but only the older files - is that how it works?

The code below checks the current directory for a list of folders, and then in each of those folders it checks for files younger than 60 days. If it doesn't find any then it moves the folder and contents to "%target%".

The number of days is set in line 2 which is -60 below.

Code: Select all

@echo off
call :getdate today -60

set "target=%userprofile%\desktop\target folder"

md "%target%" 2>nul


for /f "delims=" %%a in ('dir /a:d /b') do (
xcopy "%%a\*.*" "%temp%" /d:%day% /s/h/e/k/f/c/l |find ">" >nul || (
echo moving "%%~dpnxa" "%target%\"
move "%%a" "%target%\"
)
)
pause
goto :EOF


:getdate
:: Date foward & backward
@echo off
:: from code by Phil Robyn
setlocal
if [%1]==[] (
  echo to get todays date use
  echo call "%~n0" today 0
  echo.
  echo to get yesterdays date use
  echo call "%~n0" today -1
  echo.
  echo to get the date 25 days ago:
  echo call "%~n0" today -25
  echo.
  echo to get the date 1250 days in the future
  echo call "%~n0" today +1250
  goto :EOF)

set date1=%1
set qty=%2
if /i "%date1%" EQU "TODAY" (
 set date1=now
) else (
 set date1="%date1%"
)
echo >"%temp%\%~n0.vbs" s=DateAdd("d",%qty%,%date1%)
echo>>"%temp%\%~n0.vbs" WScript.Echo year(s)^&_
echo>>"%temp%\%~n0.vbs"         right(100+month(s),2)^&_
echo>>"%temp%\%~n0.vbs"         right(100+day(s),2)
for /f %%a in (
  'cscript //nologo "%temp%\%~n0.vbs"') do set result=%%a
del "%temp%\%~n0.vbs"
endlocal& set "day=%result:~4,2%-%result:~6,2%-%result:~0,4%"
:: echo %%day%% is set to "%day%" (without the quotes)

Ocalabob
Posts: 79
Joined: 24 Dec 2010 12:16
Location: Micanopy Florida

Re: move folders older than 60 days

#6 Post by Ocalabob » 04 Mar 2013 01:29

@foxidrive who saith,
Maybe in this case Robocopy wouldn't move the entire folder but only the older files - is that how it works?

Good question foxidrive!
I do know that /mov will move files by my testing. To move folders and
files you need /move but I have not tested the /move switch.

I'll give /move a test drive.

Later.

Ocalabob
Posts: 79
Joined: 24 Dec 2010 12:16
Location: Micanopy Florida

Re: move folders older than 60 days

#7 Post by Ocalabob » 04 Mar 2013 09:11

@foxidrive,
I did some limited testing and /move does indeed deal with folders and files. I failed to include the /s switch in my example and have since edited that post.

Thank you!

Squashman
Expert
Posts: 4487
Joined: 23 Dec 2011 13:59

Re: move folders older than 60 days

#8 Post by Squashman » 04 Mar 2013 10:11

Is forfiles an option?

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: move folders older than 60 days

#9 Post by foxidrive » 04 Mar 2013 12:04

Ocalabob wrote:@foxidrive,
I did some limited testing and /move does indeed deal with folders and files. I failed to include the /s switch in my example and have since edited that post.

Thank you!


Thanks for looking into it Ocalabob...

What would happen if Robocopy uses the /move and /s and is run in the folderA directory and this file below is the only one that is older than 60 days?
folderA\FolderB\abc.txt
Would the entire folderB be moved to the target or only abc.txt?

To make it easy - I tried it below. abc.txt was from 2012 and def.txt was todays file.

It only moved abc.txt - the OP wanted the entire folder moved only if all files were older than 2 months.

The error below was unexpected for me though...

Code: Select all

d:\abc\folderA>dir /b /s
d:\abc\folderA\folderB
d:\abc\folderA\folderB\abc.txt
d:\abc\folderA\folderB\def.txt

d:\abc\folderA>robocopy . \abc\folderC\ /move /s /minage:60

-------------------------------------------------------------------------------
   ROBOCOPY     ::     Robust File Copy for Windows
-------------------------------------------------------------------------------

  Started : Tuesday, 5 March 2013 4:57:29 AM
   Source : d:\abc\folderA\
     Dest : d:\abc\folderC\

    Files : *.*

  Options : *.* /S /DCOPY:DA /COPY:DAT /MOVE /MINAGE:60 /R:1000000 /W:30

------------------------------------------------------------------------------

          New Dir          0    d:\abc\folderA\
          New Dir          2    d:\abc\folderA\folderB\
100%        New File                   0        abc.txt

------------------------------------------------------------------------------

               Total    Copied   Skipped  Mismatch    FAILED    Extras
    Dirs :         2         2         0         0         0         0
   Files :         2         1         1         0         0         0
   Bytes :         0         0         0         0         0         0
   Times :   0:00:00   0:00:00                       0:00:00   0:00:00
   Ended : Tuesday, 5 March 2013 4:57:29 AM


2013/03/05 04:57:29 ERROR 32 (0x00000020) Deleting Source Directory d:\abc\folderA\
The process cannot access the file because it is being used by another process.

Ocalabob
Posts: 79
Joined: 24 Dec 2010 12:16
Location: Micanopy Florida

Re: move folders older than 60 days

#10 Post by Ocalabob » 05 Mar 2013 07:47

@foxidrive
What would happen if Robocopy uses the /move and /s and is run in the folderA directory and this file below is the only one that is older than 60 days?
folderA\FolderB\abc.txt
Would the entire folderB be moved to the target or only abc.txt?

Yes, folderB and abc.txt will be moved to folderC. Here's my results
after running Robocopy from a batch file.

C:\abc>dir /b /s
C:\abc\folderA
C:\abc\folderC
C:\abc\folderA\FolderB
C:\abc\folderA\FolderB\def.txt
C:\abc\folderC\FolderB
C:\abc\folderC\FolderB\abc.txt
It only moved abc.txt - the OP wanted the entire folder moved only if all files were older than 2 months.

I missed that important detail so Robocopy will not give the desired
in my example.
The error below was unexpected for me though...

I was able to reproduce the error. I then ran Robocopy from a batch file
outside of folderA with no error. I have no explanation for that so I think
I put together a few more tests.

Good job with your script and thank you for the Robocopy challenges!
Later.

Post Reply