Learn how to a build a cloud-first strategyRegister Now

  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 340
  • Last Modified:

Automatically copy file and create folder that...

I have a question and I really need your expertise as I don't know if I should use good old DOS or maybe visual basic! Ok, here's the scenario:
I have a software that automatically output a file, let say the name of the file is Report.xyz on daily basis. Everyday the Report.xyz get overwritten so what I want to do is the following:
I would like to save the copy of the file each day before it gets created and I thought that maybe I can create a batch that would look at the file then create a folder name with the date of the file and then copy the file to that folder? i.e.

Report.xyz was create on March 20, 2006 then create a folder called March202006 and copy the report.xyz to that directory.
Report.xyz was create on March 21, 2006 then create a folder called March212006 and copy the report.xyz to that directory.

Can this be done and how? Or do you advise that I use visual basic, access or something similar?

Thank you.
  • 2
  • 2
1 Solution
The problem with using a batch file is that the date returned by the DOS date command needs processing to remove any characters that cannot appear in a file name (such as "/" or "\").

One easy way to solve this problem is to create an empty Visual Basic project with only one module. Add the following code to the module:


Sub Main()
  On Error Resume Next
  Dim s As String
  s = Date
  s = Replace(s, "/", "-")
  s = Replace(s, "\", "-")
  MkDir "f:\" & s
  FileCopy "f:\report.xyz", "f:\" & s & "\report.xyz"
  Kill "f:\report.xyz"
End Sub


This program assumes that the report file is stored in f:\. You need to replace "f:\" with the full path of the report file.

Now you only need to compile this program into an EXE, and then schedule a daily task for this program using the Windows task scheduler.


  Nayer Naguib
This is not exactly what you specified.

It's the batch file I've use many times before, it only keeps a copy of the old one, does not put it in a date folder but you already got the date in the filename.
I mainly love it because it's so simple and does the job (for me).

del C:\Archive\Project_old10.zip
ren C:\Archive\Project_old9.zip Project_old10.zip
ren C:\Archive\Project_old8.zip Project_old9.zip
ren C:\Archive\Project_old7.zip Project_old8.zip
ren C:\Archive\Project_old6.zip Project_old7.zip
ren C:\Archive\Project_old5.zip Project_old6.zip
ren C:\Archive\Project_old4.zip Project_old5.zip
ren C:\Archive\Project_old3.zip Project_old4.zip
ren C:\Archive\Project_old2.zip Project_old3.zip
ren C:\Archive\Project_old1.zip Project_old2.zip
ren C:\Archive\Project.zip Project_old1.zip
[now place code to create the new report or archive or whatever here]

As you can see it keeps 10 extra days of the archive or report, easily enough to extend to more. You only get tired fingers maybe. ;-)

If you really want to get a date folder I suggest either using VB or such as suggested by Nayer Naguib or you need to use some alternative DOS or DOS -extender or whatever.
jsufeeAuthor Commented:
This worked PERFECT. I will award you the "few" point but I had a question and I was afraid you might not come back to the post.

Instead of copying a file I would like to copy all file sin a directory and when I add *.* it dosn't work. Is Filecopy the right command or should I use something else.

>I will award you the "few" point but I had a question and I was afraid you might not come back to the post.

LOL! I'm not on Experts Exchange after the points anyway. :-)

You can use the following piece of code to copy files to some destination using wildcards:


fname = Dir("*.*", vbNormal) 'use here whatever wildcards you want
FileCopy fname, "f:\mydestination\" & fname
fname = Dir  'DO NOT use any parameters with Dir here
Loop Until fname = ""



  Nayer Naguib
jsufeeAuthor Commented:
hehehe :)
Many Thanks

Featured Post

Prep for the ITIL® Foundation Certification Exam

December’s Course of the Month is now available! Enroll to learn ITIL® Foundation best practices for delivering IT services effectively and efficiently.

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