VBScript needed to copy a file to a new folder and append a UK datestamp to the filename.

Hi,

I require a VBScript that will copy a file from one folder on the computer to another folder and then rename the file by adding a datestamp in UK format. I have looked around but the only ones I seem to be able to find are designed to run on computers with a US locale.

If possible the script would detect the systems locale that it is running on so that it would work on UK and US locale systems and maybe also the ability to copy the file to a different system as well.

Example:

Copy file from C:\MyFolder\MyFile.txt to C:\MyBackup\MyFile_2007-12-31.txt

Thanks in advance.
Bulldog98Asked:
Who is Participating?
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.

ee_rleeCommented:
hi

try this code out

just change MyFile and DestFolder to change the file to be copied and the destination folder.
Dim FSys
Dim MyFile, DestFolder
Dim DestFile
 
MyFile="C:\MyFolder\MyFile.txt"
DestFolder="C:\MyBackUp\"
 
Set FSys = CreateObject("Scripting.FileSystemObject")
DestFile= DestFolder & FSys.GetBaseName(MyFile) & "_" & Format(Now,"yyyy-mm-dd") & "." & FSys.GetExtension(MyFile)
 
FSO.CopyFile MyFile, DestFile

Open in new window

0
Bulldog98Author Commented:
Thanks for the quick response but when executing that script I receive the following error:

Line: 9
Char: 1
Error: Type mismatch: 'Format'
Code: 800A000D

Thanks
0
ee_rleeCommented:
how about this one
Dim FSys
Dim MyFile, DestFolder
Dim DestFile
 
MyFile="C:\MyFolder\MyFile.txt"
DestFolder="C:\MyBackUp\"
 
Set FSys = CreateObject("Scripting.FileSystemObject")
DestFile= DestFolder & FSys.GetBaseName(MyFile) & "_" & Year(Date) & "-"
if Month(Date)<10 then DestFile= DestFolder & "0"
DestFile= DestFolder & Month(Date) & "-"
if Day(Date)<10 then DestFile= DestFolder & "0"
DestFile= DestFolder & Day(Date) & "." & FSys.GetExtension(MyFile)
 
FSO.CopyFile MyFile, DestFile

Open in new window

0
Big Business Goals? Which KPIs Will Help You

The most successful MSPs rely on metrics – known as key performance indicators (KPIs) – for making informed decisions that help their businesses thrive, rather than just survive. This eBook provides an overview of the most important KPIs used by top MSPs.

Bulldog98Author Commented:
Sorry, that does not work either. :(

Line: 13
Char: 1
Error: Object doesn't support this property or method: 'GetExtension'

Thanks
0
ee_rleeCommented:
can you try this?
Dim i
Dim FSys
Dim MyFile, DestFolder
Dim FName, FExt
Dim DestFile
 
MyFile="C:\MyFolder\MyFile.txt"
DestFolder="C:\MyBackUp"
 
Set FSys = CreateObject("Scripting.FileSystemObject")
FExt=""
for i=len(myfile) to 1 step -1
   FExt= mid(myfile,i,1) & FExt
   if mid(myfile,i,1)="." then exit for
next
FName=""
for i=len(myfile)-len(FExt) to 1 step -1
   FName= mid(myfile,i,1) & FName
   if mid(myfile,i,1)="\" then exit for
next
 
DestFile= Mid(MyFile,Instr(MyFile,
DestFile= DestFolder & FName & "_" & Year(Date) & "-"
if Month(Date)<10 then DestFile= DestFolder & "0"
DestFile= DestFolder & Month(Date) & "-"
if Day(Date)<10 then DestFile= DestFolder & "0"
DestFile= DestFolder & Day(Date) & "." & FExt
 
FSys.CopyFile MyFile, DestFile

Open in new window

0
RobSampsonCommented:
Hi, try this:

'==================
strSourceFolder = = "C:\temp\Scripts\TestFiles"
strDestFolder="C:\temp\Scripts\TestFiles"
If Right(strDestFolder, 1) <> "\" Then strDestFolder = strDestFolder & "\"
 
Set objFSO = CreateObject("Scripting.FileSystemObject")
For Each objFile In objFSO.GetFolder(strSourceFolder).Files
      ' Check for a file extension
      If InStr(objFile.Name, ".") > 0 Then
            strExt = Mid(objFile.Name, InStrRev(objFile.Name, "."))
      Else
            strExt = ""
      End If
      strBaseName = Left(objFile.Name, Len(objFile.Name) - Len(strExt))
      strTheDate = Left(TimeNow(Now), 10)
      strNewName = strBaseName & "_" & strTheDate & strExt
      objFSO.CopyFile objFile.Path, strDestFolder & strNewName, True
Next

MsgBox "Done"

Function TimeNow(dDateTime)
      TimeNow =      Year(dDateTime) & "-" &_
                        Right("00" & Month(dDateTime), 2) & "-" &_
                        Right("00" & Day(dDateTime), 2) & "-" &_
                        Right("00" & Hour(dDateTime), 2) & "-" &_
                        Right("00" & Minute(dDateTime), 2) & "-" &_
                        Right("00" & Second(dDateTime), 2)
End Function
'==================

Regards,

Rob.
0

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
Bulldog98Author Commented:
Thanks for your help, everything is working now.
0
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
VB Script

From novice to tech pro — start learning today.