Solved

batch file to move file based on modified/creation date

Posted on 2011-02-28
16
1,962 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
On Demand Webinar: Networking for the Cloud Era

Ready to improve network connectivity? Watch this webinar to learn how SD-WANs and a one-click instant connect tool can boost provisions, deployment, and management of your cloud connection.

 
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 56

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 56

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

Ready to get started with anonymous questions?

It's easy! Check out this step-by-step guide for asking an anonymous question on Experts Exchange.

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…
One of my most closely kept secrets is revealed in this discussion How to output text on the same line This question was recently posted in EE by Simon336697 (http://www.experts-exchange.com/Programming/Languages/Scripting/Shell/Batch/Q_2459…
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …

624 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