• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 665
  • Last Modified:

Move all files older than date X to location Y

i want to move all files older than 01/01/2006 in folder SharedFolder and all subFolders to MyFolder.

i'd like help writing a batch script to do this (or point me to a free utility)

thanks
windows server 2003
0
zephyr_hex (Megan)
Asked:
zephyr_hex (Megan)
  • 6
  • 5
  • 2
  • +2
1 Solution
 
amajidkhCommented:
this will get you started

http://blog.integrii.net/?p=27 but you willhave to modify it

mk
0
 
weellioCommented:
xcopy may help you here
start - cmd - xcopy /?
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
weellioCommented:
xcopy c:\thisfolder\*.* c:\sharefolder\ /D:01-01-2006
0
 
SteveGTRCommented:
xcopy /d will copy files on or after the date. I thought of the same thing (after you posted your initial comment).

Try this:

@echo off

setlocal enabledelayedexpansion

REM ** REM out next line to enable processing
set debug=echo

set srcDir=c:\SharedFolder
set destDir=c:\MyFolder

set cutoffDate=20060101

pushd "%srcDir%"

for /f "tokens=*" %%a in ('dir /s /b /a-d 2^>NUL') do call :PROCESS "%%a" %%~ta

popd

goto :EOF

:PROCESS

call :GETDATEPARTS "%~2"

if /i "%yy%%mm%%dd%" GEQ "%cutoffDate%" goto :EOF

set subDir=%~dp1
set subDir=%destDir%!subDir:%srcDir%=!

if not exist "%subDir%" %debug% mkdir "%subDir%"

%debug% copy "%~1" "%subDir%%~nx1"

goto :EOF

:GETDATEPARTS

set dt=%~1
set tok=1-3

if "%dt:~0,1%" GTR "9" set tok=2-4

set yyyy=

for /f "tokens=%tok% delims=.:/-, " %%a in ('echo %~1') do (
  for /f "skip=1 tokens=2-4 delims=/-,()." %%x in ('echo.^|date') do set %%x=%%a&set %%y=%%b&set %%z=%%c
)

if not "%yyyy%"=="" set yy=%yyyy%

if 1%yy% LSS 1000 (if %yy% LSS 70 (set yy=20%yy%) else (set yy=19%yy%))
if 1%mm% LSS 100 set mm=0%mm%
if 1%dd% LSS 100 set dd=0%dd%

goto :EOF
0
 
SteveGTRCommented:
Of course there is always robocopy:

http://www.microsoft.com/downloads/details.aspx?familyid=9d467a69-57ff-4ae7-96ee-b18c4790cffd&displaylang=en

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

  Started : Thu Aug 02 09:37:37 2007

              Usage :: ROBOCOPY source destination [file [file]...] [options]

             source :: Source Directory (drive:\path or \\server\share\path).
        destination :: Destination Dir  (drive:\path or \\server\share\path).
               file :: File(s) to copy  (names/wildcards: default is "*.*").

::
:: Copy options :
::
                 /S :: copy Subdirectories, but not empty ones.
                 /E :: copy subdirectories, including Empty ones.
             /LEV:n :: only copy the top n LEVels of the source directory tree.

                 /Z :: copy files in restartable mode.
                 /B :: copy files in Backup mode.
                /ZB :: use restartable mode; if access denied use Backup mode.
            /EFSRAW :: copy all encrypted files in EFS RAW mode.

  /COPY:copyflag[s] :: what to COPY for files (default is /COPY:DAT).
                       (copyflags : D=Data, A=Attributes, T=Timestamps).
                       (S=Security=NTFS ACLs, O=Owner info, U=aUditing info).

           /DCOPY:T :: COPY Directory Timestamps.

               /SEC :: copy files with SECurity (equivalent to /COPY:DATS).
           /COPYALL :: COPY ALL file info (equivalent to /COPY:DATSOU).
            /NOCOPY :: COPY NO file info (useful with /PURGE).

            /SECFIX :: FIX file SECurity on all files, even skipped files.
            /TIMFIX :: FIX file TIMes on all files, even skipped files.

             /PURGE :: delete dest files/dirs that no longer exist in source.
               /MIR :: MIRror a directory tree (equivalent to /E plus /PURGE).

               /MOV :: MOVe files (delete from source after copying).
              /MOVE :: MOVE files AND dirs (delete from source after copying).

     /A+:[RASHCNET] :: add the given Attributes to copied files.
     /A-:[RASHCNET] :: remove the given Attributes from copied files.

            /CREATE :: CREATE directory tree and zero-length files only.
               /FAT :: create destination files using 8.3 FAT file names only.
               /256 :: turn off very long path (> 256 characters) support.

             /MON:n :: MONitor source; run again when more than n changes seen.
             /MOT:m :: MOnitor source; run again in m minutes Time, if changed.

      /RH:hhmm-hhmm :: Run Hours - times when new copies may be started.
                /PF :: check run hours on a Per File (not per pass) basis.

             /IPG:n :: Inter-Packet Gap (ms), to free bandwidth on slow lines.

::
:: File Selection Options :
::
                 /A :: copy only files with the Archive attribute set.
                 /M :: copy only files with the Archive attribute and reset it.
    /IA:[RASHCNETO] :: Include only files with any of the given Attributes set.
    /XA:[RASHCNETO] :: eXclude files with any of the given Attributes set.

 /XF file [file]... :: eXclude Files matching given names/paths/wildcards.
 /XD dirs [dirs]... :: eXclude Directories matching given names/paths.

                /XC :: eXclude Changed files.
                /XN :: eXclude Newer files.
                /XO :: eXclude Older files.
                /XX :: eXclude eXtra files and directories.
                /XL :: eXclude Lonely files and directories.
                /IS :: Include Same files.
                /IT :: Include Tweaked files.

             /MAX:n :: MAXimum file size - exclude files bigger than n bytes.
             /MIN:n :: MINimum file size - exclude files smaller than n bytes.

          /MAXAGE:n :: MAXimum file AGE - exclude files older than n days/date.
          /MINAGE:n :: MINimum file AGE - exclude files newer than n days/date.
          /MAXLAD:n :: MAXimum Last Access Date - exclude files unused since n.
          /MINLAD:n :: MINimum Last Access Date - exclude files used since n.
                       (If n < 1900 then n = n days, else n = YYYYMMDD date).

                /XJ :: eXclude Junction points. (normally included by default).

               /FFT :: assume FAT File Times (2-second granularity).
               /DST :: compensate for one-hour DST time differences.

               /XJD :: eXclude Junction points for Directories.
               /XJF :: eXclude Junction points for Files.

::
:: Retry Options :
::
               /R:n :: number of Retries on failed copies: default 1 million.
               /W:n :: Wait time between retries: default is 30 seconds.

               /REG :: Save /R:n and /W:n in the Registry as default settings.

               /TBD :: wait for sharenames To Be Defined (retry error 67).

::
:: Logging Options :
::
                 /L :: List only - don't copy, timestamp or delete any files.
                 /X :: report all eXtra files, not just those selected.
                 /V :: produce Verbose output, showing skipped files.
                /TS :: include source file Time Stamps in the output.
                /FP :: include Full Pathname of files in the output.
             /BYTES :: Print sizes as bytes.

                /NS :: No Size - don't log file sizes.
                /NC :: No Class - don't log file classes.
               /NFL :: No File List - don't log file names.
               /NDL :: No Directory List - don't log directory names.

                /NP :: No Progress - don't display % copied.
               /ETA :: show Estimated Time of Arrival of copied files.

          /LOG:file :: output status to LOG file (overwrite existing log).
         /LOG+:file :: output status to LOG file (append to existing log).

       /UNILOG:file :: output status to LOG file as UNICODE (overwrite existing log).
      /UNILOG+:file :: output status to LOG file as UNICODE (append to existing log).

               /TEE :: output to console window, as well as the log file.

               /NJH :: No Job Header.
               /NJS :: No Job Summary.

           /UNICODE :: output status as UNICODE.

::
:: Job Options :
::
       /JOB:jobname :: take parameters from the named JOB file.
      /SAVE:jobname :: SAVE parameters to the named job file
              /QUIT :: QUIT after processing command line (to view parameters).
              /NOSD :: NO Source Directory is specified.
              /NODD :: NO Destination Directory is specified.
                /IF :: Include the following Files.
0
 
zephyr_hex (Megan)DeveloperAuthor Commented:
do robocopy and xcopy MOVE files?  i don't want to copy them... i want to move them....
0
 
SteveGTRCommented:
Move files. I missed that. My initial batch processing copies them. Here's a modified version.

@echo off

setlocal enabledelayedexpansion

REM ** REM out next line to enable processing
set debug=echo

set srcDir=c:\SharedFolder
set destDir=c:\MyFolder

set cutoffDate=20060101

pushd "%srcDir%"

for /f "tokens=*" %%a in ('dir /s /b /a-d 2^>NUL') do call :PROCESS "%%a" %%~ta

popd

goto :EOF

:PROCESS

call :GETDATEPARTS "%~2"

if /i "%yy%%mm%%dd%" GEQ "%cutoffDate%" goto :EOF

set subDir=%~dp1
set subDir=%destDir%!subDir:%srcDir%=!

if not exist "%subDir%" %debug% mkdir "%subDir%"

if exist "%subDir%%~nx1" %debug% del "%subDir%%~nx1"

%debug% move "%~1" "%subDir%%~nx1"

goto :EOF

:GETDATEPARTS

set dt=%~1
set tok=1-3

if "%dt:~0,1%" GTR "9" set tok=2-4

set yyyy=

for /f "tokens=%tok% delims=.:/-, " %%a in ('echo %~1') do (
  for /f "skip=1 tokens=2-4 delims=/-,()." %%x in ('echo.^|date') do set %%x=%%a&set %%y=%%b&set %%z=%%c
)

if not "%yyyy%"=="" set yy=%yyyy%

if 1%yy% LSS 1000 (if %yy% LSS 70 (set yy=20%yy%) else (set yy=19%yy%))
if 1%mm% LSS 100 set mm=0%mm%
if 1%dd% LSS 100 set dd=0%dd%

goto :EOF
0
 
zephyr_hex (Megan)DeveloperAuthor Commented:
thanks.  i will give that a test run and post back results.
0
 
zephyr_hex (Megan)DeveloperAuthor Commented:
i am getting an error:
move "C:\Documents and Settings\Owner\My Documents\test\Customer Sort.rpt" ""C:\
Documents and Settings\Owner\My Documents\test2"C:\Documents and Settings\Owner\
My Documents\test\Customer Sort.rpt"
'and' is not recognized as an internal or external command,
operable program or batch file.


i changed the source and destination paths, and defined them as follows:
set srcDir="C:\Documents and Settings\Owner\My Documents\test"
set destDir="C:\Documents and Settings\Owner\My Documents\test2"

is that where the problem is?
0
 
SteveGTRCommented:
Get rid of the double quotes:

set srcDir=C:\Documents and Settings\Owner\My Documents\test
set destDir=C:\Documents and Settings\Owner\My Documents\test2
0
 
zephyr_hex (Megan)DeveloperAuthor Commented:
ok, it seems to be running, but not actually doing the move operation.  i ran it from the command line and put output to a log file.  this is the contents of the log file:
move "C:\Documents and Settings\Owner\My Documents\test\qpg.exe" "C:\Documents and Settings\Owner\My Documents\test2\qpg.exe"


which is correct... out of the files in folder "test", this is the one that is older than Jan 2006.  however, the file remains in its original location, and is not moved to the destination.

ideas?
0
 
SteveGTRCommented:
It is currently in debug mode for testing. To active the actual processing you have to REM out the set statement as annotated in the code.

REM ** REM out next line to enable processing
REM set debug=echo
0
 
zephyr_hex (Megan)DeveloperAuthor Commented:
perfect.

thanks
0
 
irishpaulCommented:
what type of script is this? .bat, .vbs?
0
 
SteveGTRCommented:
.bat
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

  • 6
  • 5
  • 2
  • +2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now