MOVE files based on DATE MODIFIED

Microsoft Windows xp professional w/serv...
February 12, 2010 at 09:57:51
Specs: Windows XP
So I created the below code to grab the YEAR of a file from the DATE MODIFIED and if it is NOT EQUAL to 2010, then move all the files in that DIR by creating a second .BAT. However, I have MANY sub-dirs, with MORE files, how can I have it go through ALL sub-dirs without having to code the sub dir each time.

echo off & setLocal EnableDELAYedExpansion
PUSHD Y:\DIR1\DIR2\
SET source=Y:\DIR1\SUBDIR1\
SET dest="F:\DIR1\SUBDIR1\"
for /f "tokens=* delims= " %%a in ('dir/b/a-d') do (
SET DATETIME=%%~Ta
for /f "tokens=1 delims=/: " %%m in ("%%~Ta") DO SET month=%%m
for /f "tokens=2 delims=/: " %%d in ("%%~Ta") DO SET day=%%d
for /f "tokens=3 delims=/: " %%y in ("%%~Ta") DO SET year=%%y
IF "!year!" NEQ "2010" (SET FILE=%%a)
IF "!year!" NEQ "2010" (>>"F:\MOVE.bat" ECHO MOVE /Y "!source!!FILE!" !dest! )
)
CALL "F:\MOVE.bat"


See More: MOVE files based on DATE MODIFIED

Report •


#1
February 12, 2010 at 10:14:14
That looks pretty good. But I'm not quite with you on the pushd src & dest.

To work on multiple directories, I would wrap it in another FOR:

[From the dir above your subs.]

for /f "tokens=* delims= " %%a in ('dir/s/b/ad') do (
pushd "%%a"
:: your FOR goes here
)


=====================================
Helping others achieve escape felicity

M2


Report •

#2
February 12, 2010 at 10:16:41
do you want the dir. structure mirrored or just all the files
globbed into one dest. directory?

prob'ly need to use xcopy then del /s


Report •

#3
February 12, 2010 at 10:19:37
So my end goal is as follows..

From one server, run the BATCH to go across the LAN to another, search for files that are older than 2010 in ALL subdirs from one primary DIR, then MOVE to same TREE structure or COPY/DEL to same TREE structure/current location.

I thought to use XCOPY, but wasnt finding success and botched that idea.

I tried searching the forum for a SERVER to SERVER move of files with same TREE structure by date modified...so I apologize if this is duplicated somehow.


Report •

Related Solutions

#4
February 12, 2010 at 11:13:22
My previous post sounds confusing...so I will diagram it...I need the following:

MOVE files with MODIFIED DATE older than 12/31/2009 in ALL sub directories on ONE server...to another with the same tree structure.

D:\MAINDIR\SUBDIR1\*.* [modified < year 2010] MOVED to
E:\MAINDIR\SUBDIR1\ [same DIR names]

And there would be MULTIPLE SUBDIR{2,3,4,5,etc} to crawl through once in the MAINDIR.

That is my challenege now, otherwise I have been going in and changing it. So I will try M2G's suggestion and see what occurs.


Report •

#5
February 12, 2010 at 14:44:30
i glued them together and added a couple things look like
they got omitted... and did some minor surgery...

echo off & setLocal EnableDELAYedExpansion
PUSHD Y:\DIR1\DIR2\
SET source=Y:\DIR1\SUBDIR1\
SET dest="F:\DIR1\SUBDIR1\"

for /f "tokens=* delims= " %%a in ('dir/s/b/ad') do (
pushd "%%a"
if not exist "!dest!%%a" md "!dest!%%a"
:: or: md "!dest!%%a" 2>nul
for /f "tokens=* delims= " %%b in ('dir/b/a-d') do (
set year=%%~Tb
:: ASSuming filedate looks like 01/02/2009
set year=!year:~6,4!
IF "!year!" NEQ "2010" >>F:\MOVE.bat ECHO MOVE /Y "!source!!FILE!" "!dest!%%a"
)
)
:: safetied off until verified
echo examine the batchfile...
more F:\move.bat


Report •

#6
February 13, 2010 at 07:07:30
> So I created the below code to grab the YEAR of
> a file from the DATE MODIFIED and if it is NOT
> EQUAL to 2010, then move all the files in that DIR
> by creating a second .BAT. However, I have MANY
> sub-dirs, with MORE files, how can I have it go
> through ALL sub-dirs without having to code the sub
> dir each time.
>

I would check out ROBOCOPY (MS software) or XXCOPY (3rd party stuff) and check out if any date related parameter (and ROBCOPY has got a huuuuuuuge amount of them) does what you ask ... instead of re-inventing the wheel.

Edit : there you go, got it working with XXCOPY :

xxcopy *.* D:\temp /DB:2009-12-31 /S

Copies all files in current dir, where last mod. time is last day of 2009 or earlier, and with files to be beholded in subdirs (creating dirs where needed)

The ROBOCOPY version also has mentioning of date, but only with "last access time" ... not what you want.

http://www.xxcopy.com

edit : you may need this, to perform a MOVE instead of copy :

xxcopy *.* D:\temp /DB:2009-12-31 /S /RC

(be CAREFUL as this command can be dangerous)


Report •

#7
February 28, 2010 at 07:36:25

I think OP already got a good solution to his problem. Here is one possible alternative for consideration.

# Scipt MoveModDate.txt
var str src, dest, date
var str list, file, newlocation
lf -r -n "*" $src ($fmtime < $date) > $list
while ($list <> "")
do
    lex "1" $list > $file
    sal -p ("^"+$src+"^") $dest $file > $newlocation
    system -s move ("\""+$file+"'\"") (\""+$newlocation+"\"")
done

This in biterscripting. Here is how it works. The lf (list files - http://www.biterscripting.com/helpp... ) finds all files in $src directory (recursively in subfolders -r) whose file mod time is earlier than $date. The $newlocation is created by replacing $src with $dest in $file. Finally, each such $file is moved to corresponding $newlocation. Call the script as


script MoveModDate.txt src("D:/MAINDIR") dest("E:/MAINDIR") date("20100101")


i think both forward and backward slashes work in file paths.


Report •

#8
March 17, 2010 at 14:52:40
Wooow! can we not use FORFILES? or am I missing something here?
Try this ---
FORFILES /p C:\Temp /m *.* /s /D -1 /C "cmd /c echo @file @fdate @ftime @path"

you could replace the '-1' with a date. Does it extract the files u wanted?


Report •

#9
March 18, 2010 at 04:30:27
@~0enum

I just read through the syntax...and that looks like it will work wonderfully. I will try it both on the DEV box and PROD box and let you know the outcome!!

Thanks!


Report •

Ask Question