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 on daily basis. Everyday the 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. was create on March 20, 2006 then create a folder called March202006 and copy the to that directory. was create on March 21, 2006 then create a folder called March212006 and copy the to that directory.

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

Thank you.
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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:\", "f:\" & s & "\"
  Kill "f:\"
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\
ren C:\Archive\
ren C:\Archive\
ren C:\Archive\
ren C:\Archive\
ren C:\Archive\
ren C:\Archive\
ren C:\Archive\
ren C:\Archive\
ren C:\Archive\
ren C:\Archive\
[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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
jsufeeAuthor Commented:
hehehe :)
Many Thanks
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.