Autohotkey : skip comment lines in Loop / FileReadLine - autohotkey

i read a config.ini with a location of some .exe to run, but i want to skip a comment line from my config.ini (cause i want to explain how to use the file), if someone can help me thanks
#NoEnv
#SingleInstance force
Loop
{
FileReadLine, exe, config.ini, %A_Index%
if ErrorLevel
break
Run %exe%
Sleep , 300
}
return
ExitApp
config.ini
// Put here location of your programs << line to skip
// Thanks << line to skip
C:\WINDOWS\notepad.exe
C:\WINDOWS\****.exe
...

Loop
{
FileReadLine, exe, config.ini, %A_Index%
if ErrorLevel
break
else if SubStr(exe,1,2)=="//"
continue
Run %exe%
Sleep , 300
}
Using SubStr() check if first two chars in line are // if so use continue to skip rest of loop and start next iteration.

Related

It maybe seem unnecessary, but I want to reopen a spezific program after it is closed by ahk

This is how far i got:
#IfWinActive ahk_exe zotero.exe
f7::
WinGetTitle, Title, A
WinClose, A
Sleep 1000
IfWinNotExist(zotero)
{
Run, zotero.exe , , Min
}
Return
I am unsure about the if statement in an if statement. How can I make it work?
I would suggest to use:
Loop {
Sleep, 1000 ; just 1 second delay between any check
process, Exist, "Zotero.exe"
if ErrorLevel
Run, zotero.exe , , Min
}

Issue with an Autohotkey Script running in Windows 10

The following script doesn't work properly in Windows 10. The force shutdown screen doesn't go away (which is supposed to happen so the script can continue). I tried using ESC instead of ENTER along with adding two TABs before the ENTER (neither of those methods worked). I am using it to backup the RAM Disk (made with the driver I mentioned in my previous question) before shutting down/rebooting the OS:
#NoEnv
#Persistent
SendMode Input
SetWorkingDir %A_ScriptDir%
SetTimer, RunBeforeShutdown, Off
Gui,+LastFound
hwnd:=WinExist()
DllCall("ShutdownBlockReasonCreate","Uint",hwnd,"Str","")
DllCall("kernel32.dll\SetProcessShutdownParameters", UInt, 0x4FF, UInt, 0)
;puts us first in line for getting the shutdown call, i guess?
OnMessage(0x11, "WM_QUERYENDSESSION")
Return
WM_QUERYENDSESSION(wParam, lParam)
{
ENDSESSION_Logoff = 2147483648
If (lParam == ENDSESSION_Logoff) {
global EventType = "Logoff"
} Else {
global EventType = "Shutdown"
;no way to distinguish between shutdown and restart
}
SetTimer, RunBeforeShutdown, On
Return false
}
runBeforeShutdown:
SetTimer, RunBeforeShutdown, Off
Sleep, 1000
SendInput, {ENTER} ; gets us past the 'Force shudown' screen
Sleep, 1000
#SingleInstance, Force
DllCall("ShutdownBlockReasonDestroy","Uint",hwnd)
; **** Your commands go here ****
RunWait cmd.exe /c backup.bat
; ********
If (EventType == "Logoff") {
Shutdown, 0
} Else {
Shutdown, 1
}
Reload
Return
EDIT: I ended up making a Scheduled Task that runs Drive Snapshot every night with parameters to back up the drive to a drive image.

AutoHotKey Help, matching character and replacing

My save feature, note I do not want to know better ways to save it or corrections to my save code, I just need help on the part where I need it to automatically remove additional non required/old brackets generated on save:
Here's the save button:
buttonSave:
FileAppend,
(
nprocess,exist, %Input%
nprocess,priority, %Input%, %PriorInput%
}
), C:\Users\%A_UserName%\Desktop\AutoSetup\Settings.ahk
MsgBox, 0, Saved, %PriorInput% Priority to %Input% has been saved.
How can I get AutoHotkey to find } and remove any matching the char } and then add one } at the very end of the file, and have this looped?
I have a save feature and the output is this:
loop
{
process,exist, Steam.exe
process,priority, Steam.exe,
}
process,exist, Steam.exe
process,priority, Steam.exe,
}
I want it to find the end } and the middle } remove both of them and then add a single } at the very end. If you save, it always adds a bracket at the bottom.
buttonSave:
FileRead, Contents, C:\Users\%A_UserName%\Desktop\AutoSetup\Settings.ahk
if not ErrorLevel ; Successfully loaded
{
StringTrimRight, Contents, Contents, 1 ; remove the end }
FileAppend, %Contents%, C:\Users\%A_UserName%\Desktop\AutoSetup\Temp.ahk
Contents = ; Free the memory
}
FileAppend,
(
process,exist, %Input%
process,priority, %Input%, %PriorInput%
}
), C:\Users\%A_UserName%\Desktop\AutoSetup\Temp.ahk
FileMove, C:\Users\%A_UserName%\Desktop\AutoSetup\Temp.ahk, C:\Users\%A_UserName%\Desktop\AutoSetup\Settings.ahk, 1 ; overwrite existing file
; Run, edit C:\Users\%A_UserName%\Desktop\AutoSetup\Settings.ahk
MsgBox, 0, Saved, %PriorInput% Priority to %Input% has been saved.
; reload ; if Settings.ahk is included in this script
return
I'd use something like this (The loop causes high CPU utilization):
#NoEnv
#SingleInstance Force
#Persistent
Menu, Tray, Add
Menu, Tray, Add, Add Process, Add_Process
Menu, Tray, Default, Add Process ; doubleclick the tray icon to add a new process
SetTimer, set_priority, 500
return
set_priority:
Process,exist, Notepad.exe
Process,priority, Notepad.exe, L
; Add Process
return
Add_Process:
InputBox, ProcessName, ProcessName, Enter a Process.
if !ErrorLevel
InputBox, priority, priority, Enter a priority.
if !ErrorLevel
{
text =
(
Process,exist, %ProcessName%.exe
Process,priority, %ProcessName%.exe, %priority%
; Add Process
)
FileRead, Contents, %A_ScriptFullPath%
if not ErrorLevel ; Successfully loaded
{
StringReplace, Contents, Contents, `; Add Process, %text%
FileAppend, %Contents%, %A_ScriptDir%\Temp.ahk
Contents = ; Free the memory
}
FileMove, %A_ScriptDir%\Temp.ahk, %A_ScriptFullPath%, 1 ; overwrite existing file
reload
}
return

Series of actions in one script, successively

AutoHotKey.
Leraning how to make a series of actions, with strings, files, variables etc. From 1 to 101 of them. Files in one folder or folders in one folder, strings from high, variables in one script. By more simple and classical methods.
If you want to do multiple similar actions it's almost always a good idea to use some form of loop.
Arrays can also help a lot.
Here's a file/folder loop:
Loop Files, C:\*.exe, R ; get all .exe files that are on C:\ and subfolders of it
{
MsgBox, Full path of the current file: %A_LoopFileFullPath%
If (A_LoopFileName = "virus.exe") {
MsgBox, A file called virus.exe was found.
}
}
Here's a normal loop:
Loop, 101 ;run the following code 101 times
{
MsgBox, This is iteration number %A_Index%
If (A_Index = 10)
MsgBox, Nice! You made it through 10 iterations!
}
...
Loop, 101 ;run the following code 101 times
{
If (A_Index >= 10 && A_Index < 20)
MsgBox, This is iteration number %A_Index%. (10-20)
If (A_Index >= 37 && A_Index < 71)
{
MsgBox, This is iteration number %A_Index%. (37-71)
}
}

How to detect whether a process or application is running and terminate the program if it isn't

So I am attempting to determine whether the process MultiMC.exe is running or not and if it isn't I want to end my script.
IfExist, Process MultiMC.exe
ExitApp, 0
Else
ExitApp 1
I've tried this but it isn't working.
Try the following "Process,Exist, act.exe", lifted from http://www.autohotkey.com/board/topic/50026-if-process-not-running-run-it/
I only show this so you see how the "Process,Exist, act.exe" is used in another script.
#persistent
SetTitleMatchMode,2
Loop
{
Process,Exist, act.exe ; Sets errorlevel to process PID
IfWinNotExist, % "ahk_pid " errorlevel ; Expression for ahk_pid
{ ; Block to do something.
Run, C:\Documents and Settings\Pat\Desktop\act.exe
Break ; Stops loop if run or it will continue forever.
}
}