We help IT Professionals succeed at work.

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

Bulldog98
Bulldog98 asked
on
Medium Priority
1,142 Views
Last Modified: 2012-06-27
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.
Comment
Watch Question

Top Expert 2008

Commented:
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

Author

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
Top Expert 2008

Commented:
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

Author

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

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

Thanks
Top Expert 2008

Commented:
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

CERTIFIED EXPERT
Most Valuable Expert 2012
Top Expert 2014
Commented:
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.

Author

Commented:
Thanks for your help, everything is working now.