Solved

batch file to move file based on modified/creation date

Posted on 2011-02-28
16
1,873 Views
Last Modified: 2012-05-11
hi,

i have two folder A and B

in folder A i have files (e.g. file1.edi, file2.rgi) created 1 day ago, 2 days ago etc...
i am in need of a batch that can copy the files in folder A to folder B based on the modified/creation date of 1 day ago, 2 days ago or of the running date etc....
0
Comment
Question by:eaweb
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 8
  • 5
  • 2
  • +1
16 Comments
 
LVL 12

Expert Comment

by:mwochnick
ID: 35000052
I just want to be sure - are you using DOS or a command prompt within windows?
0
 

Author Comment

by:eaweb
ID: 35000105
is it possible to have the copy procedure as follow?

on Monday copy files created on Friday, Tuesday copy files created on Monday, Wednesday copy files created on Tuesday etc...
0
 

Author Comment

by:eaweb
ID: 35000108
i want to run the code using a batch file
0
Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
LVL 12

Expert Comment

by:mwochnick
ID: 35000294
I understand wanting to use a batch file, but if you are on a window platform you can use Windows PowerShell which has a lot more capability than the DOS command prompt
0
 

Author Comment

by:eaweb
ID: 35000328
it's a windows server 2003 platform and i can only run a batch on it.
0
 
LVL 2

Expert Comment

by:Mattrw
ID: 35001027
Hi I have used robocopy for this type of job previously.  It comes with the 2003 resource kit or you can download below:

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

You can copy files based on age and dates etc.  A quick low down on the switches below:

http://ss64.com/nt/robocopy.html

ROBOCOPY source_folder destination_folder [file(s)_to_copy] [options]
0
 

Author Comment

by:eaweb
ID: 35001158
the option is great but, i am in need of a conventional code to do the following.

on Monday copy files created on Friday, Tuesday copy files created on Monday, Wednesday copy files created on Tuesday etc...

i don't have the robocopy on the server and i am not allowed to put either.
0
 
LVL 12

Expert Comment

by:mwochnick
ID: 35001306
so what are your infrastructure limitations?
does this need to run via a scheduler?
do you have another server/workstation where you could install ROBOCOPY or powershell?
can you map a drive to this server from another server with the ability to update?
could you create a small .NET program that would have windows scheduler kick it off and run locally on the server?
0
 
LVL 54

Expert Comment

by:Bill Prew
ID: 35003574
Can you run a VBS file on the server, date logic is much easier in VBS than BAT.

~bp
0
 

Author Comment

by:eaweb
ID: 35006583
@ billprew: yes i can run a vbs on the server

@mwochnick:
so what are your infrastructure limitations?
--can not install anything
does this need to run via a scheduler?
--yes, i am going to use the windows scheduler
do you have another server/workstation where you could install ROBOCOPY or powershell?
--no
can you map a drive to this server from another server with the ability to update?
--no
could you create a small .NET program that would have windows scheduler kick it off and run locally on the server?
--no

vbs is possible
it's a file server and very secure.
0
 

Author Comment

by:eaweb
ID: 35006612
if it is possible with a bat file would be great.
0
 
LVL 12

Expert Comment

by:mwochnick
ID: 35008157
is it important to separate these files by day? are the files going to different locations based on the day?

if not you could run the batch job everyday then all you would need is the following
XCOPY /M *.* newfolderpath

Open in new window

to copy the files. This takes advantage of the archive bit on the files.

0
 

Author Comment

by:eaweb
ID: 35008498
yes, the copy of file by modified/creation date is important as there are going to be processed by date
0
 
LVL 54

Accepted Solution

by:
Bill Prew earned 450 total points
ID: 35013898
Okay, here's a VB script to do the job.  Save as a VBS andthen runlikethis from a command line (or scheduler):

cscript EE26853320.vbs

' Define source and destination folders
sFromDir = "C:\EE26853320\A"    'Doesn't end in a back slash
sDestDir = "C:\EE26853320\B\"   'Must end in a back slash

' Define file system object
Set oFSO = CreateObject("Scripting.FileSystemObject")

' Calculate the date of the files we want to copy
' This is generally yesterday, but Friday needs to be copied on a Monday
Select Case Weekday(dGetDate)
   Case 2 'Monday
      dGetDate = DateAdd("d", -3, Now)
   Case Else 'Tuesday to Friday
      dGetDate = DateAdd("d", -1, Now)
End Select

' Look at all files in source, copy matching date files
Set oFolder = oFSO.GetFolder(sFromDir)
For Each oFile In oFolder.Files
   If DateDiff("d", dGetDate, oFile.DateLastModified) = 0 Then
      oFSO.CopyFile oFile, sDestDir, True
   End If
Next

Open in new window

~bp
0
 

Author Comment

by:eaweb
ID: 35026737
is it possible to call this vbs from with a bat file?
0
 
LVL 12

Assisted Solution

by:mwochnick
mwochnick earned 50 total points
ID: 35027424
The command cscript along with the script name should be enough.  For example:

cscript c:\myscript.vbs

It should execute like any other command.
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.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

TOMORROW TOMORROW.BAT is inspired by a question I get asked over and over again; that is, "How can I use batch file commands to obtain tomorrow's date?" The crux of this batch file revolves around the XCOPY command - a technique I discovered w…
YESTERDAY YESTERDAY.BAT is inspired by a previous article I wrote entitled: TOMORROW.BAT (http://www.experts-exchange.com/OS/Microsoft_Operating_Systems/MS_DOS/A_4196-Advanced-Batch-File-Programming-TOMORROW-BAT.html). The crux of this batch f…

752 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question