Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 2224
  • Last Modified:

batch file to move file based on modified/creation date

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
eaweb
Asked:
eaweb
  • 8
  • 5
  • 2
  • +1
2 Solutions
 
mwochnickCommented:
I just want to be sure - are you using DOS or a command prompt within windows?
0
 
eawebAuthor Commented:
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
 
eawebAuthor Commented:
i want to run the code using a batch file
0
Visualize your virtual and backup environments

Create well-organized and polished visualizations of your virtual and backup environments when planning VMware vSphere, Microsoft Hyper-V or Veeam deployments. It helps you to gain better visibility and valuable business insights.

 
mwochnickCommented:
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
 
eawebAuthor Commented:
it's a windows server 2003 platform and i can only run a batch on it.
0
 
MattrwCommented:
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
 
eawebAuthor Commented:
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
 
mwochnickCommented:
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
 
Bill PrewCommented:
Can you run a VBS file on the server, date logic is much easier in VBS than BAT.

~bp
0
 
eawebAuthor Commented:
@ 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
 
eawebAuthor Commented:
if it is possible with a bat file would be great.
0
 
mwochnickCommented:
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
 
eawebAuthor Commented:
yes, the copy of file by modified/creation date is important as there are going to be processed by date
0
 
Bill PrewCommented:
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
 
eawebAuthor Commented:
is it possible to call this vbs from with a bat file?
0
 
mwochnickCommented:
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: 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.

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