Sign in to follow this  
Followers 0
MrChris

trying to convert a batch script to au3

5 posts in this topic

I have a batch script I wrote to help in ejecting my usb device from my xp machine. And with the below batch code it works just fine. But I would rather use a autoit script as autoit will produce much prettier looking dialogs then a dos batch window.

my current batch script it below. what AutoIt code can I use to produce the same results as the below batch script?

eject.cmd:

@Echo off
goto eject

:eject
removedrive.exe "SanDisk Cruzer Micro USB Device"
if errorlevel 0 goto end
if errorlevel 1 goto procexp

:procexp
start %usbdrive%\eject\procexp.exe

:end
reg delete "HKCU\Environment" /v usbdrive /f
echo.
echo You may now safley unplug the "SanDisk Cruzer Micro USB Device"
echo.
pause
exit

thanks for any help anyone can provide.

MrChris

Share this post


Link to post
Share on other sites



Not sure of %usbdrive%, as if the system allocates this env. variable. Not sure of exitcode result, but have taken a chance of output.

This may help to get you started.

$exitcode = RunWait('removedrive.exe "SanDisk Cruzer Micro USB Device"')
If $exitcode = 0 Then
    end()
ElseIf $exitcode = 1 Then
    procexp()
EndIf

Exit

Func procexp()
    RunWait(@ComSpec & ' /c start %usbdrive%\eject\procexp.exe', '', @SW_HIDE)
EndFunc

Func end()
    If RegDelete('HKCU\Environment', 'usbdrive') = 1 Then
        MsgBox(0, '', 'You may now safely unplug the "SanDisk Cruzer Micro USB Device"')
    EndIf
EndFunc

:)

Share this post


Link to post
Share on other sites

@MHz

This was a great help. Thank you very much.

MrChris

Share this post


Link to post
Share on other sites

#4 ·  Posted (edited)

After reading through the help on EnvSet it doesnt look like it is possible to set a env var to such as %cd% to set the var of that the current dir is? in a dos batch file I would just run setx.exe abcdefghi %cd% and if the batch file is run from C:\test then the env var abcdefghi is set to C:\.

If I put RunWait('setx.exe abcdefghi %cd%', '', @SW_HIDE) in a blank au3 file it set the var as %cd% rather than a valid drive\path.

Edited by MrChris

Share this post


Link to post
Share on other sites

After reading through the help on EnvSet it doesnt look like it is possible to set a env var to such as %cd% to set the var of that the current dir is? in a dos batch file I would just run setx.exe abcdefghi %cd% and if the batch file is run from C:\test then the env var abcdefghi is set to C:\.

If I put RunWait('setx.exe abcdefghi %cd%', '', @SW_HIDE) in a blank au3 file it set the var as %cd% rather than a valid drive\path.

Your confusing me with what you need :D .

In DOS, you would use PUSHD to store the current firectory and change current directory. Then use POPD to return to previous current directory. If C:\test is the current directory, then abcdefghi would equal C:\test if I'm not mistaken.

Now EnvSet will set a environment variable, much like what you set in a cmd file for tempory use. EnvSet will last for the life of the script.

SetX is a commandline tool to permanently set a environment variable. This is different to the above 2 items yet again as is written permanently to registry.

I'm going to guess as you mention last of SetX, of wanting a permanent environment variable.

This is what I use to do this

; Example of creating %SendTo% for the userprofile

_Variable('User', 'SendTo', '%UserProfile%\SendTo')

Func _Variable($type, $key, $value)
    ; adds a permanent environment variable.
    Local $environment
    Switch $type
        Case 'System'
            $environment = 'HKLM\SYSTEM\CurrentControlSet\Control\Session Manager\Environment'
        Case 'User'
            $environment = 'HKCU\Environment'
        Case Else
            Return
    EndSwitch
    If Not RegRead($environment, $key) Then
        If RegWrite($environment, $key, 'Reg_Expand_sz', $value) Then EnvUpdate()
    EndIf
EndFunc

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