Solved

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

Posted on 2007-11-29
9
6,731 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
[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
  • 4
  • 3
9 Comments
 
LVL 25

Expert Comment

by:InteractiveMind
ID: 20376263
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
ID: 20376277
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
ID: 20382382
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
Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

 

Accepted Solution

by:
mjbtec earned 0 total points
ID: 20382844
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
 
LVL 25

Expert Comment

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

Author Comment

by:mjbtec
ID: 20405679
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
ID: 20405901
Nice one, thank you.
0
 
LVL 1

Expert Comment

by:modus_operandi
ID: 20412434
Closed, 250 points refunded.
modus_operandi
EE Moderator
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

A short article about problems I had with the new location API and permissions in Marshmallow
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
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…
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…

630 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