Jump to content
Sign in to follow this  
ttleser

StdOutRead & Windows XP

Recommended Posts

ttleser

Greetings all.

Need some help understanding what's happening here. I've got a simple script that will determine if a file is encrypted or not, but I'm having issues with it working in Windows XP however it works fine in Windows 7. To find out if a file is encrypted is an easy thing to find out. If you open up a CMD prompt, change to the directory with the file and type "cipher FILENAME" it will report back whether or not it's encrypted:

C:\Users\c0006767\Desktop>cipher test2.txt

Listing C:\Users\c0006767\Desktop\

New files added to this directory will be encrypted.

E test2.txt

C:\Users\c0006767\Desktop>

The "E" in front of the return data indicated it's "E"ncrypted, a "U" would indicate "U"nencrypted. This command works fine on both Windows XP and Windows 7. Cipher.exe has been part of Windows since Windows 2000. My script works great in Windows 7, however in Windows XP it's cutting off the "E test2.txt" part of the output, so the return looks like this:

Listing C:\Users\c0006767\Desktop\

New files added to this directory will be encrypted.

Again, if I open up a CMD prompt and try it that way directly, it displays what it should. I've tried different ways for the while loop, while 1 v.s. while processexists($foo), netiher seem to help Windows XP. For both operating systems, I'm an administrator. Here's the script:

#include <Constants.au3>
#RequireAdmin

$Foo = Run(@ComSpec & ' /c cipher.exe "test2.txt"', "c:\", @SW_HIDE, $STDERR_CHILD + $STDOUT_CHILD)
Local $line2
$line2 = ""
$Temp2 = ""
While 1
$line2 &= StdoutRead($Foo)
If @error Then ExitLoop
WEnd
While 1
$line2 &= StderrRead($Foo)
If @error Then ExitLoop
WEnd
Sleep(3000)
msgbox(0,$What,$line2,1)

Thoughts?

Share this post


Link to post
Share on other sites
ttleser

Really? What does it show? You do need the extension in the filename.

If you type "cipher" at the CMD prompt in....say... your documents directory, it'll show you all the files and folders (not subfolders) and their encrypted/unencrypted state. We're running "professional" at work, however this works for Windows 7 home edition too.

So:

c:

cdusersYOURIDDesktop

Cipher

Edited by ttleser

Share this post


Link to post
Share on other sites
JohnOne

I have no idea why it would not show the extra output, but perhaps a workaround might be to adapt something similar to this.

$PID = Run("cipher.exe file.ext", "c:\", @SW_HIDE, $STDERR_MERGED)

While ProcessExists($PID)
WEnd

If StringInStr(StdoutRead($PID,1), "not") Then
    MsgBox(0, 0, "Not Encrypted")
Else
    MsgBox(0, 1, "Encrypted")
EndIf
Edited by JohnOne

AutoIt Absolute Beginners    Require a serial    Pause Script    Video Tutorials by Morthawt   ipify 

Monkey's are, like, natures humans.

Share this post


Link to post
Share on other sites
ttleser

That doesn't work either. It was reporting the file as not encrypted when it actually is encrypted. So I rem'd out the IF section and did:

msgbox(0,"test",stdoutread($PID,1)) instead and again it's cutting off the bottom.

The $stderr_merged or $stdout_child.... can someone explain how it works exactly? I've used stdoutread before, but I don't really understand how it works. Does specifying $stdout_child in the run line state that I'm intending to look at the output at some point, so have the data in memory until I need it (stdoutread in a loop)??

Share this post


Link to post
Share on other sites
ttleser

I got it to output what you expected it to on win 7.

I needed to make sure the working dir in Run() was the same as the file being tested.

I got it to work fine in Windows 7... the problem is that Windows XP isn't behaving the same. Windows XP is the one that's truncating the last few lines of the output.

Share this post


Link to post
Share on other sites
JohnOne

Are all the files you are testing in the root of c drive?

EDIT:

Also, I edit that code, cause I'd originally pasted it like this.

If StringInStr(StdoutRead($PID,1), "not") Then
    MsgBox(0, 0, "Not Encrypted")
Else
    MsgBox(0, 1, "Not Encrypted")
EndIf
Edited by JohnOne

AutoIt Absolute Beginners    Require a serial    Pause Script    Video Tutorials by Morthawt   ipify 

Monkey's are, like, natures humans.

Share this post


Link to post
Share on other sites
ttleser

No. The files I'm testing are on my desktop which my code allows for.

Share this post


Link to post
Share on other sites
ttleser

Again, keep in mind this problem is happening on Windows XP only, Windows 7 is fine.

OK, got an update on this.

I tried my script again, but changed it so that the run command would look like this:

$foo = Run(@Comspec & "/c cipher.exe test2.txt >> c:output.txt","c:",@SW_HIDE)

Then later in the code I did

_FileReadtoArray("c:output.txt",$Array)

_ArrayDisplay($Array)

My thought was to have DOS output the results of the cipher command to a text file then I would just read that text file and see what it said. The Array showed the same results as previous attempts:

Listing C:

New files added to this directory will be encrypted.

However, get this. If I use notepad to open the output.txt file, it shows what I would have expected it to show:

Listing C:

New files added to this directory will not be encrypted.

E test2.txt

Why is it that _FileReadToArray didn't pick up on the extra lines?... Once I opened the output.txt in Notepad and re-saved it... the _FileReadToArray did pick up on the last lines.

Share this post


Link to post
Share on other sites
ttleser

Show how your code allows for it.

Tried:

$Foo = Run(@ComSpec & ' /c cipher.exe "test2.txt"', "c:", @SW_HIDE, $STDERR_CHILD + $STDOUT_CHILD)

$Foo = Run(@ComSpec & ' /c cipher.exe "c:test2.txt"', "c:", @SW_HIDE, $STDERR_CHILD + $STDOUT_CHILD)

The working directory in this example is c: which is where the test2.txt file is stored which should suffice... However, I had also tried doing '/c cipher.exe "c:test2.txt"' as well, results were the same.

Share this post


Link to post
Share on other sites
BrewManNH

You still aren't showing how your code allows for the files reside on the desktop and this script accomdates that. Which I believe JohnOne was asking about.


If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

Share this post


Link to post
Share on other sites
JohnOne

try with FileChangeDir()

#include <Constants.au3>

$FileDir = "C:\"
ConsoleWrite("-Changing Working Directory To - " & $FileDir & @LF)
FileChangeDir($FileDir)
ConsoleWrite("+Working Directory Is - " & @WorkingDir & @LF)

$PID = Run("cipher.exe list.txt", @WorkingDir, @SW_HIDE, $STDERR_MERGED)

ProcessWaitClose($PID)

$_StdoutRead = StdoutRead($PID)
ConsoleWrite($_StdoutRead & @LF)

If StringInStr($_StdoutRead, "not") Then
    MsgBox(0, 0, "Not Encrypted")
Else
    MsgBox(0, 1, "Encrypted")
EndIf

AutoIt Absolute Beginners    Require a serial    Pause Script    Video Tutorials by Morthawt   ipify 

Monkey's are, like, natures humans.

Share this post


Link to post
Share on other sites
ttleser

try with FileChangeDir()

#include <Constants.au3>

$FileDir = "C:\"
ConsoleWrite("-Changing Working Directory To - " & $FileDir & @LF)
FileChangeDir($FileDir)
ConsoleWrite("+Working Directory Is - " & @WorkingDir & @LF)

$PID = Run("cipher.exe list.txt", @WorkingDir, @SW_HIDE, $STDERR_MERGED)

ProcessWaitClose($PID)

$_StdoutRead = StdoutRead($PID)
ConsoleWrite($_StdoutRead & @LF)

If StringInStr($_StdoutRead, "not") Then
MsgBox(0, 0, "Not Encrypted")
Else
MsgBox(0, 1, "Encrypted")
EndIf

John, I tried the script you just posted and got the same result. So you guys know, I'm ignoring the If StringinStr portion of the script causing it's looking for text that isn't what I need. I think you're not understanding what part in the output I'm interested in. In the output example listed below (the correct output), the Red text is what I'm interested in. The Red text is what is being cut off on Windows XP only. The text that says "New files added to this directory will not be encrypted" isn't what I'm interested in.

Listing C:\

New files added to this directory will not be encrypted.

E Test2.txt

The console output shows:

Listing C:\

New files added to this directory will not be encrypted.

which it should show:

Listing C:\

New files added to this directory will not be encrypted.

E Test2.txt

The "E" before Test2.txt means the file is encrypted, it would be a "U" if it weren't. I've attached a screenshot of my Windows XP desktop that shows a DOS window where I ran the cipher command (by itself it shows all files in the current directory and their encryption status) and cipher 1.txt that shows the encryption status of just that file. The screenshot also shows John's code (with filename change) with it's output. it also shows the files in c:\ in Windows explorer (note the file is green, which means it's encrypted).

post-7414-0-98020600-1363033502_thumb.jp

Share this post


Link to post
Share on other sites
ttleser

Any thoughts as to why I can have the output redirected to a file in DOS. You can open the file in notepad and view the "E test2.bat" but if you pull in the contents of the file in AutoIT, you can't see "E test2.bat" unless you had saved it in notepad?

Could it be that the data in the text file as well as the stream in AutoIT is.... I'm going to fumble around on this..... is in a different format (UTF 16, ASCI, Binary) or something that AutoIT can't pick up on? I'm grasping at straws as to why that part of the stream isn't readable. Any thoughts on this?

Share this post


Link to post
Share on other sites
BrewManNH

I ran the VBScript below on XP and I don't get the file name using VBScript either, so it appears to be an XP issue with StdOutRead.

Wscript.echo execStdOut("cipher.exe ""C:\Windows\Media\Windows XP Balloon.wav""")

Function execStdOut(cmd)
   Dim goWSH : Set goWSH = CreateObject( "WScript.Shell" )
   Dim aRet: Set aRet = goWSH.exec(cmd)
   execStdOut = aRet.StdOut.ReadAll()
End Function

EDIT: BTW, if you run the command line (minus the doubled quotation marks) in the command console, you get the full output with the file name in the console window.

Edited by BrewManNH

If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

Share this post


Link to post
Share on other sites
MHz

Piping to more gets all of the stream.

Tested with this code on Win XP Pro

FileWrite('c:\test2.txt', '')
$Foo = Run('cmd /c cipher.exe test2.txt|more', 'c:\', @SW_HIDE, 8); merged
Global $line2
Do
    $line2 &= StdOutRead($Foo)
Until @error
MsgBox(0, @ScriptName, $line2)

#cs Output with excessive empty lines removed
---------------------------
ttleser.au3
---------------------------
Listing c:\

New files added to this directory will not be encrypted.

U test2.txt
---------------------------
OK
---------------------------
#ce

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×