Drag and Drop folders to a pop up gui Topic is solved

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
haomingchen1998
Posts: 176
Joined: 20 Feb 2023, 16:37

Drag and Drop folders to a pop up gui

29 Oct 2023, 18:30

My code let me select a folder from GUI, and rename the files inside this folder with modified_date as prefix. Is it possible to make it so, I can just drag and drop multiple folders to this GUI instead of me manually selecting folders? Thanks!

Code: Select all

dateTimePrefixPattern := "^\d{2}-\d{2}-\d{4} {\d{2}\.\d{2}\.\d{2}\.\w{2}}_"

; Open a GUI for folder selection
FileSelectFolder, sourceFolder

; If the user didn't select a folder, exit the script
if (sourceFolder = "")
    ExitApp

; Loop through all files and subfolders in the selected folder
Loop, Files, %sourceFolder%\*, R
{
    if (RegExMatch(A_LoopFileName, dateTimePrefixPattern))
        continue
    FileGetTime, modifiedTime, %A_LoopFileLongPath%, M
    FormatTime, modifiedTime, %modifiedTime%, MM-dd-yyyy {hh.mm.ss.tt}_
    newFilename := A_LoopFileDir . "\" . modifiedTime . "_" . A_LoopFileName
    FileMove, %A_LoopFileLongPath%, %newFilename%
}

voice := ComObjCreate("SAPI.SpVoice")
voice.volume := 100
voice.Speak("Files Renamed")
Exitapp
User avatar
Hellbent
Posts: 2114
Joined: 23 Sep 2017, 13:34

Re: Drag and Drop folders to a pop up gui

29 Oct 2023, 19:06

check this out.

F.Y.I. The ListBox control is just for demo purposes.
.
drag and drop 1.gif
drag and drop 1.gif (179.87 KiB) Viewed 699 times
.

Code: Select all

Gui, Show, w600 h300

return
*ESC::ExitApp

GuiDropFiles:
	arr := StrSplit( A_GuiEvent, "`n" )
	outString := ""
	for k , v in arr	
		outString .= v "|"
	Gui, Add, ListBox, xm ym w580 h280 , % outString 
	return

haomingchen1998
Posts: 176
Joined: 20 Feb 2023, 16:37

Re: Drag and Drop folders to a pop up gui

30 Oct 2023, 17:34

Hellbent wrote:
29 Oct 2023, 19:06
check this out.

F.Y.I. The ListBox control is just for demo purposes.
.
drag and drop 1.gif
.

Code: Select all

Gui, Show, w600 h300

return
*ESC::ExitApp

GuiDropFiles:
	arr := StrSplit( A_GuiEvent, "`n" )
	outString := ""
	for k , v in arr	
		outString .= v "|"
	Gui, Add, ListBox, xm ym w580 h280 , % outString 
	return

Unfortunately, this GUI didn't work for me. Perhaps because I'm using Ahk Version 1.1.36.02?
Image
User avatar
Hellbent
Posts: 2114
Joined: 23 Sep 2017, 13:34

Re: Drag and Drop folders to a pop up gui

31 Oct 2023, 14:04

Did you run the code on it's own (no other code)?

If you are using a named gui (other than "1") you will need to edit the name of the label.

If for example you name your gui "Settings" you will need to change the "DropFiles" label to SettingsGuiDropFiles

Try this.

Code: Select all

Gui, Show, w600 h300

return
*ESC::ExitApp

GuiDropFiles:
	arr := StrSplit( A_GuiEvent, "`n" )
	outString := ""
	for k , v in arr	
		outString .= v "|"

	Gui, Add, ListBox, xm ym w580 h280 , % outString 
	MsgBox, % A_GuiEvent
	MsgBox, % outString
	return

haomingchen1998
Posts: 176
Joined: 20 Feb 2023, 16:37

Re: Drag and Drop folders to a pop up gui

31 Oct 2023, 19:38

Hellbent wrote:
31 Oct 2023, 14:04
Did you run the code on it's own (no other code)?

If you are using a named gui (other than "1") you will need to edit the name of the label.

If for example you name your gui "Settings" you will need to change the "DropFiles" label to SettingsGuiDropFiles

Try this.

Code: Select all

Gui, Show, w600 h300

return
*ESC::ExitApp

GuiDropFiles:
	arr := StrSplit( A_GuiEvent, "`n" )
	outString := ""
	for k , v in arr	
		outString .= v "|"

	Gui, Add, ListBox, xm ym w580 h280 , % outString 
	MsgBox, % A_GuiEvent
	MsgBox, % outString
	return

I found the cause, it's because my new script template always have #UseHook and run as admin script, removed that solved the issue. Can you also give me some ideas on how do I make this GUI work with my script below? I'm was trying to replace sourceFolder with outString, but kept getting errors. Thanks!

Code: Select all

dateTimePrefixPattern := "^\d{2}-\d{2}-\d{4} {\d{2}\.\d{2}\.\d{2}\.\w{2}}_"

; Open a GUI for folder selection
FileSelectFolder, sourceFolder

; If the user didn't select a folder, exit the script
if (sourceFolder = "")
    ExitApp

; Loop through all files and subfolders in the selected folder
Loop, Files, %sourceFolder%\*, R
{
    if (RegExMatch(A_LoopFileName, dateTimePrefixPattern))
        continue
    FileGetTime, modifiedTime, %A_LoopFileLongPath%, M
    FormatTime, modifiedTime, %modifiedTime%, yyyy-MM-dd {hh.mm.ss.tt}_
    newFilename := A_LoopFileDir . "\" . modifiedTime . "_" . A_LoopFileName
    FileMove, %A_LoopFileLongPath%, %newFilename%
}

voice := ComObjCreate("SAPI.SpVoice")
voice.volume := 100
voice.Speak("Files Renamed")
Exitapp
User avatar
Hellbent
Posts: 2114
Joined: 23 Sep 2017, 13:34

Re: Drag and Drop folders to a pop up gui  Topic is solved

01 Nov 2023, 15:33

Give this a try.

Code: Select all

#SingleInstance Force


Gui1 := {} 								;create a structure to hold the variables associated with the window. (henceforth: "Object")
Gui, New, +AlwaysOnTop +hwndhwnd  		;create your new window
Gui1.Hwnd := hwnd						;add the handle of the window to the window object.

;add other controls 
;blah blah
;blah
Gui, Show, w300 h300 ,DropFiles Demo	;Show the window




Return									;End of Auto-Execute Section

GuiClose:								;exit routine
GuiContextMenu:
*Esc::ExitApp


GuiDropFiles:	;{

	if( A_Gui != Gui1.Hwnd )							;Do nothing if it's the wrong window
		return
	
	arr := StrSplit( A_GuiEvent , "`n" )				;split the paths 

	dateTimePrefixPattern := "^\d{2}-\d{2}-\d{4} {\d{2}\.\d{2}\.\d{2}\.\w{2}}_"
	
	for k , v in arr	{								;loop through the paths
		FileGetAttrib, Attributes, % arr[ k ]			;get the Attributes
		if( instr( Attributes , "D" ) ){				;test if the path is a directory

			msgbox, % "This is a folder`n" arr[ k ]		;the path is a directory, have at it.

		}else{
			msgbox, % "NOT a folder`n" arr[ k ]			;the path isn't a directory.
			
		}
	}
	
	;your old code.
	/*	
	; Loop through all files and subfolders in the selected folder
	Loop, Files, %sourceFolder%\*, R
	{
	    if (RegExMatch(A_LoopFileName, dateTimePrefixPattern))
	        continue
	    FileGetTime, modifiedTime, %A_LoopFileLongPath%, M
	    FormatTime, modifiedTime, %modifiedTime%, yyyy-MM-dd {hh.mm.ss.tt}_
	    newFilename := A_LoopFileDir . "\" . modifiedTime . "_" . A_LoopFileName
	    FileMove, %A_LoopFileLongPath%, %newFilename%
	}
	*/
	voice := ComObjCreate("SAPI.SpVoice")
	voice.volume := 100
	voice.Speak("Files Renamed")
	return
;}

			
haomingchen1998
Posts: 176
Joined: 20 Feb 2023, 16:37

Re: Drag and Drop folders to a pop up gui

01 Nov 2023, 20:29

Hellbent wrote:
01 Nov 2023, 15:33
Give this a try.

Code: Select all

#SingleInstance Force


Gui1 := {} 								;create a structure to hold the variables associated with the window. (henceforth: "Object")
Gui, New, +AlwaysOnTop +hwndhwnd  		;create your new window
Gui1.Hwnd := hwnd						;add the handle of the window to the window object.

;add other controls 
;blah blah
;blah
Gui, Show, w300 h300 ,DropFiles Demo	;Show the window




Return									;End of Auto-Execute Section

GuiClose:								;exit routine
GuiContextMenu:
*Esc::ExitApp


GuiDropFiles:	;{

	if( A_Gui != Gui1.Hwnd )							;Do nothing if it's the wrong window
		return
	
	arr := StrSplit( A_GuiEvent , "`n" )				;split the paths 

	dateTimePrefixPattern := "^\d{2}-\d{2}-\d{4} {\d{2}\.\d{2}\.\d{2}\.\w{2}}_"
	
	for k , v in arr	{								;loop through the paths
		FileGetAttrib, Attributes, % arr[ k ]			;get the Attributes
		if( instr( Attributes , "D" ) ){				;test if the path is a directory

			msgbox, % "This is a folder`n" arr[ k ]		;the path is a directory, have at it.

		}else{
			msgbox, % "NOT a folder`n" arr[ k ]			;the path isn't a directory.
			
		}
	}
	
	;your old code.
	/*	
	; Loop through all files and subfolders in the selected folder
	Loop, Files, %sourceFolder%\*, R
	{
	    if (RegExMatch(A_LoopFileName, dateTimePrefixPattern))
	        continue
	    FileGetTime, modifiedTime, %A_LoopFileLongPath%, M
	    FormatTime, modifiedTime, %modifiedTime%, yyyy-MM-dd {hh.mm.ss.tt}_
	    newFilename := A_LoopFileDir . "\" . modifiedTime . "_" . A_LoopFileName
	    FileMove, %A_LoopFileLongPath%, %newFilename%
	}
	*/
	voice := ComObjCreate("SAPI.SpVoice")
	voice.volume := 100
	voice.Speak("Files Renamed")
	return
;}

			
I can't thank you enough for getting me started, thank you SO much! I made some final touches to make it look nicer and display the path in the GUI. Just gonna share the completed script in case someone also needs a script that rename all files inside a folders as modified date in year-month-day {12 hours time} format. Have a great day!

Code: Select all

Gui1 := {} 								
Gui, New, +AlwaysOnTop +hwndhwnd  		
Gui1.Hwnd := hwnd						

Gui, Add, ListView, w500 h400 vMyListView, Name  
Gui, Add, Button, gStartRenaming x250 y420, Rename    

Gui, Show, w550 h450 ,DropFiles to Rename with modified date in [yyyy-MM-dd + time]	

Return									

GuiClose:								
GuiContextMenu:
*Esc::ExitApp

GuiDropFiles:
	if( A_Gui != Gui1.Hwnd )
		return
	arr := StrSplit( A_GuiEvent , "`n" )
	dateTimePrefixPattern := "^\d{4}-\d{2}-\d{2} {\d{2}\.\d{2}\.\d{2}\.\w{2}}_"
	for k , v in arr	{
		FileGetAttrib, Attributes, % arr[ k ]
		if( instr( Attributes , "D" ) ){
			LV_Add("", arr[k])  
		}
	}
	return

StartRenaming:
	Loop, % LV_GetCount()  
	{
		LV_GetText(sourceFolder, A_Index)  
		Loop, Files, %sourceFolder%\*, R
		{
		    if (RegExMatch(A_LoopFileName, dateTimePrefixPattern))
		        continue
		    FileGetTime, modifiedTime, %A_LoopFileLongPath%, M
		    FormatTime, modifiedTime, %modifiedTime%, yyyy-MM-dd {hh.mm.ss.tt}_
		    newFilename := A_LoopFileDir . "\" . modifiedTime . "_" . A_LoopFileName
		    FileMove, %A_LoopFileLongPath%, %newFilename%
		}
	}
	voice := ComObjCreate("SAPI.SpVoice")
	voice.volume := 100
	voice.Speak("Files Renamed")
    ExitApp
return

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: Google [Bot], peter_ahk and 331 guests