Page 1 of 1

Your Opinion on this simple script (Autorun Batch)

Posted: 26 Jun 2019, 03:36
by need4speed
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!