Solved

batch file to move file based on modified/creation date

Posted on 2011-02-28
16
1,838 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
  • 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: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering 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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

The following is a collection of cases for strange behaviour when using advanced techniques in DOS batch files. You should have some basic experience in batch "programming", as I'm assuming some knowledge and not further explain the basics. For some…
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…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

761 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