Solved

need script to save logfile as hostname + time + date

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

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 52

Expert Comment

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

~bp
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Unlike scripting languages such as C# where a semi-colon is used to indicate the end of a command, Microsoft's VBScript language relies on line breaks to determine when a command begins and ends. As you can imagine, this quickly results in messy cod…
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 …
This Micro Tutorial will teach you how to censor certain areas of your screen. The example in this video will show a little boy's face being blurred. This will be demonstrated using Adobe Premiere Pro CS6.
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.

863 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

20 Experts available now in Live!

Get 1:1 Help Now