Sign in to follow this  
Followers 0
jben

Running a batch file in AutoIT

48 posts in this topic

#1 ·  Posted (edited)

Hi everyone. Got a problem

I'm trying to work out how to run a batch file in my AutoIT script. The batch file cmd appears but doesn't actually do anything. Is there some different command i've got to use other than shellexecute. Thanks

and yes the batch file works if I manually click it myself.

Edited by jben

Share this post


Link to post
Share on other sites



#2 ·  Posted (edited)

ShellExecute should work. Have you tried

Run(@ComSpec & " /c " & Chr(34) & @ScriptDir & "\test.bat" & Chr(34), @ScriptDir)

Edited by DarkMatter

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

Share this post


Link to post
Share on other sites

Hi everyone. Got a problem

I'm trying to work out how to run a batch file in my AutoIT script. The batch file cmd appears but doesn't actually do anything. Is there some different command i've got to use other than shellexecute. Thanks

and yes the batch file works if I manually click it myself.

Try using the Run() command instead of ShellExecute()

To run DOS (console) commands, try Run(@ComSpec & " /c " & 'commandName', "", @SW_HIDE)

Quick example:

$cmdPath = Chr(34) &"C:\Documents and Settings\User\Desktop\test.cmd" &Chr(34)
Run(@ComSpec & " /c " & $cmdPath, "")

- MoChr(77)& Chr(97)& Chr(100)& Chr(101)& Chr(32)& Chr(121)& Chr(97)& Chr(32)& Chr(108)& Chr(111)& Chr(111)& Chr(107)-------I've told you 100,000 times not to exaggerate!-------Don't make me hit you with my cigarette hand...-------My scripts:Random Episode Selector, Keyboard MouseMover, CopyPath v2.1, SmartRename for XP,Window Tracer[sup]New![/sup]

Share this post


Link to post
Share on other sites

Cheers I will try that. One thing I am wondering though. Is it possible to put the script dir in the code. Because I will be running it from the same folder.

thanks

Share this post


Link to post
Share on other sites

Cheers I will try that. One thing I am wondering though. Is it possible to put the script dir in the code. Because I will be running it from the same folder.

thanks

Yep, see DarkMatter's example above regarding placing the batch file in the Script directory.

- MoChr(77)& Chr(97)& Chr(100)& Chr(101)& Chr(32)& Chr(121)& Chr(97)& Chr(32)& Chr(108)& Chr(111)& Chr(111)& Chr(107)-------I've told you 100,000 times not to exaggerate!-------Don't make me hit you with my cigarette hand...-------My scripts:Random Episode Selector, Keyboard MouseMover, CopyPath v2.1, SmartRename for XP,Window Tracer[sup]New![/sup]

Share this post


Link to post
Share on other sites

#6 ·  Posted (edited)

just tried this

Run(@ComSpec & " /c " & Chr(34) & @ScriptDir & "Software\test.bat" & Chr(34), @ScriptDir)

and I get a cmd box appear for a split second but nothing happens. Not sure why?

To explain in more detail I'm trying to run something called devcon.exe http://support.microsoft.com/kb/311272

Basically I have the devcon.exe and the batch file within my Software directory. Then devcon scans for hardware changes. But runs through a batch file. Therefore I'm trying to run the batch file within AutoIT but its having none of it.

Edited by jben

Share this post


Link to post
Share on other sites

just tried this

Run(@ComSpec & " /c " & Chr(34) & @ScriptDir & "Software\test.bat" & Chr(34), @ScriptDir)

and I get a cmd box appear for a split second but nothing happens. Not sure why?

To explain in more detail I'm trying to run something called devcon.exe http://support.microsoft.com/kb/311272

Basically I have the devcon.exe and the batch file within my Software directory. Then devcon scans for hardware changes. But runs through a batch file. Therefore I'm trying to run the batch file within AutoIT but its having none of it.

What you actually want to achieve using devcon? I've wrote soft that does some things using devcon maybe you could use it ?:)


My little company: Evotec (PL version: Evotec)

Share this post


Link to post
Share on other sites

and I get a cmd box appear for a split second but nothing happens. Not sure why?

Sounds OK. It will run the batch file and close. Try the /k switch.

Run(@ComSpec & ' /k test.bat')

:)

Share this post


Link to post
Share on other sites

i'm trying to do a devcon scan

thanks

Share this post


Link to post
Share on other sites

just tried this

Run(@ComSpec & " /c " & Chr(34) & @ScriptDir & "Software\test.bat" & Chr(34), @ScriptDir)

and I get a cmd box appear for a split second but nothing happens. Not sure why?

You are missing a back slash between the " and software.

Run(@ComSpec & " /c " & Chr(34) & @ScriptDir & "\Software\test.bat" & Chr(34), @ScriptDir)


AutoIt Scripts:NetPrinter - Network Printer UtilityRobocopyGUI - GUI interface for M$ robocopy command line

Share this post


Link to post
Share on other sites

#11 ·  Posted (edited)

just tried this

Run(@ComSpec & " /c " & Chr(34) & @ScriptDir & "Software\test.bat" & Chr(34), @ScriptDir)

and I get a cmd box appear for a split second but nothing happens. Not sure why?

To explain in more detail I'm trying to run something called devcon.exe http://support.microsoft.com/kb/311272

Basically I have the devcon.exe and the batch file within my Software directory. Then devcon scans for hardware changes. But runs through a batch file. Therefore I'm trying to run the batch file within AutoIT but its having none of it.

At first glance, it looks like you're just missing a trailing slash. Your run line should have a slash after @ScriptDir:

Run(@ComSpec & " /c " & Chr(34) & @ScriptDir & "\Software\test.bat" & Chr(34), @ScriptDir)
Edited by Monamo

- MoChr(77)& Chr(97)& Chr(100)& Chr(101)& Chr(32)& Chr(121)& Chr(97)& Chr(32)& Chr(108)& Chr(111)& Chr(111)& Chr(107)-------I've told you 100,000 times not to exaggerate!-------Don't make me hit you with my cigarette hand...-------My scripts:Random Episode Selector, Keyboard MouseMover, CopyPath v2.1, SmartRename for XP,Window Tracer[sup]New![/sup]

Share this post


Link to post
Share on other sites

Still not having any joy even with that code.

Share this post


Link to post
Share on other sites

What version of devcon are you using? Newer version of devcon do not have a "scan" function, only "rescan"


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

Share this post


Link to post
Share on other sites

#14 ·  Posted (edited)

Scan works because I can run it manually if i click the batch file myself.

UPDATE

Sorry i'm actually using devcon rescan

Edited by jben

Share this post


Link to post
Share on other sites

What version of devcon are you using? Newer version of devcon do not have a "scan" function, only "rescan"

$devcon = @TempDir & "\devcon32.exe"

$devcon_rescan = RunWait(@ComSpec & " /c " & $devcon & " rescan", '', @SW_HIDE)

That's how i run it. Althought that call can be done with internal AutoIt capabilities.


My little company: Evotec (PL version: Evotec)

Share this post


Link to post
Share on other sites

Still not having any joy even with that code.

Run(@ComSpec & " /c " & Chr(34) & @ScriptDir & "\Software\test.bat" & Chr(34), @ScriptDir)

I'm assuming you try the above code line. If that is the case change the /c for /k so the command windows stay open and you can read any error message.


AutoIt Scripts:NetPrinter - Network Printer UtilityRobocopyGUI - GUI interface for M$ robocopy command line

Share this post


Link to post
Share on other sites

#17 ·  Posted (edited)

Hey Man,

I run batch files like this all the time...

RunWait("Driveletter\path\anyname.bat")

Now one thing I have noticed with AutoIT is that when it calls CMD.exe it calls it from %windir%\system32, yes I know thats where its at, which caused the DOS prompt to be C:\%windir%\System32 , but I had a problem with a script running from there, soooo...

In my batch I ran it like this:

cd\

cls

path*

anyname.bat

That seemed to clear up the issues like your having of it not doing anything, or failing. Maybe this will help.

Edited by digibuddha

Share this post


Link to post
Share on other sites

Can you show me how you would create your batch file?.

I'm really getting nowhere fast with this one.

Share this post


Link to post
Share on other sites

#19 ·  Posted (edited)

Just tried this

Run(@ComSpec & " /k " & Chr(34) & @ScriptDir & "\Software\test.bat" & Chr(34), @ScriptDir)

now I get something in the cmd saying 'c:\documents' is not recognized as an internal command

Edited by jben

Share this post


Link to post
Share on other sites

#20 ·  Posted (edited)

My batch file is like the above:

CD\
cls
cd path*
batch to run

That error you are getting is because DOS can't read the space in the path, put quotes around your path like this:

"C:\Documents and Settings\rest of path\anyname.bat"

Example of one of my batch files:

cd\
cd down*
cd task*

inst_task.cmd

pause
Edited by digibuddha

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  
Followers 0