Link to home
Start Free TrialLog in
Avatar of Bulldog98
Bulldog98

asked on

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.
Avatar of ee_rlee
ee_rlee
Flag of Philippines image

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

Avatar of Bulldog98
Bulldog98

ASKER

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

Sorry, that does not work either. :(

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

Thanks
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

ASKER CERTIFIED SOLUTION
Avatar of RobSampson
RobSampson
Flag of Australia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thanks for your help, everything is working now.