Solved

need script to save logfile as hostname + time + date

Posted on 2011-03-01
3
698 Views
Last Modified: 2012-05-11
I need a batch or vb script to rename a logfile with the hostname, time and date.
I found a vb script that will place the time and date but I'm not sure how to add the hostname part. If there's something out there that will do all three I'd prefer that then trying to piece together the hostname to the following vb script I found,
' timestampfile.vbs
' renames file %1 to ~YYYYMMDDHHMMSS.txt

Function Lz(Numb) ' Add leading zeros to single digit numbers
  Dim tmpval
  if isnumeric(Numb) then
      If (Numb>-1) and (Numb<10) Then
          tmpval = "0" & Numb
    Else
        tmpval = CStr(Numb)
      End if ' (Numb>-1) and (Numb<10)
  Else
      tmpval = Null
  end if ' isnumber(Numb)
  Lz = tmpval

End Function ' Lz

Dim dTheTime ' Current time
Dim sTS ' Timestamp
dTheTime = Now ' The time is now (TheTime is set to currend date/time)
Wscript.Echo "It is now " & dTheTime
sTS = Year(dTheTime) & Lz(Month(dTheTime)) & Lz(Day(dTheTime))
sTS = sTS & Lz(Hour(dTheTime)) & Lz(Minute(dTheTime)) & Lz(Second(dTheTime))
Wscript.Echo "Timestamp: [" & sTS & "]"

Dim args,fso,fn,f,txt,parts
Set args = WScript.Arguments ' get command-line arguments
fn = args(0) ' first argument
Set fso = CreateObject("Scripting.FileSystemObject")
if fso.FileExists(fn) Then
    Set f = fso.GetFile(fn)
    Wscript.Echo "[" & f.Name & "]"
    parts = Split(f.Name,".")
    txt = parts(UBound(parts))
    Wscript.echo sTS & "." & txt
    f.Name = "~" & sTS & "." & txt
end if ' if fso.FileExists(fn)

'begin dead code
' code to spawn a shell with timestamp as an environment variable
'Dim objShell
'Set objShell = CreateObject("WScript.Shell")
'objShell.Run "%COMSPEC% /C SET TIMESTAMP=" & sTS
0
Comment
Question by:dcs-user
  • 2
3 Comments
 
LVL 53

Accepted Solution

by:
Bill Prew earned 500 total points
ID: 35010537
Very easy to do in a BAT script, just use the following:

ren "c:\temp\logfile.txt" "%COMPUTERNAME%_%DATE:~-4%%DATE:~-10,2%%DATE:~-7,2%_%TIME:~0,2%%TIME:~3,2%%TIME:~6,2%.txt"

Open in new window


This will rename the file c:\temp\logfile.txt to hostname_YYMMDD_HHMMSS.txt

~bp
0
 

Author Closing Comment

by:dcs-user
ID: 35010790
Thanks Bill! That did it.
0
 
LVL 53

Expert Comment

by:Bill Prew
ID: 35011293
Great, thank you.

~bp
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Hello again, all.  For those of you that have been following along, you'll know that this is my third article on this topic (though it is not Part III).  This article is sort of remedial, and probably the topic with which I should have started the s…
When you see single cell contains number and text, and you have to get any date out of it seems like cracking our heads.
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

808 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question