Solved

Command Prompt text to file with inserted text

Posted on 2013-06-11
11
596 Views
Last Modified: 2013-12-03
I have a java command I'm running from a command prompt

the java initiates an open connection to receive data in bursts from the vendor

I am appending the=is to a text file with no problem

C:\ java -cp m5.jar > C:\temp\output.txt

The data comes in and is appended t the file line by line

WHat I need to do is on EACH line passed to the text file insert a leading timestamp in MMddyyyy hh:mm:ss format

So these lines
transid 123
transID 124

Would go in
06112013 11:10:01  transid 123
06112013 11:10:11  transid 124

Would a
0
Comment
Question by:lrbrister
  • 5
  • 2
  • 2
  • +2
11 Comments
 
LVL 11

Expert Comment

by:netballi
Comment Utility
The thing you are trying to achieve is not possible with command prompt you need to built the logic in your program or script java -cp m5.jar to redirect the output with time stamp. to an external file.
0
 

Author Comment

by:lrbrister
Comment Utility
netballi,
  Thanks.  At least I have an answer
0
 
LVL 43

Expert Comment

by:Steve Knight
Comment Utility
Yes and no... how often is the data written.  What you could do:

Java running in one process writing to the file
Second process would:

1. Check if log file exists, rename to log.working.txt
2. Read each line of log.working.txt  Send each line with added time stamp to log.archive.txt
3. Delete log.working.txt
4. Loop around again

Something like this, not tested ...

Steve

@echo off
set log=output.txt
set working=log.working.txt
set archive=log.archive.txt

cd /d C:\temp

:loop
if exist %log% (
  if exist %working% del %working%
  rename %log% %working%
  (for /f "tokens=*" %%a in (%working%) do echo %date% %time% %%~a)>>%archive%
  del %working% 2>NUL
)
call :sleep 5
goto loop
exit /b

:sleep
REM Wait no. of seconds approx.
  ping 127.0.0.1 -n %~1 >NUL 2>&1
exit /b

Open in new window

0
 
LVL 51

Expert Comment

by:Bill Prew
Comment Utility
Do you care what the time stamp is?  Meaning do you want it to be when that line was written, or anything after that?

Can you change the java program to add the time stamp, or do you not have the source code?

It looks like the Java program is writing to the STDOUT stream, it might be that the output file is being held open while the Java program is executing, in which case you may not be able to have a second program dubbing in the time stamp.

Do you process the data from the file in bursts as well, right after they come in, or is the file processed in a "batch" mode maybe once a day, etc?

~bp
0
 

Author Comment

by:lrbrister
Comment Utility
billprew
The java keeps the connection open

Basically I send a command with their jar with a list of phone numbers to monitor
The connection stays open and I get a stream or data back in XML format (csta standards) for each phone.

As the data comes in it is appended to the text file.

Looks like there's no way to do this "live."  Right folks?
0
6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

 
LVL 43

Expert Comment

by:Steve Knight
Comment Utility
OK so it doesn't work like I suggested then?

If you rename the file in explorer while the app is working does it rename, does the app keep working?

If not then you'll have to amend the app, or stop the app periodically and time stamp all those lines then carry on.

Steve
0
 

Author Comment

by:lrbrister
Comment Utility
dragon-it,
 I'm going t have to review that tomorrow.

We're going live on a new system and I'm absolutely buried for next 20 hours.

I will most certainly look at your solution.
0
 
LVL 82

Accepted Solution

by:
oBdA earned 500 total points
Comment Utility
You can use an AutoIt script (http://www.autoitscript.com/, free for private and commercial use) that takes the input and adds the time stamp at the beginning of each new line.
AutoIt is an easy to learn script language which allows you to "compile" the scripts  (that is, bundle the script and the interpreter) into stand-alone exes (32 and/or 64bit).
Download and install AutoIt and the full (customized) SciTE Editor and compile the script "AddDateToStdout.au3" (or whatever you want to name it) below to "AddDateToStdout.exe" as console/CUI application (not as GUI!). With the full editor, you should be able to just open the script in SciTE and hit F7 to create the exe.
Then pipe the output to AddDateToStdout.exe before you redirect to a log file (if you insist, I could provide a compiled version, but I'd rather not distribute exe files...):
C:\ java -cp m5.jar | AddDateToStdout.exe > C:\temp\output.txt
#Region ;**** Directives created by AutoIt3Wrapper_GUI ****
#AutoIt3Wrapper_UseUpx=n
#AutoIt3Wrapper_Compile_Both=y
#AutoIt3Wrapper_UseX64=y
#AutoIt3Wrapper_Change2CUI=y
#AutoIt3Wrapper_Res_requestedExecutionLevel=asInvoker
#EndRegion ;**** Directives created by AutoIt3Wrapper_GUI ****
#cs
AddDateToStdout.au3
Script for AutoIt (http://www.autoitscript.com/)
Reads from another process's stdout and adds a timestamp at the beginning of each line.

Tested with AutoIt v3.3.8.1; use Aut2exe.exe or the bundled SciTE to compile this script into an executable.
#ce
If Not @Compiled Then
	MsgBox(0, "", "This script must be compiled as Console application and data piped to it in order to properly demonstrate its functionality.")
	Exit -1
EndIf

AutoItSetOption("MustDeclareVars", 1)
AutoItSetOption("TrayIconHide", 1)

Global $intErrorLevel

Func _ParseCommandLine()
Local $strSyntax, $i
	If ($CmdLine[0] <> 0) Then
		$strSyntax = @CRLF & _
			"Reads stdout from another process and adds a time stamp at the beginning of each line." & @CRLF & _
			"Example: ping localhost | " & @ScriptName & @CRLF & _
			"To capture stderr as well, redirect the process's stderr to stdout by using '2>&1'." & @CRLF & _
			"Example: dir sdfsdf 2>&1 | " & @ScriptName & @CRLF
		ConsoleWrite($strSyntax)
		Exit 1
	EndIf
EndFunc

Func _Main()
Local $strInput, $strLine, $strTimeStamp
Local $bAddAtBeginningOfLine = True
	While True
		$strInput = ConsoleRead()
		If @error Then
			ExitLoop
		EndIf
		If (@extended > 0) Then
			$strTimeStamp = @MON & @MDAY & @YEAR & " " & @HOUR & ":" & @MIN & ":" & @SEC
			If $bAddAtBeginningOfLine Then
				$strLine = $strTimeStamp & @TAB & $strInput
			Else
				$strLine = $strInput
			EndIf
			If (StringRight($strLine, 2) = @CRLF) Then
				$strLine = StringReplace(StringTrimRight($strLine, 2), @CRLF, @CRLF & $strTimeStamp & @TAB) & @CRLF
				$bAddAtBeginningOfLine = True
			Else
				$strLine = StringReplace($strLine, @CRLF, @CRLF & $strTimeStamp & @TAB)
				$bAddAtBeginningOfLine = False
			EndIf
			ConsoleWrite($strLine)
			Sleep(25)
		EndIf
	WEnd
	Return 0
EndFunc

; ---------- MAIN ---------- ---------- ---------- ---------- ---------- ---------- ---------- ----------

_ParseCommandLine()
$intErrorLevel = _Main()
Exit $intErrorLevel

Open in new window

0
 

Author Closing Comment

by:lrbrister
Comment Utility
That's it!

Thanks
0
 
LVL 82

Expert Comment

by:oBdA
Comment Utility
Just noticed one slight error, sorry: swap lines 61 and 62, so that Sleep(25) is directly before "WEnd" instead of "EndIf".
#Region ;**** Directives created by AutoIt3Wrapper_GUI ****
#AutoIt3Wrapper_UseUpx=n
#AutoIt3Wrapper_Compile_Both=y
#AutoIt3Wrapper_UseX64=y
#AutoIt3Wrapper_Change2CUI=y
#AutoIt3Wrapper_Res_requestedExecutionLevel=asInvoker
#EndRegion ;**** Directives created by AutoIt3Wrapper_GUI ****
#cs
AddDateToStdout.au3
Script for AutoIt (http://www.autoitscript.com/)
Reads from another process's stdout and adds a timestamp at the beginning of each line.

Tested with AutoIt v3.3.8.1; use Aut2exe.exe or the bundled SciTE to compile this script into an executable.
#ce
If Not @Compiled Then
	MsgBox(0, "", "This script must be compiled as Console application and data piped to it in order to properly demonstrate its functionality.")
	Exit -1
EndIf

AutoItSetOption("MustDeclareVars", 1)
AutoItSetOption("TrayIconHide", 1)

Global $intErrorLevel

Func _ParseCommandLine()
Local $strSyntax, $i
	If ($CmdLine[0] <> 0) Then
		$strSyntax = @CRLF & _
			"Reads stdout from another process and adds a time stamp at the beginning of each line." & @CRLF & _
			"Example: ping localhost | " & @ScriptName & @CRLF & _
			"To capture stderr as well, redirect the process's stderr to stdout by using '2>&1'." & @CRLF & _
			"Example: dir sdfsdf 2>&1 | " & @ScriptName & @CRLF
		ConsoleWrite($strSyntax)
		Exit 1
	EndIf
EndFunc

Func _Main()
Local $strInput, $strLine, $strTimeStamp
Local $bAddAtBeginningOfLine = True
	While True
		$strInput = ConsoleRead()
		If @error Then
			ExitLoop
		EndIf
		If (@extended > 0) Then
			$strTimeStamp = @MON & @MDAY & @YEAR & " " & @HOUR & ":" & @MIN & ":" & @SEC
			If $bAddAtBeginningOfLine Then
				$strLine = $strTimeStamp & @TAB & $strInput
			Else
				$strLine = $strInput
			EndIf
			If (StringRight($strLine, 2) = @CRLF) Then
				$strLine = StringReplace(StringTrimRight($strLine, 2), @CRLF, @CRLF & $strTimeStamp & @TAB) & @CRLF
				$bAddAtBeginningOfLine = True
			Else
				$strLine = StringReplace($strLine, @CRLF, @CRLF & $strTimeStamp & @TAB)
				$bAddAtBeginningOfLine = False
			EndIf
			ConsoleWrite($strLine)
		EndIf
		Sleep(25)
	WEnd
	Return 0
EndFunc

; ---------- MAIN ---------- ---------- ---------- ---------- ---------- ---------- ---------- ----------

_ParseCommandLine()
$intErrorLevel = _Main()
Exit $intErrorLevel

Open in new window

0
 

Author Comment

by:lrbrister
Comment Utility
oBdA
I caught that but thanks
0

Featured Post

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Suggested Solutions

This is about my first experience with programming Arduino.
If you’re thinking to yourself “That description sounds a lot like two people doing the work that one could accomplish,” you’re not alone.
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

763 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

7 Experts available now in Live!

Get 1:1 Help Now