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?
Deleting sub-folders and files Topic is solved
Re: Deleting sub-folders and files
Code: Select all
Loop, Files, C:\MyFolderWithSubfolders\*.*, D ; D indicates directories only
FileDelete, % A_LoopFileFullPath
For all of the above, test everything on dummy files and directories first so you don’t accidentally delete anything important.
Re: Deleting sub-folders and files
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"
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"
Re: Deleting sub-folders and files Topic is solved
Sorry, my mistake. Should have used FileRemoveDir with the 1 (recurse) option: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?
Code: Select all
Loop, Files, C:\MyFolderWithSubfolders\*.*, D ; D indicates directories only
FileRemoveDir, % A_LoopFileFullPath, 1
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
}
Re: Deleting sub-folders and files
Thank you for the correction. It now works exactly as intended.boiler wrote: ↑21 Mar 2020, 05:22Sorry, my mistake. Should have used FileRemoveDir with the 1 (recurse) option:
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.
Who is online
Users browsing this forum: Google [Bot] and 198 guests