Link to home
Start Free TrialLog in
Avatar of Luis Diaz
Luis DiazFlag for Colombia

asked on

AutoHotkey: List files through a GUI

Hello experts,
I am trying to set up a file listing through an AutoHotkey GUI.
Here are the requirements:
Through a GUI report folder path in which will be generated the file.
List files in a new file and open it.
The final need is to add the following new features:
-File name and file full path differentiation
-Immediate and recurse differentiation
I am quite sure that the best approach for this is radio. However prior to propose something I would like to know:-
-If code structure is ok or should I review it.
-What is the best option to create the 2*2 differentiation.
Functions with switch case?
The differentiation is based on the:
Loop, Files, %FolderPath%\*.*, FD, R ; for recurse, blank for immediate
FileList = %FileList%% A_LoopFileName %`n; for file name
FileList = %FileList%% A_LoopFileFullPath %`n; for file full path

I am aware that I need to do some file tuning and add the controls. However I will do it following your recommendations.

I attached my initial version.

Thank you for your help.
AppendFilesList.ahk
SOLUTION
Avatar of Joe Winograd
Joe Winograd
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Bill Prew
Bill Prew

@Luis,

There are Windows GUI freeware programs out there that produce a file list, with some filtering and sorting options, etc.  Is there some reason why you are trying to build your own?  Can you share how you will use this?  Or is it more for learning exercise?

Just a few samples, although I couldn't quickly find "the one" that I had used a while back and found good.



»bp
Avatar of Luis Diaz

ASKER

@Joe:
>You should review it, especially the code that you copied from elsewhere...lines 16-28 (the Loop,Files code)
I tried to reviewed, please find attached file:
I took as a reference example provided at #4 provided at: https://www.autohotkey.com/docs/commands/LoopFile.htm
>Bad syntax...should not have a comma after the FD. In other words, the one, two, or three letters are all together in a single string after the third comma.
Ok, noted, have included in the proposal attached.
User generated image
Thank you for your help.
AppendListFiles.ahk
Hi Bill,

The idea is to have most of my daily actions centralized in a single application such as AutoHotkey.
I have a limited time to cover my tasks an I need to find a straightforward way to do this.
I realized that AutoHotkey trough a simple keyboard shortcut combination will help me to manage this in a swiftly manner.
Even if some application cover my need and are beyond expectations, they don't tackle my core need which is to enable me to launch the listing in a swiftly manner. Example: In 3-5 seconds I am able to cover the need without asking my self how the application works, which are the shortcuts associated, updates to relaunch, etc...

Regards,
Luis.
Hi Joe,
I reviewed the spaces as recommended in our previous questions.
Sorry for this.
I attached new version.
Regards,
Luis.
AppendListFiles.ahk
Hi Luis,

Before I review all of your new code carefully, a quick glance at it leads me to ask this question. When you write a line like this...

Loop, Files,%FolderPath%\*.*, FD R  ; Include Files and directories

...do you not notice or not care about the differences in the spaces? I ask only because I need to know if I should continue to mention it to you or simply forget about it and focus only on functionality.

There are numerous other examples, such as these two consecutive lines in the GUI:

Gui,Add,Button,x+10 gButtonCancelEx,&Cancel
Gui, Show,,File listing

And these two consecutive lines:

TempFile:=FolderPath . "\" . CurrentDateTimeFilesFolders . "_listing.txt"
FileList := ""

Then there's this line, which makes me cringe:

FileList=%FileList%%A_LoopFileName%`n

I've given you this advice before...never use = as the assignment operator...use only := as the assignment operator. In fact, = as an assignment operator doesn't even work in AHK V2.

Also, I'm pretty much going to stop mentioning indentation. I get the sense that you do care about it, but some combination of SciTE4AutoHotkey and the copy/paste process causes your posted code to have indentation that is different from what's on your PC. Regards, Joe
Obviously, our messages crossed. I'll look at your new version soon.
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Hi Joe,

I reviewed the various points except StrSplit, I need to think how to do this.
Let me know if this version is better or if there are some pending errors to review.

Regards,
Luis.
AppendListFiles.ahk
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Hi Joe,
>Rewrite using := as the assignment operator.
If I do this:
FileList:=%FileList%%A_LoopFileName%`n I got error message.
I suppose that I need to change the declaration however my knowledge is limited on this.

>StrSplit issue:
The problem is not how StrSplit works. I am aware of the arguments used.
What I cannot see is the purpose of Stringsplit in the function (for the current version).
Based on documentation related to Stringsplit, StringSplit,FileItem,A_LoopField,%A_Tab% is the outputvar, however I don't see this var anywhere declare in the script after the split.
I can transpose to StrSplit(A_LoopField,A_Tab) however I don't see what this bring to the script.
I made a comparison by executing the script with StringSplit, StrSplit and without any of the function and the content of 3 files are the same.
AppendListFiles.ahk
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Hi Joe,

>Assignment statement

Indeed we have discussed many times about this, I don't know why I didn't see it, I remembered that in our exchange I have the difficulties with % vs dot concatenation and the dot is the reference for concatenation when it comes variable assignment.
>"What makes you think that sorts by date?" I said that it doesn't, because FileList has A_LoopFileName at the beginning of each line, not A_LoopFileTimeModified. Now do you understand that comment?

Now I see the meaning of your comment, as long as you are not using A_loopFileTimeModified the sort cannot take place.
And as a result it doesn't make sense.

If we where in a situation that we need to Strsplit as we have: FileList:=FileList . A_LoopFileTimeModified "`t" A_LoopFileName "`n" I suppose that I need to take the first array if my delimiter is "`t",
the Strsplit declaration would take place withing the Loop,Parse,FileList,`n? or after you loop the entire FileList. Probably wrong question but I prefer to ask.

I attached last version. I hope we can continue with the other requirements.
AppendListFiles.ahk
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
>what are you trying to do inside the Loop,Parse with the built-in A_LoopField variable that you can't do inside the Loop,Files with the various built-in A_LoopFileXXXX variables?
Nothing more.
I took as a reference the example #4 of AutoHotkey documenation without thinking about the logic behind:

I adjusted as following:

^+f12::
Gui,Add,Text,,Report folder path in which will be generated file listing
Gui,Add,Edit,vFolderPath xm
Gui,Add,Radio,Checked vListing xm,&Immediate
Gui,Add,Radio,x+10,&Recurse
Gui,Add,Button,xm gButtonOKEx Default,&OK
Gui,Add,Button,x+10 gButtonCancelEx,&Cancel
Gui,Show,,File listing
Return

ButtonOKEx:
Gui,Submit

FormatTime,CurrentDateTimeFilesFolders,,yyyyMMdd_HHmmss
TempFile:=FolderPath . "\" . CurrentDateTimeFilesFolders . "_listing.txt"
FileList:=""
Loop,Files,%FolderPath%\*.*,FDR  ;Include Files and directories
{
		;~ FileList:=FileList . A_LoopFileTimeModified "`t" A_LoopFileName "`n" 
		FileList:=FileList . A_LoopFileName "`n" 		
}
	Clipboard:=FileList
	ClipWait,2 ; wait for information to appear on clipboard
If (ErrorLevel=1)
{
		MsgBox,4144,Error,No text appearing after 2 second
		Return
}
FileDelete,%TempFile%
FileAppend,%FileList%,%TempFile%
Run,%TempFile%
Return

ButtonCancelEx:
Gui,Destroy
Return

Open in new window


Sorry for the indentation issue, but I am still having the problem even with notepad++.

Let me know if I should adjust it.

Concerning the StrSplit, and based on the fact that I am going to remove the Loop,Parse loop, I prefer to take the time to think about this as I don't have the answer yet on how to use the function for this specific case.
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Hi Joe,
Thank you for your last comment.
I will reply latter on.
Concerning the StrSplit. I am trying to train myself by putting the StrSplit inside the Loop,Files

Loop,Files,%FolderPath%\*.*,FDR  ;Include Files and directories
{
		FileList:=FileList . A_LoopFileTimeModified "`t" A_LoopFileName "`n" 
		;~ FileList:=FileList . A_LoopFileName "`n"
		Delimiters:="`t"
		FileListSplitted:=StrSplit(FileList,Delimiters) 
		FileList2:=
} 

Open in new window


I have a blocking point here. Withing the loop,Parse I can use the Loop_Field attribute to combine this and take the second portion.
FileList2:=FileListSplitted[2] . A_LoopField "`n"  however I am doing this with a Loop,Files, and I don't know if I can use A_LoopField.

Could you please give me a hand to define the FileList2?

Thank you for your help.
Joe,
Concerning your comment: #a43031049
I supposed that the homework here is to set up two functions with switch case for the folder choice and file info choice?
Regards,
Luis.
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
>Look at the Special Variables Available Inside a File-Loop section. Is A_LoopField in there?
No is not there.
>Now look at the Remarks section in the Loop,Parse doc:
The built-in variable A_LoopField exists within any parsing loop

Ok now it is clear. I am going to give up the StrSplit for the moment. I was thinking about that because I would like to have a sort by date descending in the file list reported,
Sort, FileList  ; Sort by date.
However for this I supposed that I need A_LoopFileTimeModified

In any case, for the moment I am going to focus on the radios which is the main purpose of the question.
> No is not there.

Correct!

> The built-in variable A_LoopField exists within any parsing loop

Correct!

> Ok now it is clear.

Glad to hear it!

>  I am going to give up the StrSplit for the moment

The point is, you do not need StrSplit in this case.

> for this I supposed that I need A_LoopFileTimeModified

Exactly! And that already exists inside the Loop,Files. You do not need to StrSplit to get it...it is already there.

> In any case, for the moment I am going to focus on the radios which is the main purpose of the question.

The radio buttons (or any other way of getting user selections) is, as I've mentioned before, only half the battle. The other half is the actual processing based on the user selections. So the "main purpose" is not just the radios...it is also the processing based on the selected radios.
Hi Joe,
Understood, my last comment covers radio and also the process behind radios.
Prior to posting the process and avoid wrong directions on this, could you please advice on what is the best option to manage the process.

Based on the value of: vFolders (1,2 or 3) were are going to have:
-Loop,Files,%FolderPath%\*.*,FD ; vFolder=1 files and directories of root folder
-Loop,Files,%FolderPath%\*.*,R ; vFolder=2 files and directories of subfolders without listing the one's located at root folder  
-Loop,Files,%FolderPath%\*.*,FDR ; vFolder=3 files and directories of root folder and subfolders

I am not sure about the 2 options.
The initial intention of the question was to have files and folders at Root folder (without the recourse) and the second files and folder of root folder and subfolder (with the recourse) so simply have FD and FDR. Probably a misunderstanding. However if you prefer to work with the 3 choices I am in.

As regards vFileInfo (1 or 2):
FileList:=FileList . A_LoopFileName "`n" ; vFileInfo=1
FileList:=FileList .  A_LoopFileFullPath "`n" ; vFileInfo=2

What is your opinion on the best way to manage the process. Set up two functions in order to retrieve the required attributes. Set up one function in that case one related to vFolder or vFileInfo? or not setting functions and setting up the if or switch statements withing the GUI?
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Hi Joe,

Please find attached my first attempt.
I attached file in txt extension to avoid the indentation issue:
I sent you screenshot in case you see discrepancy concerning the indentation issue.
User generated imageI started the testing phase and the AutoHotkey script works as expected. I am going to continue the testing phase.
Let me know if I should review it.
Additional question: How can I reduce the height between FolderPath and next available radio selector?
User generated imageAppendListFiles.txt
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Hi Joe,

Just to confirm, lines to remove are the following:

User generated image
I continued the test and I haven't see issues for the moment. I am going to continue with the testing phase.
Another think that I would like know if the listing is performed by default by last modified date or it is performed based on the windows files attributes setting. Because the aim is to have a sort by last modified date descending, however if I can manage this with the built-in windows setting it is ok, otherwise I need to find a mechanism to sort by last modified date in descending.

Regards,
Luis.

I attached last version.


Regards,
Luis.
AppendListFiles.ahk
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Hi Joe,

Thank you for your previous recommendation.
I added the script to my AutoHotkey/Hotstring file and changed the hotkey combination to make it easy to use on a daily basis. I did some tests and it works for the moment.
I noticed one little issue but it is quite manageable. When I report a folder finishing with "\" (backslash) it will list full path with double "\\" (double backslash).
Example C:\temp\\file.txt I don't know if add a removal on the folderpath if last character is equal to "\" (backslash).
The workaround is very simple, just report the folder path without a "\" at the end.


The current version covers my key needs however I would like to go beyond with the following requirements:
1.Entitle first line of the file based on the file info selector: File full path or file name. This will allows me to have a proper file listing.
2.Add a new file selector composed by: A_LoopFileTimeModified "`t" A_LoopFileName "`n" and therefore have 3 files info choices. This will allows me to work on excel if I want to sort file listing or do another operations related to the date of files and folders. With this new choice I think that the requirement 1 required more work as they are two columns headers to report.

I can manage the second requirement however I would like to have your advice on how to report header title at line 1 (requirement 1)

If you prefer that I create a new question I can do it.

I also changed the buttons names related to ok and cancel as recommended in our previous exchange in order to avoid clashing with the other GUIs already added in my AutoHotkey/Hotstring and used on a daily basis.

;====================================
;Gui: list files and folders attributes
;====================================

^+f::
Gui,Add,Text,xm,Select folder for file listing:
Gui,Add,Edit,vFolderPath w400
Gui,Add,Text,,Select folders to process:
Gui,Add,Radio,Checked vRecurse,&Recurse
Gui,Add,Radio,x+10,&NoRecurse
Gui,Add,Text,xm,Select information to put in file listing:
Gui,Add,Radio,Checked vFileInfo,&Filename (without path)
Gui,Add,Radio,x+10,Filename with &Path
Gui,Add,Button,xm gButtonOKFilesFoldersListing Default,&OK
Gui,Add,Button,x+10 gButtonCancelFilesFoldersListing,&Cancel
Gui,Show,,File Listing
Return

ButtonOKFilesFoldersListing:
Gui,Submit

FormatTime,CurrentDateTimeFilesFolders,,yyyyMM
TempFile:=FolderPath . "\" . CurrentDateTimeFilesFolders . "_listing.txt"
FileList:=""
If (Recurse=1)
{
		LoopMode:="FDR"
}
Else
{
		LoopMode:="FD"
}
FileList:=""
Loop,Files,%FolderPath%\*.*,%LoopMode%
{
  If (FileInfo=1)
		{
    FileList:=FileList . A_LoopFileName . "`n"
  }
		Else
		{
    FileList:=FileList . A_LoopFileFullPath . "`n"
		}
}
Msgbox,,,LoopMode:%LoopMode% `nFileInfo:%FileInfo%
Clipboard:=FileList
ClipWait,2 ; wait for information to appear on clipboard
If (ErrorLevel=1)
{
		MsgBox,4144,Error,No text appearing after 2 seconds
		Return
}
Sort,FileList
FileDelete,%TempFile%
FileAppend,%FileList%,%TempFile%
Run,%TempFile%
Gui,Destroy
Return

ButtonCancelFilesFoldersListing:
Gui,Destroy
Return

Open in new window


Thank you very much for your help.

Regards,
Luis.
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thank you Joe.

1.I adjusted based on your recommendations
2.Added to my AutoHotkey/Hotstring file
3.Made some tests successfully

;====================================
;Gui: list files and folders attributes
;====================================

^+f::
Gui,Add,Text,xm,Select folder for file listing:
Gui,Add,Edit,vFolderPath w400
Gui,Add,Text,,Select folders to process:
Gui,Add,Radio,Checked vRecurse,&Recurse
Gui,Add,Radio,x+10,&NoRecurse
Gui,Add,Text,xm,Select information to put in file listing:
Gui,Add,Radio,Checked vFileInfo,&Filename (without path)
Gui,Add,Radio,x+10,Filename with &Path
Gui,Add,Button,xm gButtonOKFilesFoldersListing Default,&OK
Gui,Add,Button,x+10 gButtonCancelFilesFoldersListing,&Cancel
Gui,Show,,File Listing
Return

ButtonOKFilesFoldersListing:
Gui,Submit
If (SubStr(FolderPath,0,1)!="\")
  FolderPath:=FolderPath . "\"
FormatTime,CurrentDateTimeFilesFolders,,yyyyMM
TempFile:=FolderPath . CurrentDateTimeFilesFolders . "_listing.txt"
FileList:=""
If (Recurse=1)
{
		LoopMode:="FDR"
}
Else
{
		LoopMode:="FD"
}
FileList:=""
Loop,Files,%FolderPath%*.*,%LoopMode%
{
  If (FileInfo=1)
		{
    FileList:=FileList . A_LoopFileName . "`n"
  }
		Else
		{
    FileList:=FileList . A_LoopFileFullPath . "`n"
		}
}
Msgbox,,,LoopMode:%LoopMode% `nFileInfo:%FileInfo%
Clipboard:=FileList
ClipWait,2 ; wait for information to appear on clipboard
If (ErrorLevel=1)
{
		MsgBox,4144,Error,No text appearing after 2 seconds
		Return
}
Sort,FileList
FileDelete,%TempFile%
FileAppend,%FileList%,%TempFile%
Run,%TempFile%
Gui,Destroy
Return

ButtonCancelFilesFoldersListing:
Gui,Destroy
Return

Open in new window


Screenshot to prove the right indentation: :-)
User generated image
Let me know if you see an issue otherwise I will close the question.

In the meantime I will create the new question. Thank you very much for your help on this!
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Noted Joe, I will keep the question opened in order to keep in mind that I need to review it based on comment: #a43031623 on the computer in which I have my reference AutoHotkey/Hotstring file.

>OK, although I may not be able to look at it until tomorrow.
No issue I can wait.
Hi Joe,

Given the fact that we have the question: Question 29172877 going on. I proceed to close this question. Thank you very much for your help.

Regards,
Luis.
You're welcome, Luis, I'll try to get to your new question later today. Regards, Joe