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

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
scekcen
Posts: 4
Joined: 05 Feb 2021, 19:56

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

05 Feb 2021, 20:10

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.
User avatar
mikeyww
Posts: 27191
Joined: 09 Sep 2014, 18:38

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

05 Feb 2021, 22:20

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.
scekcen
Posts: 4
Joined: 05 Feb 2021, 19:56

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

06 Feb 2021, 07:38

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
anaglypta
Posts: 17
Joined: 10 Nov 2020, 10:18

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

06 Feb 2021, 07:56

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, ...
scekcen
Posts: 4
Joined: 05 Feb 2021, 19:56

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

06 Feb 2021, 08:23

Thank you for your answers. So how can I check if a file or folder has been selected?
BoBo
Posts: 6564
Joined: 13 May 2014, 17:15

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

06 Feb 2021, 08:46

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.
User avatar
mikeyww
Posts: 27191
Joined: 09 Sep 2014, 18:38

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

06 Feb 2021, 08:50

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
scekcen
Posts: 4
Joined: 05 Feb 2021, 19:56

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

06 Feb 2021, 09:39

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

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: Bing [Bot], Chunjee and 124 guests