Jump to content

Running a batch file in AutoIT


Recommended Posts

Alternatively you could just run devcon and capture what it's return was:

#include <Constants.au3>

$PID = Run("devcon rescan", "C:\Windows\System32", @SW_HIDE, $STDERR_CHILD + $STDOUT_CHILD)

While ProcessExists($PID)
    $line = StdoutRead($PID)
    ConsoleWrite($line & @CRLF)
    If @error Then ExitLoop
Wend

[sub]Quantum mechanics: The dreams stuff is made of[/sub]

Link to comment
Share on other sites

  • Replies 47
  • Created
  • Last Reply

Top Posters In This Topic

Top Posters In This Topic

The issue you are having is that you are running a DOS command that requires 8.3 path settings. Documents not recognized is pointing to Documents and Settings. If you are going to run anything in your Documents and Settings directory then you need to use DOS 8.3 path settings.

Docume~1 = Documents and Settings

If you have a folder that contains a period then you need to use the full name (i.e. John.Smith versus John.S~1)

So, paths for

C:\Documents and Settings\John.Smith would appear as:

C:\Docume~1\John.Smith\etc.\etc.

Trust me, this is your issue.

Here is a function that I wrote to work with DOS 8.3 path settings:

$path = @ScriptDir & "\Software\test.bat" 
$sPath = _input($path)
Run(@ComSpec & " /k " & Chr(34) & $sPath & Chr(34), @ScriptDir)

; ## _input() function
; ## Check our path variable and see if it is longer than 8 characters
; ## If it is, apply 8.3 dos short path structure to it
; ## In addition, if a directory contains a period, ignore the short directory
; ## and use the full folder name (ex. john.collins instead of john.c~1)
; ## Set our input and output directory path formats here.
Func _input($var)
    Local $len, $leftbuild, $rightbuild, $newpath
    $oSplit = StringSplit($var, '\', 1)
    For $i = 1 To $oSplit[0]
        $len = StringLen($oSplit[$i])
        If $len > 8 Then
            If StringInStr($oSplit[$i], ".") <> 0 Then
                $newpath = $newpath & $oSplit[$i] & "\"
            Else
                $leftbuild = StringLeft($oSplit[$i], 6)
                $rightbuild = "~1"
                $newpath = $newpath & $leftbuild & $rightbuild & "\"
            EndIf
        Else
            ; ## If the root of C:\ is chosen, format so C:\ doesn't return as C:\\
            If $oSplit[$i] = "" Then
                $newpath = $newpath & $oSplit[$i]
            Else
                $newpath = $newpath & $oSplit[$i] & "\"
            EndIf
        EndIf
    Next
    $var = $newpath
    Return $var
EndFunc   ;==>_input

I added an example of how to use the function I wrote. Try it out.

Edited by Ealric

My Projects: [topic="89413"]GoogleHack Search[/topic], [topic="67095"]Swiss File Knife GUI[/topic], [topic="69072"]Mouse Location Pointer[/topic], [topic="86040"]Standard Deviation Calculator[/topic]

Link to comment
Share on other sites

How would I incorporate @scriptdir into this though. Really confused

The issue you are having is that you are running a DOS command that requires 8.3 path settings. Documents not recognized is pointing to Documents and Settings. If you are going to run anything in your Documents and Settings directory then you need to use DOS 8.3 path settings.

Docume~1 = Documents and Settings

If you have a folder that contains a period then you need to use the full name (i.e. John.Smith versus John.S~1)

So, paths for

C:\Documents and Settings\John.Smith would appear as:

C:\Docume~1\John.Smith\etc.\etc.

Trust me, this is your issue.

Here is a function that I wrote to work with DOS 8.3 path settings:

$path = @ScriptDir & "\Software\test.bat" 
$sPath = _input($path)
Run(@ComSpec & " /k " & Chr(34) & $sPath & Chr(34), @ScriptDir)

; ## _input() function
; ## Check our path variable and see if it is longer than 8 characters
; ## If it is, apply 8.3 dos short path structure to it
; ## In addition, if a directory contains a period, ignore the short directory
; ## and use the full folder name (ex. john.collins instead of john.c~1)
; ## Set our input and output directory path formats here.
Func _input($var)
    Local $len, $leftbuild, $rightbuild, $newpath
    $oSplit = StringSplit($var, '\', 1)
    For $i = 1 To $oSplit[0]
        $len = StringLen($oSplit[$i])
        If $len > 8 Then
            If StringInStr($oSplit[$i], ".") <> 0 Then
                $newpath = $newpath & $oSplit[$i] & "\"
            Else
                $leftbuild = StringLeft($oSplit[$i], 6)
                $rightbuild = "~1"
                $newpath = $newpath & $leftbuild & $rightbuild & "\"
            EndIf
        Else
            ; ## If the root of C:\ is chosen, format so C:\ doesn't return as C:\\
            If $oSplit[$i] = "" Then
                $newpath = $newpath & $oSplit[$i]
            Else
                $newpath = $newpath & $oSplit[$i] & "\"
            EndIf
        EndIf
    Next
    $var = $newpath
    Return $var
EndFunc   ;==>_input

I added an example of how to use the function I wrote. Try it out.

Link to comment
Share on other sites

How would I incorporate @scriptdir into this though. Really confused

@ScriptDir is the directory you are executing your script from. I put the example at the top. Just edit the $path variable.

My Projects: [topic="89413"]GoogleHack Search[/topic], [topic="67095"]Swiss File Knife GUI[/topic], [topic="69072"]Mouse Location Pointer[/topic], [topic="86040"]Standard Deviation Calculator[/topic]

Link to comment
Share on other sites

I added an example of how to use the function I wrote. Try it out.

Sorry but your function is flawed. You cannot guess what the filesystem does with 8.3 filenames. The only sure way is to use the windows api to ask the filesystem.

Run this example against your function and see a possible difference. The Msgbox displays different paths for me.

$folder1 = @HomeDrive & '\foldername 1'
$folder2 = @HomeDrive & '\foldername 2'
DirCreate($folder1)
DirCreate($folder2)
MsgBox(0, '', _input($folder2))
MsgBox(0, '', FileGetShortName($folder2))

There are other posts around that I explain this with links to MSDN of how the filesystem works. The filesystem has the right to set 8.3 filenames as it seems fit with the ~1, ~2, ~3... and it can optionally reuse these indexes at it's own discretion.

Edit:

Look at this post here for more info.

Edited by MHz
Link to comment
Share on other sites

when you say edit the $path variable do you mean

$sPath = _input($path)

and also, what do you mean by edit this, not sure what to put there?.

oh my I really am lost now. Starting to think it would be eaysier to call a vb exe file that runs the batch file.

Edited by jben
Link to comment
Share on other sites

my batch file is this

devcon.exe rescan

and the devcon.exe file is within the same directory as the batch file

Something thats really getting to me is the fact that the devcon.bat file I created works if i manually run it, but I just can't get it to work any other way

I am trying to run devcon as I want to refresh the hardware list. I have reasons for doing this.

Edited by jben
Link to comment
Share on other sites

Jben,

If you want the short answer, use the script from the root of your C: drive in a short folder name:

Create a folder called C:\script and place your files in there and run it. As I explained above the issue is that you were trying to run it from the C:\Documents and Settings and as explained the file system is looking for 8.3 path short file names.

MHz, I understand what you are saying and yes it would be a more concise way if you have many folders with similar short name abbreviations. In this case however, I am right. How many people have more than 2 C:\document... in their root? The chances of that happening are about as common as Microsoft releasing it's source code.

Thanks,

My Projects: [topic="89413"]GoogleHack Search[/topic], [topic="67095"]Swiss File Knife GUI[/topic], [topic="69072"]Mouse Location Pointer[/topic], [topic="86040"]Standard Deviation Calculator[/topic]

Link to comment
Share on other sites

Here is your batch file:

cd\
cls
cd Devcon
cd i386
devcon.exe rescan

Here is your AutoIT file:

RunWait("C:\Devcon\i386\devcon.bat")

This works I just ran it on my PC and no problems.

Now you could elevate the users rights to run it if you need that let me know.

Link to comment
Share on other sites

Here is your batch file:

cd\
cls
cd Devcon
cd i386
devcon.exe rescan

Here is your AutoIT file:

RunWait("C:\Devcon\i386\devcon.bat")

This works I just ran it on my PC and no problems.

Now you could elevate the users rights to run it if you need that let me know.

And, notice your path uses all short names under 8 characters? This is why his script has been failing. He's been trying to run it from his desktop in a folder located there.

My Projects: [topic="89413"]GoogleHack Search[/topic], [topic="67095"]Swiss File Knife GUI[/topic], [topic="69072"]Mouse Location Pointer[/topic], [topic="86040"]Standard Deviation Calculator[/topic]

Link to comment
Share on other sites

MHz, I understand what you are saying and yes it would be a more concise way if you have many folders with similar short name abbreviations. In this case however, I am right. How many people have more than 2 C:\document... in their root? The chances of that happening are about as common as Microsoft releasing it's source code.

All it takes is one folder or file named "Document" or similar. Do not think that it is a 2nd "Documents and DoDaDay". So no, your function id flawed, period. You are not right by any means. :)
Link to comment
Share on other sites

Thanks for that. The only problem I have by creating that folder in the c: drive is that this runs of a disk, hence the reason for running it from the scriptdir

If a user went to run the program on their PC then this folder would not be available. Therefore I guess I would have to copy the batch and exe file to the c: drive.

if its running from the scriptdir then I would have the folder in place as it would be on the disk.

Unless theres a way that I code code this to copy the files to the c: drive, then run the batch file and then delete the folder then i would be fine. Just don't know though

Edited by jben
Link to comment
Share on other sites

Thanks for that. The only problem I have by creating that folder in the c: drive is that this runs of a disk, hence the reason for running it from the scriptdir

If a user went to run the program on their PC then this folder would not be available. Therefore I guess I would have to copy the batch and exe file to the c: drive.

if its running from the scriptdir then I would have the folder in place as it would be on the disk.

Unless theres a way that I code code this to copy the files to the c: drive, then run the batch file and then delete the folder then i would be fine. Just don't know though

I would recommend doing that and using the Temp directory. Most of the scripters here do that with their code.

My Projects: [topic="89413"]GoogleHack Search[/topic], [topic="67095"]Swiss File Knife GUI[/topic], [topic="69072"]Mouse Location Pointer[/topic], [topic="86040"]Standard Deviation Calculator[/topic]

Link to comment
Share on other sites

All it takes is one folder or file named "Document" or similar. Do not think that it is a 2nd "Documents and DoDaDay". So no, your function id flawed, period. You are not right by any means. :)

MHz you don't understand that "I'm agreeing with you".

However, for the purpose of this scenario you are incorrect. Go to the root of your C: drive and try to create a folder named "Documents". See what results you get.

Secondly, Document = 8 characters so it's not affected by my function. So, the folder would have to have Documents.... in it. You cannot create a root folder called C:\Documents because windows will not allow you to on XP. Secondly, if you create a folder called Documents1 and compare it to Documents and Settings, guess which folder gets named Docume~1 first? That's right, the folder with the empty space following it has more priority on "any" filesystem.

Thus, while you are correct in the reasoning and the way to properly do it, my way works 100% in this scenario.

Thanks,

Edited by Ealric

My Projects: [topic="89413"]GoogleHack Search[/topic], [topic="67095"]Swiss File Knife GUI[/topic], [topic="69072"]Mouse Location Pointer[/topic], [topic="86040"]Standard Deviation Calculator[/topic]

Link to comment
Share on other sites

How would i go about copying a file to the temp dir though and then deleting it after?

thanks

Questions:

1. Are you supplying your users with a CD or are you going to place the files on a shared resource?

2. How do you envision the users running this script?

My Projects: [topic="89413"]GoogleHack Search[/topic], [topic="67095"]Swiss File Knife GUI[/topic], [topic="69072"]Mouse Location Pointer[/topic], [topic="86040"]Standard Deviation Calculator[/topic]

Link to comment
Share on other sites

This is what I have done

FileCopy(@ScriptDir & "\Software\Devcon\devcon.bat", "c:\temp\devcon.bat")

FileCopy(@ScriptDir & "\Software\Devcon\devcon.exe", "c:\temp\devcon.exe")

Runwait ("c:\temp\devcon.bat")

FileDelete ("c:\temp\devcon.bat")

FileDelete ("c:\temp\devcon.exe")

and it works. Do you think that will be fine, or do i need some sort of error checking. Thanks

Link to comment
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
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...