Solved

How do I append DOS output to a file from an AutoIt script?

Posted on 2007-11-29
9
6,441 Views
Last Modified: 2008-02-01
I've created an AutoIt script that gives me quick access to commonly used programs I use to perform routine maintenance on the computers at the office where I work.

One function I use is the NET TIME command so I can verify that the computer is set to receive the time from our server. I am trying to record the output of this command to a log file for future reference but my script is not logging it.

I have these variables initialized at the beginning of my script:

$path="\\lrecdops-dc3\d$\home_folders\michael\maintenance\"
$file=$path & @ComputerName & ".txt"

And here is the code from the segment that runs the NET TIME command:

RunWait("net time >> " & $file)
FileOpen ($file, 1 )
FileWriteLine($file, _Now() & " - Ran NET TIME check" & @LF)
FileClose ($file)      

When I run the script, the DOS window flashes, but when I check the log all I see is the line saying it ran the NET TIME check.  

How do I get the script to append the NET TIME output to the log file?
0
Comment
Question by:mjbtec
  • 4
  • 3
9 Comments
 
LVL 25

Expert Comment

by:InteractiveMind
Comment Utility
If there's any whitespace in the filename, then you won't get the required output. What you want to do, is wrap quotation marks around the file name.

I'm not familiar with the AutoIT language, so you'll have to help me a bit here:
How do you include quotation marks inside a string?

In most other languages, you prepend a backslash, like so:

RunWait("net time >> \"" & $file & "\"")

But based solely on the code you've presented above, it would seem unlikely that this is the correct syntax in AutoIT..
0
 
LVL 25

Expert Comment

by:InteractiveMind
Comment Utility
Futhermore, it would seem that the net time application writes it's output to a different stream; try this instead:

RunWait("net time 2> " & $file)
0
 

Author Comment

by:mjbtec
Comment Utility
Well, this is getting quite interesting.

When I run my script and invoke the NET TIME command first, it works fine.

RunWait(@ComSpec & " /k " & "net time >>" & $file)            

BUT...

When I have the script write anything to the log file and then try invoking the NET TIME command, I get this error:

The process cannot access the file because it is being used by another process.

That's what was in the DOS window when it flashed. Normally NET TIME takes a few seconds to run.

I think the script is somehow keeping the log file open so NET TIME can't write anything to it.

I'm taking this to the forums on AutoIt's web site and will come back here when I find something out.
0
 

Accepted Solution

by:
mjbtec earned 0 total points
Comment Utility
Someone at the AutoIt forums suggested that I close the handle created by FileOpen() function. This fixed my problem.

Here's the modified code that works:



$FileOpen = FileOpen ($file, 1)

FileWriteLine($file,"Output of NET TIME check:" & @LF)

FileClose ($FileOpen)

RunWait(@ComSpec & " /k " & "net time >>" & $file)	

Open in new window

0
What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

 
LVL 25

Expert Comment

by:InteractiveMind
Comment Utility
Excellent, thanks for letting us know the solution.
I'll have this thread PAQ'd, and your points refunded.
0
 

Author Comment

by:mjbtec
Comment Utility
To answer your question about inserting quotes in a string, I use the CHR function to load its corresponding ASCII code, which is 43.

So if I wanted to use quotes in this string:

net time >> \"" & $file & "\"

I would use

net time >> \" & chr(34) & " & $file & " & chr(34) & "\
0
 
LVL 25

Expert Comment

by:InteractiveMind
Comment Utility
Nice one, thank you.
0
 
LVL 1

Expert Comment

by:modus_operandi
Comment Utility
Closed, 250 points refunded.
modus_operandi
EE Moderator
0

Featured Post

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.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Modbus - whats the maximum I can store in one register? 4 74
matchUp  challenge 9 71
count7 challenge 12 67
powerN  challenge 3 47
This article is meant to give a basic understanding of how to use R Sweave as a way to merge LaTeX and R code seamlessly into one presentable document.
Displaying an arrayList in a listView using the default adapter is rarely the best solution. To get full control of your display data, and to be able to refresh it after editing, requires the use of a custom adapter.
An introduction to basic programming syntax in Java by creating a simple program. Viewers can follow the tutorial as they create their first class in Java. Definitions and explanations about each element are given to help prepare viewers for future …
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

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

12 Experts available now in Live!

Get 1:1 Help Now