Deleting sub-folders and files Topic is solved

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
Lem2001
Posts: 127
Joined: 27 Jun 2017, 17:59

Deleting sub-folders and files

20 Mar 2020, 20:43

1. How do I delete only sub-folders from a specified folder? (without having to name all the sub-folders individually and use FileRemoveDir for each one).

2. Is there a way to specify exceptions to a file *.* delete command? (in other words: delete all files in a folder except for certain named files). If not, then what would be the best way to achieve the same outcome?
User avatar
boiler
Posts: 17075
Joined: 21 Dec 2014, 02:44

Re: Deleting sub-folders and files

20 Mar 2020, 22:42

Code: Select all

Loop, Files, C:\MyFolderWithSubfolders\*.*, D ; D indicates directories only
    FileDelete, % A_LoopFileFullPath
Regarding your second question, I would put the exception names in an array, and when you loop through all the files, check each one against all the names in the array and don’t delete it if it is found in that list.

For all of the above, test everything on dummy files and directories first so you don’t accidentally delete anything important.
Lem2001
Posts: 127
Joined: 27 Jun 2017, 17:59

Re: Deleting sub-folders and files

21 Mar 2020, 01:30

Thanks.

Should this work even if the sub-folders contain content, or does it require R for recurse? I ask because I replaced the path in your example with my test path but nothing is happening. I did try adding R just in case, but still nothing.

I literally just pasted my path over your example path (I presume that there's nothing else that needs to be done) and then assigned it a hotkey to run it for testing. I left the path in 'legacy' format with no quotes (as you did) so I don't think I've messed up that bit. What am I missing?

For the second issue, I've looked up how to put things into an array, but what I don't know how to do is: "... check each one against all the names in the array and don’t delete it"
User avatar
boiler
Posts: 17075
Joined: 21 Dec 2014, 02:44

Re: Deleting sub-folders and files  Topic is solved

21 Mar 2020, 05:22

Lem2001 wrote: Thanks.

Should this work even if the sub-folders contain content, or does it require R for recurse? I ask because I replaced the path in your example with my test path but nothing is happening. I did try adding R just in case, but still nothing.

I literally just pasted my path over your example path (I presume that there's nothing else that needs to be done) and then assigned it a hotkey to run it for testing. I left the path in 'legacy' format with no quotes (as you did) so I don't think I've messed up that bit. What am I missing?
Sorry, my mistake. Should have used FileRemoveDir with the 1 (recurse) option:

Code: Select all

Loop, Files, C:\MyFolderWithSubfolders\*.*, D ; D indicates directories only
    FileRemoveDir, % A_LoopFileFullPath, 1


Lem2001 wrote: For the second issue, I've looked up how to put things into an array, but what I don't know how to do is: "... check each one against all the names in the array and don’t delete it"
You would loop through your array values within your file loop and compare them to A_LoopFileName, like this:

Code: Select all

Exceptions := ["file1.txt", "file2.txt"]
Loop, Files, C:\MyPath\*.*, F
{
    Found := 0
    for Each, Filename in Exceptions
        if (Filename = A_LoopFileName)
            Found := 1
    if !Found
        FileDelete, % A_LoopFileFullPath
}        
Lem2001
Posts: 127
Joined: 27 Jun 2017, 17:59

Re: Deleting sub-folders and files

21 Mar 2020, 12:40

boiler wrote:
21 Mar 2020, 05:22
Sorry, my mistake. Should have used FileRemoveDir with the 1 (recurse) option:
Thank you for the correction. It now works exactly as intended.

boiler wrote:
21 Mar 2020, 05:22
You would loop through your array values within your file loop and compare them to A_LoopFileName, like this:
I appreciate you writing out an example in full that I can use as a template. I can confirm that this also works perfectly, and it's a really useful bit of code that I will be able to adapt to various other uses in future.

Thanks again for your help.

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: Bing [Bot] and 168 guests