Solved

need script to save logfile as hostname + time + date

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

Accepted Solution

by:
Bill Prew earned 500 total points
Comment Utility
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
Comment Utility
Thanks Bill! That did it.
0
 
LVL 51

Expert Comment

by:Bill Prew
Comment Utility
Great, thank you.

~bp
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Recently I finished a vbscript that I thought I'd share.  It uses a text file with a list of server names to loop through and get various status reports, then writes them all into an Excel file.  Originally it was put together for our Altiris server…
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 …
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

743 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

17 Experts available now in Live!

Get 1:1 Help Now