Solved

need script to save logfile as hostname + time + date

Posted on 2011-03-01
3
701 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 54

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 54

Expert Comment

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

~bp
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Everything left of comma in excel 4 51
Auto-merge multiple transaction ledgers in Excel 5 44
CMD VB Script: remove empty rows 12 13
script to read text file 34 28
This is an addendum to the following article: Acitve Directory based Outlook Signature (http://www.experts-exchange.com/Programming/Languages/Visual_Basic/VB_Script/Q_24950055.html) The script is fine, and works in normal client-server domains…
Deploying a Microsoft Access application in a Citrix environment is not difficult but takes a few steps. However, Citrix system people are often of little help, as they typically know next to nothing about Access. The script provided here will take …
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial
Suggested Courses

734 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