Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Command Prompt text to file with inserted text

Posted on 2013-06-11
11
Medium Priority
?
627 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
ID: 39238431
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
ID: 39238463
netballi,
  Thanks.  At least I have an answer
0
 
LVL 43

Expert Comment

by:Steve Knight
ID: 39238467
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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
LVL 59

Expert Comment

by:Bill Prew
ID: 39238741
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
ID: 39242328
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
 
LVL 43

Expert Comment

by:Steve Knight
ID: 39242427
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
ID: 39245008
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 85

Accepted Solution

by:
oBdA earned 2000 total points
ID: 39247723
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
ID: 39247808
That's it!

Thanks
0
 
LVL 85

Expert Comment

by:oBdA
ID: 39247862
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
ID: 39248002
oBdA
I caught that but thanks
0

Featured Post

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

Question has a verified solution.

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

In this post we will learn different types of Android Layout and some basics of an Android App.
Q&A with Course Creator, Mark Lassoff, on the importance of HTML5 in the career of a modern-day developer.
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…
Introduction to Processes

916 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