Jump to content

From .bat to Autoit.


Recommended Posts

Hi guys,

I'm BRAND NEW to AutoIt. I discovered it while looking for a solution to automate the running of a wireless network installation script. Somebody had already written it, and I all I had to do was copy it and compile it with a few basic changes. It worked out REAAALY well, and got me excited about the prospect of learning AUTOIT to expand my abilities for certain issues I've been having.

I've got a small amount of regular .bat scripting and Linux shell scripting, but I'm completely new to the AutoIT language (or any REAL programming languages, for that matter.)

I've done a bit of digging through the help files, but I'm having trouble finding any command definitions, etc.

What I'm looking for is sort of a cross-reference from standard windows commands to AUTOit.

For example, in command, I could say,

"IF EXIST "%PROGRAMFILES(X86)%" (GOTO 64BIT) ELSE (GOTO 32BIT)

Which would send it to a different header in the script, depending on what the result was for 64 bit or 32 bit architecture.

Eg:

:32bit and
Do a, b, c

:64bit
Do x, y, z

And have separate commands for operations to complete for the specific architecture, then at the end of each series of operations, have another "goto" command send them to a new header.

How would one redirect the 'script' to a certain header in the script based on such a variable?

Also, is there a basic glossary of commands?

Can I run installers from network paths? such as

(msiexec /i ("\\server\installers\program.exe" /quiet)
?

Example of one thing I'd like to convert from a .bat file, and make into a workable autoit script that I can run hidden without the possibility of somebody closing the script window mid-way through the update:

[CODE]"IF EXIST "%PROGRAMFILES(X86)%" (GOTO 64BIT) ELSE (GOTO 32BIT)

:64BIT
ECHO 64BIT...
IF EXIST "C:\Program Files (x86)\old version of program (MsiExec.exe /X{oldversionuninstaller} /quiet)
If Not Exist "C:\Program Files (x86)\old version of program\" ("\\server\installers\x86installer.exe" /quiet)
GOTO Start64

:32BIT

:32bit
REM check for old version of program, and remove if it already exists.
If exist c:\programfiles\old version of program (MsiExec.exe /X{oldversionuninstaller} /quiet)
REM then to install new version
("\\server\installers\x86installer.exe" /quiet)
If Not Exist If exist c:\programfiles\old version of program ("\\server\installers\x86installer.exe" /quiet)
GOTO Start32

:Start32
START /wait "Title" "c:\programfiles\newversion\run.exe"
GOTO End

:Start64
rem start program and wait for it to exit
START /wait "Title" "c:\programfiles\newversion\run.exe"
GOTO: End

:End
endlocal
shutdown -f -r -t 03 -c "Shutdown and Restart after exiting app"

This is just an example for a generalized script that I have for my environment, where I need to run an updated installer that would remove any older versions, then launch the new program upon completion of install, then shutdown and reboot the machine after the end user closes out that particular program. I manage several hundred machines, and updates are usually pretty smooth, but there's always that one idiot who closes the command window while the script is running, and never gets the update installed. Going to a completely invisible script, or even locking user input using the BlockInput command is my primary reason for wanting to go ahead and learn a 'real' language.

If you guys who know batch commands could give me some pointers of what these sorts of commands would equate to in AutoIT, that would be really helpful. I was wishing that I could directly copy basic windows commands over and have it compile and execute it, but I knew before I tried it, it was probably a pipe dream. :/

Thanks so much in advance! Sorry to be such a n00b.

Link to comment
Share on other sites

To check the os arch see this macro @OSArch:

If @OSArch = "X64" Then
    _X64()
Else
    _X86()
EndIf

To check if a file/folder exist look for FileExists:

If FileExists("C:\autoexec.bat") Then
    MsgBox(4096, "C:\autoexec.bat File", "Exists")
Else
    MsgBox(4096, "C:\autoexec.bat File", "Does NOT exists")
EndIf

To run new .exe take a look to Run, RunWait.

Run(@WindowsDir & "\notepad.exe")

All you need is in the help file ;)

Hi!

My UDF: NetInfo UDF Play with your network, check your download/upload speed and much more! YTAPI Easy to use YouTube API, now you can easy retrive all needed info from a video. NavInfo Check if a specific browser is installed and retrive other usefull information. YWeather Easy to use Yahoo Weather API, now you can easily retrive details about the weather in a specific region. No-IP UDF Easily update your no-ip hostname(s).

My Script: Wallpaper Changer Change you wallpaper dinamically, you can also download your wallpaper from your website and share it with all!   My Snippet: _ImageSaveToBMPConvert an image to bmp format. _SciteGOTO Open a file in SciTE at specific fileline. _FileToHex Show the hex code of a specified file

Link to comment
Share on other sites

To check the os arch see this macro @OSArch:

If @OSArch = "X64" Then
    _X64()
Else
    _X86()
EndIf

Thanks for the quick reply. I guess I just don't understand what those commands you have actually DO. I'm sure it works, but if I don't know how/why it works, I'm still scratching my head... It's still greek to me.

So do I need to put the entirity of my script operations under the if/else headers, or is there a way to send it to a different header upon completing a few basic operations? Like I said, I'm a COMPLETE n00b for this stuff, so I don't really get how to set up the order of operations. I'm used to just being able to use a "goto" command, and sent it ANYWHERE with relative ease.

To check if a file/folder exist look for FileExists:

If FileExists("C:\autoexec.bat") Then
MsgBox(4096, "C:\autoexec.bat File", "Exists")
Else
MsgBox(4096, "C:\autoexec.bat File", "Does NOT exists")
EndIf
So what does the msgbox command do? Are you just suggesting that I call additional batch files, rather than writing those operations into the autoit script? That's what it LOOKS like to me, the unitiated, and that's the whole thing I'm trying to avoid. Using autoit to call more batch scripts would just add another layer of complexity, and add more windows that are likely to be closed. I guess I could do
Ingoreinput (<1>)
runwait myexistingbatchfile.bat
ignoreinput (<0>)
Runwait \newinstall.exe
ignoreinput (<1>)
runwait \script to initiate reboot.

But that seems like a really sloppy way of getting from point a to point b, allowing user input only while needed...

I'm trying to keep this to virtually silent operation with zero errors or anything to the end user. I just want to be able to push updates across my local network, and have the user not need to know or care that anything happened, until the new program launches and prompts them for some input.

Then once they close that window, I gotta initiate reboot, without the option to cancel.

Run(@WindowsDir & "\notepad.exe")
:( more gobbldygook that I don't understand. What does this do? Why?

All you need is in the help file ;)

That would be wonderful, where the heck is this alleged "the help file"? I can't find it anywhere. : All I can find are wiki articles and such that don't really address nuts and bolts. Mostly just general FAQ. Hell, I can't even seem to locate the forum rules. part of that could be that my attention is divided by about 25 different projects, including a migration to active directory, and can't focus on this for more than a few minutes at a time before I get pulled away to something else. So I apologize if I'm overlooking the obvious. Edited by wrenchmonkey
Link to comment
Share on other sites

That would be wonderful, where the heck is this alleged "the help file"? I can't find it anywhere.

Anytime you type in a AutoIt function, press F1. It will pop-up the helpfile text for that function. It's very nice :)

Spoiler

“Hello, ladies, look at your man, now back to me, now back at your man, now back to me. Sadly, he isn’t me, but if he stopped using ladies scented body wash and switched to Old Spice, he could smell like he’s me. Look down, back up, where are you? You’re on a boat with the man your man could smell like. What’s in your hand, back at me. I have it, it’s an oyster with two tickets to that thing you love. Look again, the tickets are now diamonds. Anything is possible when your man smells like Old Spice and not a lady. I’m on a horse.”

 

Link to comment
Share on other sites

Anytime you type in a AutoIt function, press F1. It will pop-up the helpfile text for that function. It's very nice :)

I did notice that feature, but since I don't actually know very many autoit functions in the first place, I'm stuck in a vicious loop. :geek:

To understand recursion, you must first understand recursion...

Edited by wrenchmonkey
Link to comment
Share on other sites

Thanks for the quick reply. I guess I just don't understand what those commands you have actually DO. I'm sure it works, but if I don't know how/why it works, I'm still scratching my head... It's still greek to me.

So do I need to put the entirity of my script operations under the if/else headers, or is there a way to send it to a different header upon completing a few basic operations? Like I said, I'm a COMPLETE n00b for this stuff, so I don't really get how to set up the order of operations. I'm used to just being able to use a "goto" command, and sent it ANYWHERE with relative ease.

You dont need to put your whole autoit code in if/else statement just use a function:

Example1()

Func Example1()
    ; Sample script with three user-defined functions
    ; Notice the use of variables, ByRef, and Return

    Local $foo = 2
    Local $bar = 5
    MsgBox(0, "Today is " & today(), "$foo equals " & $foo)
    swap($foo, $bar)
    MsgBox(0, "After swapping $foo and $bar", "$foo now contains " & $foo)
    MsgBox(0, "Finally", "The larger of 3 and 4 is " & max(3, 4))
EndFunc   ;==>Example1

So what does the msgbox command do? Are you just suggesting that I call additional batch files, rather than writing those operations into the autoit script? That's what it LOOKS like to me, the unitiated, and that's the whole thing I'm trying to avoid. Using autoit to call more batch scripts would just add another layer of complexity, and add more windows that are likely to be closed. I guess I could do

From the help file:

Displays a simple message box with optional timeout.

MsgBox(4096, "Test", "This box will time out in 10 seconds", 10)

more gobbldygook that I don't understand. What does this do? Why?

The run command just simple run an external programm.

Hi!

My UDF: NetInfo UDF Play with your network, check your download/upload speed and much more! YTAPI Easy to use YouTube API, now you can easy retrive all needed info from a video. NavInfo Check if a specific browser is installed and retrive other usefull information. YWeather Easy to use Yahoo Weather API, now you can easily retrive details about the weather in a specific region. No-IP UDF Easily update your no-ip hostname(s).

My Script: Wallpaper Changer Change you wallpaper dinamically, you can also download your wallpaper from your website and share it with all!   My Snippet: _ImageSaveToBMPConvert an image to bmp format. _SciteGOTO Open a file in SciTE at specific fileline. _FileToHex Show the hex code of a specified file

Link to comment
Share on other sites

Hmmm, I think I'm starting to get it, but I still don't understand how to send it on to call a different section after it's done with the initial if/then architecture operation.

My best guess is that you just have to come up with an order of operations that will make it do mostly what you want, with a whole lot of if/then/else

does "then" also need to be there to instruct the next operation, or will it just automatically move to the next line in the string and perform those operations?

That is to say, do I need to say,

"if X then (do Y)

then (do Z)"

Or can I just say,

"if x then (do y)

(do z)?

Example of the best way I can figure out that I should re-write the previous .bat to do essentially the same thing, but blocking user input so they can't interrupt the installers, but will still have that interaction window that's needed for the personalization section.

BlockInput(1)
If @OSArch = "X64"(Then
_X64
;check for old version of program, and remove if it already exists.
(IF FileExists =1 "C:\Program Files (x86)\old version of program" Then (MsiExec.exe /X{oldversionuninstaller} /quiet)
Then "C:\Program Files (x86)\old version of program\" run ("\\server\installers\x86installer.exe" /quiet)
if fileexists =0 "C:\Program Files (x86)\old version of program\" run ("\\server\installers\x86installer.exe" /quiet))
Else
_X86(
;check for old version of program, and remove if it already exists.
(IF FileExists =1 "C:\Program Files\old version of program" Then (MsiExec.exe /X{oldversionuninstaller} /quiet)
Then "C:\Program Files (x86)\old version of program\" run ("\\server\installers\x86installer.exe" /quiet)
if fileexists =0 "C:\Program Files\old version of program\" run ("\\server\installers\x86installer.exe" /quiet))
EndIf
BlockInput(0)
RunWait  (C:\installedprogram\launch.exe)
BlockInput (1)
shutdown -f -r -t 03 -c

Hopefully I'm getting warmer, but it's obviously still not right, because the "compile" button is totally grayed out. I can't seem to get F1 to do ANYTHING either, no matter what I have selected or entered.

Edited by wrenchmonkey
Link to comment
Share on other sites

Open the help file, and read it, that will answer most of the basic questions you're asking. Look in the folder that you installed AutoIt in, and you'll find AutoIt3Help.exe in it.

Next, you should probably run through some of the tutorials that you can find in the Wiki section. You'll find the process a lot easier once you get the basics down.

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

Link to comment
Share on other sites

Hmmm, I think I'm starting to get it, but I still don't understand how to send it on to call a different section after it's done with the initial if/then architecture operation.

My best guess is that you just have to come up with an order of operations that will make it do mostly what you want, with a whole lot of if/then/else

does "then" also need to be there to instruct the next operation, or will it just automatically move to the next line in the string and perform those operations?

That is to say, do I need to say,

"if X then (do Y)

then (do Z)"

Or can I just say,

"if x then (do y)

(do z)?

Example of the best way I can figure out that I should re-write the previous .bat to do essentially the same thing, but blocking user input so they can't interrupt the installers, but will still have that interaction window that's needed for the personalization section.

BlockInput(1)
If @OSArch = "X64"(Then
_X64
;check for old version of program, and remove if it already exists.
(IF FileExists =1 "C:\Program Files (x86)\old version of program" Then (MsiExec.exe /X{oldversionuninstaller} /quiet)
Then "C:\Program Files (x86)\old version of program\" run ("\\server\installers\x86installer.exe" /quiet)
if fileexists =0 "C:\Program Files (x86)\old version of program\" run ("\\server\installers\x86installer.exe" /quiet))
Else
_X86(
;check for old version of program, and remove if it already exists.
(IF FileExists =1 "C:\Program Files\old version of program" Then (MsiExec.exe /X{oldversionuninstaller} /quiet)
Then "C:\Program Files (x86)\old version of program\" run ("\\server\installers\x86installer.exe" /quiet)
if fileexists =0 "C:\Program Files\old version of program\" run ("\\server\installers\x86installer.exe" /quiet))
EndIf
BlockInput(0)
RunWait  (C:\installedprogram\launch.exe)
BlockInput (1)
shutdown -f -r -t 03 -c

Hopefully I'm getting warmer, but it's obviously still not right, because the "compile" button is totally grayed out. I can't seem to get F1 to do ANYTHING either, no matter what I have selected or entered.

Seriusly man read the help file and see how autoit works.

Here is an example of your script, its just an example see how the things works:

BlockInput(1)
If @OSArch = "X64" Then
    _X64()
Else
    _X86()
EndIf


Func _X64()
    ;check for old version of program, and remove if it already exists.
    If FileExists("C:\Program Files (x86)\old version of program") Then
        _RunDos("MsiExec.exe / X{oldversionuninstaller} / quiet")
        _RunDos("\\server\installers\x86installer.exe / quiet")
    Else
        _RunDos("\\server\installers\x86installer.exe / quiet")
    EndIf
EndFunc   ;==>_X64

Func _X86()
    ;check for old version of program, and remove if it already exists.
    If FileExists("C:\Program Files\old version of program") Then
        _RunDos("MsiExec.exe / X{oldversionuninstaller} / quiet")
        _RunDos("\\server\installers\x86installer.exe / quiet")
    Else
        _RunDos("\\server\installers\x86installer.exe / quiet")
    EndIf
    BlockInput(0)
    RunWait("C:\installedprogram\launch.exe")
    BlockInput(1)
    Shutdown(2)
EndFunc   ;==>_X86

Func _RunDos($sCommand)
    Local $nResult = RunWait(@ComSpec & " /C " & $sCommand, "", @SW_HIDE)
    Return SetError(@error, @extended, $nResult)
EndFunc   ;==>_RunDos

Hi!

My UDF: NetInfo UDF Play with your network, check your download/upload speed and much more! YTAPI Easy to use YouTube API, now you can easy retrive all needed info from a video. NavInfo Check if a specific browser is installed and retrive other usefull information. YWeather Easy to use Yahoo Weather API, now you can easily retrive details about the weather in a specific region. No-IP UDF Easily update your no-ip hostname(s).

My Script: Wallpaper Changer Change you wallpaper dinamically, you can also download your wallpaper from your website and share it with all!   My Snippet: _ImageSaveToBMPConvert an image to bmp format. _SciteGOTO Open a file in SciTE at specific fileline. _FileToHex Show the hex code of a specified file

Link to comment
Share on other sites

THANK YOU SO MUCH! That helps me TONS. Being able to see what the syntax SHOULD look like, as compared to what I'm used to seeing really makes it 'click'. This is basically like a miniature rosetta stone for me! I've got tons to learn, but this will really help me understand the basic formatting. I really can't thank you enough!

Also, thanks for the tip on manually running the help file. For whatever reason, it just wasn't launching from within the editor.

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...