Link to home
Create AccountLog in
Avatar of VJ1980
VJ1980

asked on

How to rename a file using VBscript

Hi All,
 I have a task of copying previous days log file and rename it to the name of the server to which it belongs and then to generate a statistics page. With help of few friends in this forum i got the script to copy files with yesterday's date using
" dt = DateAdd("d",-1, Date)
yesterday = Right(Year(dt), 2) & Right("0" & Month(dt), 2) & Right("0" & Day(dt), 2)
strfilename = "J:\log\" & "ex" & Right(Year(dt), 2) & Right("0" & Month(dt), 2) & Right("0" & Day(dt), 2) & ".log"
But got stucked with another problem the name of the file is ex110103.log, this file needs to renamed to server1110103.log before generating the report is this possible to do with VB script.

Thanks
VJ



ASKER CERTIFIED SOLUTION
Avatar of RobSampson
RobSampson
Flag of Australia image

Link to home
membership
Create an account to see this answer
Signing up is free. No credit card required.
Create Account
1. You can either change it when you copied the file

Demonstration script that uses the FileSystemObject to rename a file. Script must be run on the local computer.
Set objFSO = CreateObject("Scripting.FileSystemObject")
objFSO.MoveFile "C:\FSO\ScriptLog.txt" , "C:\FSO\BackupLog.txt"

See also: http://www.activexperts.com/activmonitor/windowsmanagement/adminscripts/filesfolders/files/#RenameFile.htm

2. create the right filename at once

strfilename = "D:\Inetpub\Wwwroot\SP2010\Myspace" & "ex" & Right(Year(dt), 2) & Right("0" & Month(dt), 2)"
Replace(strfilename ,"ex","server")
By the way, is server a variable that hold another name or is it a string ?
Avatar of VJ1980
VJ1980

ASKER

perfect solution