Goto is exiting the loop after its used once.

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
ShawnT
Posts: 20
Joined: 07 Mar 2017, 15:19

Goto is exiting the loop after its used once.

29 Jan 2020, 14:29

Trying to clean this code up a bit. Instead of using the same code both times in the else statements I added a GoTo, the problem is that it exits the script after one time accessing, i want it to keep looping thru the other *.pdfs in the folder. Just like it would if i kept the redundant code in both places. Any ideas? thanks in advance.

Code: Select all

MoveFiles:

Loop, C:\Users\%A_username%\AppData\Local\GreenFolders\GreenFolders\Temp\*, 2
{

if FileExist("C:\Users\" A_username "\AppData\Local\GreenFolders\GreenFolders\Temp\" A_loopfilename "")

	FileFolder := A_loopfilename

	Loop, C:\Users\%A_username%\AppData\Local\GreenFolders\GreenFolders\Temp\%A_loopfilename%\*.PDF
	{

		SplitPath, A_LoopFileLongPath,,,, name

		FileGetTime, TimeLocal
		
		FilePDF := A_LoopFilename
		

		If fileExist("C:\Users\" A_UserName "\Documents\SetupArchive\" name ".PDF"){ 

			FileGetTime, TimeArchive, C:\Users\%A_UserName%\Documents\SetupArchive\%name%.PDF

			If (TimeLocal = TimeArchive)
				sleep 10
			Else
			{
				GoTo, CopyDrawings
			}
		}

		Else
		{
			GoTo, CopyDrawings
		}
	}
	return
}


CopyDrawings:
{
	FileCopy,  C:\Users\%A_username%\AppData\Local\GreenFolders\GreenFolders\Temp\%FileFolder%\%FilePDF%, C:\Users\%A_UserName%\Documents\SetupArchive, 1
	FileCopy,  C:\Users\%A_username%\AppData\Local\GreenFolders\GreenFolders\Temp\%FileFolder%\%FilePDF%, \\192.168.168.1\plats\SetupArchive, 1
	FileCopy,  C:\Users\%A_username%\AppData\Local\GreenFolders\GreenFolders\Temp\%FileFolder%\%FilePDF%, \\192.168.168.1\users\%A_UserName%\SetupArchive, 1
}
ShawnT
Posts: 20
Joined: 07 Mar 2017, 15:19

Re: Goto is exiting the loop after its used once.

29 Jan 2020, 14:49

Ok found this, so i put the lines of code above loop and it works perfectly.

https://autohotkey.com/board/topic/20943-question-about-loops-and-goto/
MannyKSoSo
Posts: 440
Joined: 28 Apr 2018, 21:59

Re: Goto is exiting the loop after its used once.

29 Jan 2020, 14:50

The problem is that your label isn't set up correctly. Labels continue execution until they hit a return or exit statement. So your new code would look something like this.

Code: Select all

MoveFiles:

Loop, C:\Users\%A_username%\AppData\Local\GreenFolders\GreenFolders\Temp\*, 2
{

if FileExist("C:\Users\" A_username "\AppData\Local\GreenFolders\GreenFolders\Temp\" A_loopfilename "")

	FileFolder := A_loopfilename

	Loop, C:\Users\%A_username%\AppData\Local\GreenFolders\GreenFolders\Temp\%A_loopfilename%\*.PDF
	{

		SplitPath, A_LoopFileLongPath,,,, name

		FileGetTime, TimeLocal
		
		FilePDF := A_LoopFilename
		

		If fileExist("C:\Users\" A_UserName "\Documents\SetupArchive\" name ".PDF"){ 

			FileGetTime, TimeArchive, C:\Users\%A_UserName%\Documents\SetupArchive\%name%.PDF

			If (TimeLocal = TimeArchive)
				sleep 10
			Else
			{
				GoTo, CopyDrawings
			}
		}

		Else
		{
			GoTo, CopyDrawings
		}
	}
	return
}


CopyDrawings:
	FileCopy,  C:\Users\%A_username%\AppData\Local\GreenFolders\GreenFolders\Temp\%FileFolder%\%FilePDF%, C:\Users\%A_UserName%\Documents\SetupArchive, 1
	FileCopy,  C:\Users\%A_username%\AppData\Local\GreenFolders\GreenFolders\Temp\%FileFolder%\%FilePDF%, \\192.168.168.1\plats\SetupArchive, 1
	FileCopy,  C:\Users\%A_username%\AppData\Local\GreenFolders\GreenFolders\Temp\%FileFolder%\%FilePDF%, \\192.168.168.1\users\%A_UserName%\SetupArchive, 1
return

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: macromint, Spawnova and 346 guests