Loop with Multi OutputVar issue. Please help! Topic is solved

Ask gaming related questions (AHK v1.1 and older)
gamesguy383
Posts: 21
Joined: 09 Feb 2018, 02:10

Loop with Multi OutputVar issue. Please help!

17 Feb 2018, 01:40

Hello everyone
I'm trying to make multiple batch files with this script. So far it only makes one file, which is a last file in the list. I'm new to the AutoHotKey and struggling with getting the multiple OutputVar to work. I thought that a Loop would perform a function on every file one by one, but obviously I'm missing something. Is there anyone kind enough to point out my mistakes or where to get the info on how to fix it.
Thank you all in advance.

Code: Select all

SetWorkingDir %A_ScriptDir%
LogDir := "allgames"

IfNotExist, %CLogDir%
FileCreateDir, %LogDir%
IfNotExist, ..\gamelists\%LogDir%
FileCreateDir, ..\gamelists\%LogDir%
	
FileRead, systemsconfig, ..\systems.cfg

IfInString, systemsconfig, %LogDir%
{
	MsgBox, ,Please proceed, The %LogDir% system already exists
goto FileSetAttrib
}

IfNotInString, systemsconfig, %LogDir%
{
	ROM_RAW := "%ROM_RAW%"
	%LogDir%node =
	(join`r`n
<system>
<name>%LogDir%</name>
<fullname>%LogDir%</fullname>
<path>%LogDir%</path>
<extension>.bat .BAT</extension>
<command>"%ROM_RAW%"</command>
<theme>%LogDir%</theme>
</system>

</systemList>
	)
	
	systemsconfig := StrReplace(systemsconfig, "</systemList>", %LogDir%node)
	
	FileDelete,  ..\systems.cfg
	FileAppend, %systemsconfig%, ..\systems.cfg
		MsgBox, ,Please proceed, The %LogDir% system was created in the systems.cfg
	}
	
FileSetAttrib:
{
	MsgBox, 4,Please select, Would you like to hide unwanted files?
	IfMsgBox, No, goto Loop
	}
FileSetAttrib, +H, %A_WorkingDir%\nullDC\*.*
FileSetAttrib, -H, %A_WorkingDir%\nullDC\*.iso
FileSetAttrib, +H, %A_WorkingDir%\Dolphin\*.*
FileSetAttrib, -H, %A_WorkingDir%\Dolphin\*.bin
FileSetAttrib, +H, %A_WorkingDir%\PPSSPP\*.*
FileSetAttrib, -H, %A_WorkingDir%\PPSSPP\*.cso
FileSetAttrib, +H, %A_WorkingDir%\pcsx-2\*.*
FileSetAttrib, -H, %A_WorkingDir%\pcsx-2\*.bin
FileSetAttrib, -H, %A_WorkingDir%\pcsx-2\*.iso
	MsgBox, ,Please proceed, Unwanted files have been hidden
	
Loop:
Loop, *.*, 0, 1 

IfNotInString, A_LoopFileAttrib, H
	{
	SourceFileName := A_LoopFileName
	SourceFileDir := StrReplace(A_LoopFileDir, "-")
	Array := StrSplit(A_LoopFileName, ".")
	SourceFileNoExt := Array[1]
	
	;MsgBox, 4,Please select, %SourceFileNoExt%
	;IfMsgBox, No, goto Sort
	
	}
	
Sort:	
Sort, FileList

{
	MsgBox, 4,Please select, Would you like to return the hidden files?
	IfMsgBox, No, goto Next
	}
FileSetAttrib, -H, %A_WorkingDir%\nullDC\*.*
FileSetAttrib, -H, %A_WorkingDir%\Dolphin\*.*
FileSetAttrib, -H, %A_WorkingDir%\PPSSPP\*.*
FileSetAttrib, -H, %A_WorkingDir%\pcsx-2\*.*
	MsgBox, ,Please proceed, Hidden files have been returned

Next:
nullDC = %HOME%\nullDC.exe -config nullDC_GUI:Fullscreen=1 -config nullDC:Emulator.Autostart=1 -config ImageReader:LoadDefaultImage=1 -config ImageReader:DefaultImage="%SourceFileName%"
Dolphin = %HOME%\Dolphin.exe -e "%SourceFileName%"
PPSSPP = %HOME%\PPSSPPWindows.exe "%SourceFileName%"
pcsx2 = %HOME%\pcsx2.exe "%SourceFileName%"

if A_LoopFileFullPath contains pcsx-2
{
BatF := %SourceFileDir%

	FileDelete, %A_WorkingDir%\%LogDir%\%SourceFileNoExt% - pcsx-2.bat
	FileAppend, %BatF%, %A_WorkingDir%\%LogDir%\%SourceFileNoExt% - pcsx-2.bat
		MsgBox, , Congratulations!, %SourceFileNoExt% - pcsx-2.bat`n`n Was successfully created in the %LogDir% directory
	
					}
else
{
BatchF := %SourceFileDir%

	FileDelete, %A_WorkingDir%\%LogDir%\%SourceFileNoExt% - %SourceFileDir%.bat
	FileAppend, %BatchF%, %A_WorkingDir%\%LogDir%\%SourceFileNoExt% - %SourceFileDir%.bat
		MsgBox, , Congratulations!, %SourceFileNoExt% - %SourceFileDir%.bat`n`n Was successfully created in the %LogDir% directory

					}

ExitApp
gamesguy383
Posts: 21
Joined: 09 Feb 2018, 02:10

Re: Loop with Multi OutputVar issue. Please help!  Topic is solved

17 Feb 2018, 23:55

Not sure if anyone is interested, but I worked out that I needed a loop,parse and a crazy arrangement of the brackets { }. It was an easy fix after all and the scrip looks like this now.
Thank you all!

Code: Select all

SetWorkingDir %A_ScriptDir%
LogDir := "allgames"

IfNotExist, %CLogDir%
FileCreateDir, %LogDir%
IfNotExist, ..\gamelists\%LogDir%
FileCreateDir, ..\gamelists\%LogDir%
	
FileRead, systemsconfig, ..\systems.cfg

IfInString, systemsconfig, %LogDir%
{
	MsgBox, ,Please proceed, The %LogDir% system already exists
goto FileSetAttrib
}

IfNotInString, systemsconfig, %LogDir%
{
	ROM_RAW := "%ROM_RAW%"
	%LogDir%node =
	(join`r`n
<system>
<name>%LogDir%</name>
<fullname>%LogDir%</fullname>
<path>%LogDir%</path>
<extension>.bat .BAT</extension>
<command>"%ROM_RAW%"</command>
<theme>%LogDir%</theme>
</system>

</systemList>
	)
	
	systemsconfig := StrReplace(systemsconfig, "</systemList>", %LogDir%node)
	
	FileDelete,  ..\systems.cfg
	FileAppend, %systemsconfig%, ..\systems.cfg
		MsgBox, ,Please proceed, The %LogDir% system was created in the systems.cfg
	}
	
FileSetAttrib:
{
	MsgBox, 4,Please select, Would you like to hide unwanted files?
	IfMsgBox, No, goto Loop
	}
FileSetAttrib, +H, %A_WorkingDir%\nullDC\*.*
FileSetAttrib, -H, %A_WorkingDir%\nullDC\*.iso
FileSetAttrib, +H, %A_WorkingDir%\Dolphin\*.*
FileSetAttrib, -H, %A_WorkingDir%\Dolphin\*.bin
FileSetAttrib, +H, %A_WorkingDir%\PPSSPP\*.*
FileSetAttrib, -H, %A_WorkingDir%\PPSSPP\*.cso
FileSetAttrib, +H, %A_WorkingDir%\pcsx-2\*.*
FileSetAttrib, -H, %A_WorkingDir%\pcsx-2\*.bin
FileSetAttrib, -H, %A_WorkingDir%\pcsx-2\*.iso
	MsgBox, ,Please proceed, Unwanted files have been hidden

Loop:
FileList = 
Loop, Files, *.*, R 

IfNotInString, A_LoopFileAttrib, H
	FileList = %FileList%%A_LoopFileFullPath%`n 
	Sort, FileList

Loop, Parse, FileList, `n
{
	Array := StrSplit(A_LoopField, "\")
	SourceFileDir := StrReplace(Array[1], "-")
	SourceFileName := Array[2]
	ArraySFN := StrSplit(SourceFileName, ".")
	SourceFileNoExt := ArraySFN[1]

			{
nullDC = %HOME%\nullDC.exe -config nullDC_GUI:Fullscreen=1 -config nullDC:Emulator.Autostart=1 -config ImageReader:LoadDefaultImage=1 -config ImageReader:DefaultImage="%SourceFileName%"
Dolphin = %HOME%\Dolphin.exe -e "%SourceFileName%"
PPSSPP = %HOME%\PPSSPPWindows.exe "%SourceFileName%"
pcsx2 = %HOME%\pcsx2.exe "%SourceFileName%"

if A_LoopFileFullPath contains pcsx-2
	{
	BatF := %SourceFileDir%

	FileDelete, %A_WorkingDir%\%LogDir%\%SourceFileNoExt% - pcsx-2.bat
	FileAppend, %BatF%, %A_WorkingDir%\%LogDir%\%SourceFileNoExt% - pcsx-2.bat
		;MsgBox, , Congratulations!, %SourceFileNoExt% - pcsx-2.bat`n`n Was successfully created in the %LogDir% directory
	
	}
else
	{
	try {
	BatchF := %SourceFileDir%
		}
	catch e {
		;ignore
			}
	if !BatchF, goto FileSetAttribReturn

	FileDelete, %A_WorkingDir%\%LogDir%\%SourceFileNoExt% - %SourceFileDir%.bat
	FileAppend, %BatchF%, %A_WorkingDir%\%LogDir%\%SourceFileNoExt% - %SourceFileDir%.bat
		;MsgBox, , Congratulations!, %SourceFileNoExt% - %SourceFileDir%.bat`n`n Was successfully created in the %LogDir% directory

	}
			}

}

FileSetAttribReturn:
	{
	MsgBox, 4,Please select, Would you like to return the hidden files?
	IfMsgBox, No, goto ExitApp
	}
FileSetAttrib, -H, %A_WorkingDir%\nullDC\*.*
FileSetAttrib, -H, %A_WorkingDir%\Dolphin\*.*
FileSetAttrib, -H, %A_WorkingDir%\PPSSPP\*.*
FileSetAttrib, -H, %A_WorkingDir%\pcsx-2\*.*
	MsgBox, ,Please proceed, Hidden files have been returned	

ExitApp:
	MsgBox, ,Congratulations!, All your batch files were successfully created in the %LogDir% directory.`n`n You are ready to test!
ExitApp
gamesguy383
Posts: 21
Joined: 09 Feb 2018, 02:10

Re: Loop with Multi OutputVar issue. Please help!

24 Feb 2018, 01:11

Added functions to:

Read the source file game list,

Extract the game node with a source file

Recognise that the file extension is bat now and not the original extension

Add source file directory in the game list. So the front end knows the original directory

Insert those changes into a new %LogDir% game list system

Now the script writes the %LogDir% system in the systems.cfg, writes bat files into %LogDir% directory, reads the %LogDir% game list, extracts the desired game info, makes necessary changes to that info and then adds it to the %LogDir% game list.

Code: Select all

SetWorkingDir %A_ScriptDir%
LogDir := "allgames"

IfNotExist, %CLogDir%
FileCreateDir, %LogDir%
IfNotExist, ..\gamelists\%LogDir%
FileCreateDir, ..\gamelists\%LogDir%
	
FileRead, systemsconfig, ..\systems.cfg

IfInString, systemsconfig, %LogDir%
{
	MsgBox, ,Please proceed, The %LogDir% system already exists
goto FileSetAttrib
}

IfNotInString, systemsconfig, %LogDir%
{
	ROM_RAW := "%ROM_RAW%"
	%LogDir%node =
	(join`r`n
<system>
<name>%LogDir%</name>
<fullname>%LogDir%</fullname>
<path>%LogDir%</path>
<extension>.bat .BAT</extension>
<command>"%ROM_RAW%"</command>
<theme>%LogDir%</theme>
</system>

</systemList>
	)
	
	systemsconfig := StrReplace(systemsconfig, "</systemList>", %LogDir%node)
	
	FileDelete,  ..\systems.cfg
	FileAppend, %systemsconfig%, ..\systems.cfg
		MsgBox, ,Please proceed, The %LogDir% system was created in the systems.cfg
	}
	
FileSetAttrib:
{
	MsgBox, 4,Please select, Would you like to hide unwanted files?
	IfMsgBox, No, goto Loop
	}
FileSetAttrib, +H, %A_WorkingDir%\nullDC\*.*
FileSetAttrib, -H, %A_WorkingDir%\nullDC\*.iso
FileSetAttrib, +H, %A_WorkingDir%\Dolphin\*.*
FileSetAttrib, -H, %A_WorkingDir%\Dolphin\*.bin
FileSetAttrib, +H, %A_WorkingDir%\PPSSPP\*.*
FileSetAttrib, -H, %A_WorkingDir%\PPSSPP\*.cso
FileSetAttrib, +H, %A_WorkingDir%\pcsx-2\*.*
FileSetAttrib, -H, %A_WorkingDir%\pcsx-2\*.bin
FileSetAttrib, -H, %A_WorkingDir%\pcsx-2\*.iso
	MsgBox, ,Please proceed, Unwanted files have been hidden

Loop:
FileList = 
Loop, Files, *.*, R 

IfNotInString, A_LoopFileAttrib, H
	FileList = %FileList%%A_LoopFileFullPath%`n 
	Sort, FileList

Loop, Parse, FileList, `n
{
	Array := StrSplit(A_LoopField, "\")																							
		SourceFileName := Array.2																								
		SourceFileDir := Array.1																								

	Aray := StrSplit(SourceFileName, ".")																						
		Needle := Aray.MaxIndex()																								
			if Needle = 2																										
				SourceFileExt := Aray.2
			if Needle = 3
				SourceFileExt := Aray.3
			if Needle = 4
				SourceFileExt := Aray.4
			if Needle = 5
				SourceFileExt := Aray.5
			if Needle = 6
				SourceFileExt := Aray.6
			if Needle = 7
				SourceFileExt := Aray.7
			if Needle = 8
				SourceFileExt := Aray.8
			if Needle = 9
				SourceFileExt := Aray.9
			if Needle = 10
				SourceFileExt := Aray.10
			
	SourceFileExtensions = .%SourceFileExt%																						
	SourceFileNoExt := StrReplace(SourceFileName, SourceFileExtensions)															
	
		;MsgBox, 4, Please select,%SourceFileNoExt%
		;IfMsgBox, No, break
	
	
nullDC = %HOME%\nullDC.exe -config nullDC_GUI:Fullscreen=1 -config nullDC:Emulator.Autostart=1 -config ImageReader:LoadDefaultImage=1 -config ImageReader:DefaultImage="%SourceFileName%"
Dolphin = %HOME%\Dolphin.exe -e "%SourceFileName%"
PPSSPP = %HOME%\PPSSPPWindows.exe "%SourceFileName%"
pcsx2 = %HOME%\pcsx2.exe "%SourceFileName%"

if A_LoopField contains pcsx-2																									
{
	FileDelete, %A_WorkingDir%\%LogDir%\%SourceFileNoExt% - pcsx-2.bat															
	FileAppend, % pcsx2, %A_WorkingDir%\%LogDir%\%SourceFileNoExt% - pcsx-2.bat												
		;MsgBox, , Congratulations!, %SourceFileNoExt% - pcsx-2.bat`n`n Was successfully created in the %LogDir% directory

	
	FileRead, gamelist, ..\gamelists\pcsx-2\gamelist.xml																	

		doc := ComObjCreate("MSXML2.DOMDocument.6.0")
		doc.async := false
		doc.loadXML(gamelist)																								
	
		try {
			docNode := doc.selectSingleNode("//game[path='./" SourceFileName "']")												
			}catch e {																											
						;ignore
					  }
		if !docNode																												
			;Msgbox, ,Please scrape from the front end, The %SourceFileName%`n was not found in the pcsx-2 gamelist
			continue																											
	{
	Haystack := docNode.xml																										
	Loop, Parse, Haystack, `n																									

		if A_index = 2																											
						{
						Needle = <path>./%SourceFileNoExt% - pcsx-2.bat</path>													
						Haystack1 := StrReplace(Haystack, A_LoopField, Needle)													
						}
					else																										
						{
						Ndle = - pcsx-2</name>																					
						Haystack2 := StrReplace(Haystack1, "</name>", Ndle)														
						Haystack3 := StrReplace(Haystack2, "</game>", "</game>`n</gameList>")									
						}

	FileRead, LogDirGameList, ..\gamelists\%LogDir%\gamelist.xml																

		if ErrorLevel = 0																										
		{
			;if LogDirGameList contains %SourceFileNoExt% - pcsx-2.bat															
				;continue																										
				;{
				;MsgBox, ,Please proceed, %SourceFileNoExt% - pcsx-2.bat`n`n Already exists in the`n`n %LogDir%\gamelist.xml
				;}
			;else																												
				;{
				LogDirGameList := StrReplace(LogDirGameList, "</gamelist>", Haystack3)											
				FileDelete, ..\gamelists\%LogDir%\gamelist.xml																	
				FileAppend, %LogDirGameList%, ..\gamelists\%LogDir%\gamelist.xml													
					;MsgBox, , Congratulations!, %SourceFileNoExt% - pcsx-2.bat`n`n Was successfully created in the`n`n ..\gamelists\%LogDir%\gamelist.xml
					;return
				;}
		}	
			
		if ErrorLevel = 1																											
		{
		NewXmlLogDir = 																											
	(join`r`n
<?xml version="1.0"?>
<gameList>
	<game>
		<path></path>
		<name></name>
	</game>
</gameList>
	)

		Haystack4 := StrReplace(NewXmlLogDir, "</gameList>", Haystack3)															
		FileAppend, %Haystack4%, ..\gamelists\%LogDir%\gamelist.xml																
			;MsgBox, , Congratulations!, %SourceFileNoExt% - pcsx-2.bat`n`n Was successfully created in the`n`n ..\gamelists\%LogDir%\gamelist.xml
			;return
			}
				}
}
else																															
	{
		try {
			BatchF := %SourceFileDir%																							
			}
		catch e {																												
				;ignore	
				}
		if !BatchF, goto FileSetAttribReturn																	

	FileDelete, %A_WorkingDir%\%LogDir%\%SourceFileNoExt% - %SourceFileDir%.bat
	FileAppend, %BatchF%, %A_WorkingDir%\%LogDir%\%SourceFileNoExt% - %SourceFileDir%.bat
		;MsgBox, , Congratulations!, %SourceFileNoExt% - %SourceFileDir%.bat`n`n Was successfully created in the %LogDir% directory

	FileRead, gamelist, ..\gamelists\%SourceFileDir%\gamelist.xml

		doc := ComObjCreate("MSXML2.DOMDocument.6.0")
		doc.async := false
		doc.loadXML(gamelist)

			try {
				docNode := doc.selectSingleNode("//game[path='./" SourceFileName "']")
				}catch e {
						;ignore
						  }
			if !docNode
				;Msgbox, ,Please scrape from the front end, The %SourceFileName%`n was not found in the %SourceFileDir% gamelist
				continue
			else
				{
				Haystack := docNode.xml
				Loop, Parse, Haystack, `n

				if A_index = 2
					{
					Needle = <path>./%SourceFileNoExt% - %SourceFileDir%.bat</path>
					Haystack1 := StrReplace(Haystack, A_LoopField, Needle)
					}
				else
					{
					Ndle = - %SourceFileDir%</name>
					Haystack2 := StrReplace(Haystack1, "</name>", Ndle)
					Haystack3 := StrReplace(Haystack2, "</game>", "</game>`n</gameList>")
					}

	FileRead, LogDirGameList, ..\gamelists\%LogDir%\gamelist.xml

		if ErrorLevel = 0
		{
			;if LogDirGameList contains %SourceFileNoExt% - %SourceFileDir%.bat
				;continue
				;{
				;MsgBox, ,Please proceed, %SourceFileNoExt% - %SourceFileDir%.bat`n`n Already exists in the`n`n ..\gamelists\%LogDir%\gamelist.xml
				;}
			;else
				;{
				LogDirGameList := StrReplace(LogDirGameList, "</gamelist>", Haystack3)

				FileDelete, ..\gamelists\%LogDir%\gamelist.xml
				FileAppend, %LogDirGameList%, ..\gamelists\%LogDir%\gamelist.xml
					;MsgBox, , Congratulations!, %SourceFileNoExt% - %SourceFileDir%.bat`n`n Was successfully created in the`n`n ..\gamelists\%LogDir%\gamelist.xml
					;return
				;}
		}
		if ErrorLevel = 1
			{
			NewXmlLogDir = 
	(join`r`n
<?xml version="1.0"?>
<gameList>
	<game>
		<path></path>
		<name></name>
	</game>
</gameList>
	)

		Haystack4 := StrReplace(NewXmlLogDir, "</gameList>", Haystack3)
		FileAppend, %Haystack4%, ..\gamelists\%LogDir%\gamelist.xml
			;MsgBox, , Congratulations!, %SourceFileNoExt% - %SourceFileDir%.bat`n`n Was successfully created in the`n`n ..\gamelists\%LogDir%\gamelist.xml
			;return
		}
				}
}
	}

FileSetAttribReturn:
	{
	MsgBox, 4,Please select, Would you like to return the hidden files?
	IfMsgBox, No, goto Congratulations
	}

FileSetAttrib, -H, %A_WorkingDir%\nullDC\*.*
FileSetAttrib, -H, %A_WorkingDir%\Dolphin\*.*
FileSetAttrib, -H, %A_WorkingDir%\PPSSPP\*.*
FileSetAttrib, -H, %A_WorkingDir%\pcsx-2\*.*
	MsgBox, ,Please proceed, Hidden files have been returned	

Congratulations:
	MsgBox, ,Congratulations!, All your batch files were successfully created in the %LogDir% directory.`n`n You are ready to test!
ExitApp

Return to “Gaming Help (v1)”

Who is online

Users browsing this forum: No registered users and 48 guests