Array or Variable Issue

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
User avatar
coder_chick
Posts: 235
Joined: 05 Nov 2015, 10:43

Array or Variable Issue

Post by coder_chick » 05 Feb 2023, 16:25

I'm having some issues with the "check if file exists" and "check if file is a folder" validation steps of my code. I learn a lot better with code examples or edits to my own code, so please provide example code so I can see an recommendations in action.

Code: Select all

#NoEnv ; Recommended for performance and compatibility with future AutoHotkey releases.
SendMode Input ; Recommended for new scripts due to its superior speed and reliability.
SetWorkingDir %A_ScriptDir% ; Ensures a consistent starting directory.
#SingleInstance Prompt

Gui, Font, s12, Tahoma
Gui, Add, Text, x12 y9 w110 h30 , Output folder:
Gui, Add, Button, x502 y9 w100 h40 gBrowseButton, Browse
Gui, Add, Text, x132 y9 w360 h30 , vOutputFolder
Gui, Add, Text, x12 y49 w250 h20 , Paste Full File Paths (1 per line):
Gui, Add, Edit, x12 y79 w590 h200 vFilePaths, 
Gui, Add, Button, x492 y299 w120 h50 gStartButton, Start
Gui, Show, x127 y87 h364 w622, New GUI Window
Return

GuiClose:
ExitApp

BrowseButton:
Gui, Submit, NoHide
FileSelectFolder, OutputFolder, 3, Select output folder
Gui, Submit, NoHide
Return

StartButton:
Gui, Submit, Hide
FileDelete, %A_ScriptDir%\batch_file.bat
FileDelete, %A_ScriptDir%\errorlog.txt

; Split file paths into an array
StringSplit, FilePathsArray, FilePaths, `n

; Loop through the file paths array
Loop, % FilePathsArray0
{
; Skip empty rows
if (FilePathsArray%A_Index% = "")
continue

; Check if file exists
if (!FileExist(FilePathsArray%A_Index%)) {
FileAppend, %FilePathsArray%A_Index% does not exist.`n, %A_ScriptDir%\errorlog.txt
continue
}

; Check if file is a folder
if (A_IsDir(FilePathsArray%A_Index%)) {
    FileAppend, %FilePathsArray%A_Index% is a folder, not a file.`n, %A_ScriptDir%\errorlog.txt
    continue
}

; Extract file name from file path
FileName := RegExReplace(FilePathsArray%A_Index%, "(.*[\\\/])", "")

; Extract source path from file path
FileSourcePath := RegExReplace(FilePathsArray%A_Index%, "(.*)[\\\/].*", "$1")


; Append robocopy command to batch file
FileAppend, robocopy "%FileSourcePath%" "%OutputFolder%" "%FileName%" /tee /ts /v /fp /np /copy:dat /w:0 /r:0 /log+:"%OutputFolder%\log.txt"`n, %A_ScriptDir%\batch_file.bat

}

MsgBox, Batch file created successfully at %A_ScriptDir%\batch_file.bat.
Return
♥ ❤ ❥ coder_chick ♥ ❤ ❥

User avatar
mikeyww
Posts: 26848
Joined: 09 Sep 2014, 18:38

Re: Array or Variable Issue

Post by mikeyww » 05 Feb 2023, 18:26

Hello,

Have you tried displaying the value of your file path variable, so that you know what it is?

"Having some issues": should we understand what this means? What does the current script actually do? What should it do? Is a specific line a problem? If so, which one, and what is the problem? I understand a lot better with an explanation of what you are experiencing! At the moment, I cannot recommend anything, because I do not know what problem you are having.

User avatar
boiler
Posts: 16900
Joined: 21 Dec 2014, 02:44

Re: Array or Variable Issue

Post by boiler » 05 Feb 2023, 20:02

@coder_chick — Your code has some telltale signs, mainly incorrect syntax and non-existent AHK functionality, that are characteristic of ChatGPT output. If you actually wrote this code and can explain how it is supposed to work (and post the supporting code that is not in your script but would have to exist), then I’ll withdraw my suspicions. Otherwise, I’ll ask you to not bother the forum members with requests for help in debugging ChatGPT-generated drivel, especially when it’s not represented as such.

User avatar
coder_chick
Posts: 235
Joined: 05 Nov 2015, 10:43

Re: Array or Variable Issue

Post by coder_chick » 05 Feb 2023, 21:01

Well, boiler, you are indeed correct. However, if you look, I've been a member of this forum for years (prior to chatgpt). I don't think there is anything wrong with trying to leverage Chatgpt for help with something as I did. I'm not a savvy coder, but have been doing my best. If you're bothered by it now, just wait. It isn't going away and you'll only see more of it. AI is the future man. Its no different than someone using Google for help before bothering someone with a question. I tried to figure it out, couldn't, so now I'm here.

The script itself is pretty simple. You select an output directory, paste in a list of file paths, and it generates a robocopy copy batch script that robocopies those select files to the output directory (and no, I don't want to run robocopy directory with AHK).
♥ ❤ ❥ coder_chick ♥ ❤ ❥

User avatar
mikeyww
Posts: 26848
Joined: 09 Sep 2014, 18:38

Re: Array or Variable Issue

Post by mikeyww » 05 Feb 2023, 21:07

I speak just for myself here. I see nothing wrong with using the tool yourself, but bringing a bunch of convoluted code to the forum and asking the responders to fix it for you-- well, it's just too much for my own liking, and I agree with boiler that I find it a waste of time, at least for me. I find it unpleasant, and it conveys to me a lack of interest from the person who posts the output. It's also often garbage as already noted, code that isn't even fixable without rewriting the script. There are commands that shouldn't be there and some that do not even exist as valid commands or syntax. Who wants to rewrite such trash, and why do you ask them to do it?

I will simply add that at this time, the forum is not supporting such automated coding. My request and hope would be for people to follow the simple guidelines, and contact the moderators or admins with questions in advance.
Its no different than someone using Google for help
I disagree with this, too. Attempts to write code differ from attempts to use a chatbot or AI bot to spit out some stuff. In many cases, it appears that the people posting this stuff do not even bother to look at it before they post it. I can only imagine that this is the case with the script posted in this thread, too. A member for years, 234 posts, so what stops such a person from having a first pass at revision? Or even running the script once?
The script itself is pretty simple.
I often find this comment to be off base. If it's really simple, you wouldn't be here, so instead of talking about how simple it all is, I would consider acknowledging that real work is involved to craft the right solution, or perhaps just don't mention it at all!

User avatar
boiler
Posts: 16900
Joined: 21 Dec 2014, 02:44

Re: Array or Variable Issue

Post by boiler » 05 Feb 2023, 22:29

coder_chick wrote: If you're bothered by it now, just wait. It isn't going away and you'll only see more of it. AI is the future man. Its no different than someone using Google for help before bothering someone with a question. I tried to figure it out, couldn't, so now I'm here.
You’re not using AI as a tool. If so, you would have taken its output as a starting point and tried working out something meaningful from it. That’s the future of AI. Not dumping a bunch of nonsensical code into a forum post and claiming you can’t figure out what’s wrong with the code you’ve written. I’m sure there are those using it as a tool to give themselves a starting point, and we don’t recognize when they’re doing that precisely because they have already evaluated it and discarded it as useless or put the effort into removing the nonsense. You and some others have done neither. That’s what we’re not going to allow the forum to be cluttered with.

When AI coding tools reach a point where their output is directly useful, it will be a game changer. It’s not close to that yet if ChatGPT is the best evidence. Your post is a case in point as to why we have the policy we do about why it’s not allowed on the site at this point, as posted in the Forum Rules. As noted there, this forum isn’t the only programming help site that is taking this stance given the current state of the art in AI-generated code. It currently causes more problems and wastes more time than any benefit it might provide, and it’s not even close.

User avatar
Chunjee
Posts: 1417
Joined: 18 Apr 2014, 19:05
Contact:

Re: Array or Variable Issue

Post by Chunjee » 05 Feb 2023, 22:58

Note: If possible, always use the object-based array. It is superior to a pseudo-array in almost every aspect: it is space-saving, more flexible, clearer, and similar to many other programming languages.
StringSplit is also depreciated. Use the StrSplit() instead.

User avatar
coder_chick
Posts: 235
Joined: 05 Nov 2015, 10:43

Re: Array or Variable Issue

Post by coder_chick » 06 Feb 2023, 00:12

I am not a big coder. I've dabbled with it for years and written many little scripts to help me with my day to day. @mikeyww has helped me with issues for a long time, and I have greatly appreciated his assistance and learned a lot from him.

I haven't grasped the concept/usage of arrays yet, but I thought maybe one one could be used in this case. After struggling to get code of my own to work using an array, I went to chatgpt, and then to the forum. [Sorry didn't know about the updated community rules around chatgpt]

I will admit, since I don't really get arrays too well, I was under the impression ChatGPT's code was close. I didn't do a great job of describing my problem initially because I thought it was going to be a small mistake or something that would jump out to a senior ahk guru.

I actually know how to make my script function the way I want without using an array. It was when I tried to do it via an array that everything fell apart.

I'll just leave my working code (which I have cleaned up some other things) that doesn't use an array for others should they need it in the future:

Code: Select all

#NoEnv ; Recommended for performance and compatibility with future AutoHotkey releases.
SendMode Input ; Recommended for new scripts due to its superior speed and reliability.
SetWorkingDir %A_ScriptDir% ; Ensures a consistent starting directory.
#SingleInstance Prompt

FileDelete, %A_ScriptDir%\batch_file.bat
FileDelete, %A_ScriptDir%\errorlog.txt

Gui, Font, s12, Tahoma
Gui, Add, Text, x12 y9 w110 h30 , Output folder:
Gui, Add, Button, x502 y9 w100 h40 gBrowseButton, Browse
Gui, Add, Text, x132 y9 w360 h30 vOutputFolder, 
Gui, Add, Text, x12 y49 w250 h20 , Paste Full File Paths (1 per line):
Gui, Add, Edit, x12 y79 w590 h200 vFilePaths, 
Gui, Add, Button, x492 y299 w120 h50 gStartButton, Start
Gui, Show, x127 y87 h364 w622, New GUI Window
Return

GuiClose:
ExitApp

BrowseButton:
Gui, Submit, NoHide
FileSelectFolder, OutputFolder, 3, Select output folder
Gui, Submit, NoHide
Return

StartButton:
Gui, Submit, Hide

Loop, Parse, FilePaths, `n
{

;MsgBox, Line %A_Index% is %A_LoopField%

  ;skip empty line
  if (A_LoopField = "")
  continue

    ; Check if file exists
	IfNotExist,%A_LoopField%
{
	FileAppend,File Does Not Exist:%A_LoopField%`n, %A_ScriptDir%\errorlog.txt
    continue
}

	;check if folder and not a file
	FileGetAttrib, Attributes, %A_LoopField%
	IfInString, Attributes, D
{
		FileAppend,Item is folder and not a file:%A_LoopField%`n, %A_ScriptDir%\errorlog.txt
		continue
}
 
  ; Extract file name from file path
  FileName := RegExReplace(A_LoopField, "(.*[\\\/])", "")

  ; Extract source path from file path
  FileSourcePath := RegExReplace(A_LoopField, "(.*)[\\\/].*", "$1")

  ; Append robocopy command to batch file
  FileAppend, robocopy "%FileSourcePath%" "%OutputFolder%" "%FileName%" /tee /ts /v /fp /np /copy:dat /w:0 /r:0 /log+:"%OutputFolder%\log.txt"`n, %A_ScriptDir%\batch_file.bat


}

MsgBox, Batch file created successfully at %A_ScriptDir%\batch_file.bat.
Return
♥ ❤ ❥ coder_chick ♥ ❤ ❥

Post Reply

Return to “Ask for Help (v1)”