Solved

need script to save logfile as hostname + time + date

Posted on 2011-03-01
3
695 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

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

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

Suggested Solutions

Script to copy or move mouse-selected collection of files plus targets referenced by shortcuts (.lnk) The purpose of this article is to help illuminate the real challenges and options available (where they may exist) for utilizing simple scriptin…
Introduction During my participation as a VBScript contributor at Experts Exchange, one of the most common questions I come across is this: "I have a script that runs against only one computer. How can I make it run against a list of computers in …
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…

816 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now