Page 1 of 1

need safe wild card for FileRemoveDir

Posted: 01 Aug 2018, 10:41
by AlleyArtwork
I'm looking for the correct way to do this, i'm trying to remove additional directories created by roaming profiles, example:
becca.smith
becca.smith.001
becca.smith.002

but I realized that i'll have issues with similar username such as "rebecca.smith", where becca.smith and rebecca.smith are 2 unique users.
In this example, What's the right way to do this without deleting rebecca.smith? or am i going about this entirely wrong?

Since this doesn't work: FileRemoveDir, \\path\users\becca.smith*, 1
My workaround to loop the directory, then match foldername with IfInString, would remove both users, becca.smith as well as rebecca.smith

Code: Select all

username = becca.smith

loop, \\path\users\*, 2, 0
{
	IfInString, a_loopfilename, %username%.			
		FileRemoveDir, %A_LoopFileFullPath%, 1
		
}

Re: need safe wild card for FileRemoveDir  Topic is solved

Posted: 01 Aug 2018, 11:18
by colt
I am not sure what you file structure is like but could you check instead:

Code: Select all

IfInString, A_LoopFileFullPath, \%username%.
This would ensure you have their entire first name.

*fixed typo

Re: need safe wild card for FileRemoveDir

Posted: 01 Aug 2018, 12:00
by AlleyArtwork
Hi Colt, It's so simple i feel embarrassed!
I'll test it and confirm shortly, but yes I think that will work. Thank you very much!

[edit]
Successfully tested with the following local test. This method doesn't delete the rebecca.smith folders.
I suppose i'll still need to be careful if there's a "becca.smithee" now.

[Edit 2]
I decided that when deleting the path for a user "becca.smith" i'm just going to have a line that removes that directory specifically, then after that will be the loop to look for matches to " \%username%. " to make sure the user.* folders are deleted without partial matches either (becca.smithee scenario).
Thanks guys!

Code: Select all

FileCreateDir, c:\temp\becca.smith
FileCreateDir, c:\temp\becca.smith.1
FileCreateDir, c:\temp\becca.smith.2
FileCreateDir, c:\temp\becca.smith.3
FileCreateDir, c:\temp\rebecca.smith
FileCreateDir, c:\temp\rebecca.smith.1
FileCreateDir, c:\temp\rebecca.smith.2
FileCreateDir, c:\temp\rebecca.smith.3

username = becca.smith

FileRemoveDir, c:\temp\%username%, 1

loop, C:\temp\*, 2, 0
{
	IfInString, A_LoopFileFullPath, \%username%.			
		FileRemoveDir, %A_LoopFileFullPath%, 1
}

Re: need safe wild card for FileRemoveDir

Posted: 01 Aug 2018, 12:21
by jeeswg
- I would use this, to make sure that the file name starts with the string.

Code: Select all

if (InStr(A_LoopFileName, username) = 1)
- (For safety I might use MsgBox prior to each delete, or FileRecycle.)

Re: need safe wild card for FileRemoveDir

Posted: 01 Aug 2018, 12:54
by AlleyArtwork
Hi jeeswg,
Thank you for the alternative, this worked too!
It's always nice to see that you can do the same thing different ways.

I was trying to find this in the Help file but wasn't able to. Can you direct me to sources (or right thing to search for in Help) that show how this works and other ways to use this?

Re: need safe wild card for FileRemoveDir

Posted: 01 Aug 2018, 13:14
by jeeswg
These are the main pages I use. Cheers.
Alphabetical Command and Function Index | AutoHotkey
https://autohotkey.com/docs/commands/
Variables and Expressions
https://autohotkey.com/docs/Variables.htm