Your Opinion on this simple script (Autorun Batch)

Get help with using AutoHotkey and its commands and hotkeys
need4speed
Posts: 73
Joined: 22 Apr 2016, 06:50

Your Opinion on this simple script (Autorun Batch)

26 Jun 2019, 03:36

How the script is supposed to work:
1. Check if cmd exists
2. Check if cmd contains an certain command
3. Create a backup folder, copy cmd, execute cmd, delete cmd

Code: Select all

#Persistent
#singleinstance, force
SetWorkingDir, %A_ScriptDir%

Menu Tray, NoStandard
Menu, Tray, Tip , AutoRun Batch V1.0

fullFilePath := "C:\start.cmd"

SetTimer, CheckForFile, 1000
return

CheckForFile:
if (FileExist(fullFilePath)) {     
    
    FileRead, vText, %fullFilePath%

    if not ErrorLevel
    {
        if InStr(vText,"calc.exe")
            {  
                FileCreateDir, C:\backup
                if (ErrorLevel = 1)
                    {
                        MsgBox, 16, Error, Cannot create Folder.
                        Exit
                    }
                    
                FileCopy, %fullFilePath%, C:\backup\*.*, Overwrite
                if (ErrorLevel != 0)
                    {
                        MsgBox, 16, Error, Cannot copy file.
                        Exit
                    }
					
                RunWait, %fullFilePath%,,UseErrorLevel
                if ErrorLevel = ERROR
                    {
                        MsgBox, 16, Error, Cannot execute file.
                        Exit
                    }			
                    
                FileDelete, %fullFilePath%
                if (ErrorLevel > 0)
                    {
                        MsgBox, 16, Error, Cannot delete file.
                        Exit
                    }
                    
            }
    }
}
return
I tried to "harden" the code, if anything goes wrong an error message should pop up.
I am though not 100% sure with the ErrorLevel commands.

- How to write if ErrorLevel = ERROR as an expression
- What do you think in General? What could be improved without overcomplicating things?

Thanks for your feed-back!

Return to “Ask For Help”

Who is online

Users browsing this forum: Bing [Bot], IlanSh, MannyKSoSo, virgil27 and 180 guests