Solved

batch file to move file based on modified/creation date

Posted on 2011-02-28
16
1,570 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
 
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
Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

 
LVL 51

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 51

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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Using dates in 'DOS' batch files has always been tricky as it has no built in ways of extracting date information.  There are many tricks using string manipulation to pull out parts of the %date% variable or output of the date /t command but these r…
VALIDATING DATES One method of validating dates is to jam the date into the DATE command and see if it accepts it by examining the system's errorlevel value. A non-zero result indicates failure. A typical example might look something like the fol…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…

758 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

Need Help in Real-Time?

Connect with top rated Experts

24 Experts available now in Live!

Get 1:1 Help Now