Page 1 of 1

When you want to delete a folder or file by pressing Shift + Delete Keys, move this folder to a desired location

Posted: 05 Feb 2021, 20:10
by scekcen
Hello everybody, I'm new at AHK things. Please help me!

When I want to delete a folder or file by pressing the Shift + Delete Keys, I want to move this folder to a desired location instead of deleting it. I tried a lot of codes by doing a lot of research, but I was not successful. The last situation I come across in codes is this:

Code: Select all

Delete::
Send, ^c
;sleep, 1000
DestDir := "D:\Desktop\A"
selectedfolder = %clipboard%
;SplitPath, selectedfolder,, OutDir

;move all content of selected folder to parent folder
loop,files, %selectedfolder%\*.*, DFR 
    {
        FileMoveDir, %A_LoopFileFullPath%, %DestDir%, 1
        FileMove, %A_LoopFileFullPath%, %DestDir%, 1
    }
; Remove selectedfolder
		FileRemoveDir, %selectedfolder%, 1 
return

+Delete::
Send, ^c
;sleep, 1000
DestDir := "D:\Desktop\A"
selectedfolder = %clipboard%
;SplitPath, selectedfolder,, OutDir

;move all content of selected folder to parent folder
loop,files, %selectedfolder%\*.*, DFR 
    {
        FileMoveDir, %A_LoopFileFullPath%, %DestDir%, 1
        FileMove, %A_LoopFileFullPath%, %DestDir%, 1
    }
; Remove selectedfolder
		FileRemoveDir, %selectedfolder%, 1 
return
My main problem is that if I start this process without selecting a folder or a file, Windows starts to move the system folders into the folder I want. How can I solve this problem? Thanks for your help.

Re: When you want to delete a folder or file by pressing Shift + Delete Keys, move this folder to a desired location

Posted: 05 Feb 2021, 22:20
by mikeyww
You can use FileExist to see whether the folder exists. If not, you can Return.

If your goal is to move a folder, I don't think you would need to loop through all of its files. You can just move the folder.

Re: When you want to delete a folder or file by pressing Shift + Delete Keys, move this folder to a desired location

Posted: 06 Feb 2021, 07:38
by scekcen
Since the directory or file to be processed here does not have a specific name, I need to make sure that a folder or file is clicked or selected before the operation is performed. Otherwise, the script takes all files and folders in the Windows system folder and moves them to the folder I have specified. This situation is not what I want.

Before starting the process, I need to make sure that a folder or files are clicked / selected on the desktop. Thanks already for your help.

The final version of the script:

Code: Select all

+Delete::
KeyWait Shift
Send, ^c
;sleep, 1000
DestDir := "D:\Masaüstü\A"
selectedfolder = %clipboard%
SplitPath, selectedfolder,, OutDir

;move all content of selected folder to parent folder
loop,files, %selectedfolder%\*.*, DFR 
    {
        FileMove, %A_LoopFileFullPath%, %DestDir%, 1
		FileMoveDir, %A_LoopFileFullPath%, %DestDir%, 1
	}
; Remove selectedfolder
		FileRemoveDir, %selectedfolder%, 1 
return

Re: When you want to delete a folder or file by pressing Shift + Delete Keys, move this folder to a desired location

Posted: 06 Feb 2021, 07:56
by anaglypta
IIRC, FileMove can have odd side effects if you use it on a folder. Something like renaming the first file and bailing out. So you may want to do something like this:

Code: Select all

if InStr(A_LoopFileAttrib, "D")
	FileMoveDir, ...
else
	FileMove, ...

Re: When you want to delete a folder or file by pressing Shift + Delete Keys, move this folder to a desired location

Posted: 06 Feb 2021, 08:23
by scekcen
Thank you for your answers. So how can I check if a file or folder has been selected?

Re: When you want to delete a folder or file by pressing Shift + Delete Keys, move this folder to a desired location

Posted: 06 Feb 2021, 08:46
by BoBo
You could have a try using Explorers "copy to path"-option that is Shift+RightMouseButton+<a hotkey-character that depends on your local settings).
That way all selected files/folder paths will be copied to the clipboard where you can check them out & use for further processing. Good luck.

Re: When you want to delete a folder or file by pressing Shift + Delete Keys, move this folder to a desired location

Posted: 06 Feb 2021, 08:50
by mikeyww
If you want to use the text that you already have on the clipboard, it could be FileExist(Clipboard). If you want something more general for working directly with File Explorer, the following "get selection" function is handy.

https://www.autohotkey.com/boards/viewtopic.php?style=17&t=60403#p255256

Re: When you want to delete a folder or file by pressing Shift + Delete Keys, move this folder to a desired location

Posted: 06 Feb 2021, 09:39
by scekcen
Thank you everyone for their help and ideas. I solved the problem by adding the condition line so that the folder path to be operated on is desktop.

I'm adding the final version of the code here so that it might work for someone else.

Code: Select all

+Delete::
KeyWait Shift
Send, ^c
;sleep, 1000
DestDir := "C:\Users\Administrator\Desktop\Destination"
selectedfolder = %clipboard%
SplitPath, selectedfolder,, OutDir
;MsgBox %OutDir%
if (Outdir=="C:\Users\Administrator\Desktop") {
;move all content of selected folder to destination folder
loop,files, %selectedfolder%\*.*, DFR 
    {
      	FileMoveDir, %A_LoopFileFullPath%, %DestDir%, 1
        FileMove, %A_LoopFileFullPath%, %DestDir%, 1
        
    }
; Remove selectedfolder
       FileRemoveDir, %selectedfolder%, 1 
       ;FileDelete, %A_LoopFileFullPath%
       
		
		
}
return

Re: When you want to delete a folder or file by pressing Shift + Delete Keys, move this folder to a desired location

Posted: 06 Feb 2021, 10:07
by mikeyww
Thanks for posting the solution!