Help with exclude folders from loop Topic is solved

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
User avatar
Tomer
Posts: 366
Joined: 21 Aug 2016, 05:11

Help with exclude folders from loop

12 Dec 2017, 04:42

hii,

im using Loop, C:\*.*, , 1 and C:\ += %A_LoopFileSize% to get the total size of C:\ Drive.
however i want to exclude some folders like c:\windows and c:\program files from the loop so these folders will not counting in the total size.

tell me if you need the full code to help me figure this out..

tnx!
Osprey
Posts: 453
Joined: 18 Nov 2017, 05:50

Re: Help with exclude folders from loop

12 Dec 2017, 05:11

Try adding the following to the top of your loop (inside your loop, but before file size computation):

Code: Select all

If RegExMatch(A_LoopFileLongPath, "C:\\Program Files") or RegExMatch(A_LoopFileLongPath, "C:\\Windows")
    Continue
Continue makes it skip the rest of the loop, but move the file pointer and start again from the top.
User avatar
Tomer
Posts: 366
Joined: 21 Aug 2016, 05:11

Re: Help with exclude folders from loop

12 Dec 2017, 05:34

Thanks Osprey but i failed to make it work,
i added the full code below, may you take a look.
tnx!

Code: Select all

C_Drive = C:\

; C_Drive size
Loop, %C_Drive%\*.*, , 1
   C_DriveSize += %A_LoopFileSize%
    ; in KB
KB_C_Drive := Round(((C_DriveSize /1024)), 0)
   ; in MB
MB_C_Drive := Round(((C_DriveSize /1024)/1024), 2)
   ; in GB
GB_C_Drive := Round(((C_DriveSize /1024)/1024/1024), 2)


if KB_C_Drive  = 0
{
MsgBox, empty
}
else
if KB_C_Drive < 1024
{
    MsgBox, %KB_C_Drive% KB
}
else if MB_C_Drive > 1024
{
    MsgBox, %GB_C_Drive% GB
}
else
{
    MsgBox, %MB_C_Drive% MB
}
Osprey
Posts: 453
Joined: 18 Nov 2017, 05:50

Re: Help with exclude folders from loop

12 Dec 2017, 06:45

Try this:

Code: Select all

Loop, %C_Drive%\*.*, , 1
  If !(RegExMatch(A_LoopFileLongPath, "i)C:\\Program Files") or RegExMatch(A_LoopFileLongPath, "i)C:\\Windows") or RegExMatch(A_LoopFileLongPath, "i)C:\\\$Recycle.Bin"))
    C_DriveSize += %A_LoopFileSize%
User avatar
Tomer
Posts: 366
Joined: 21 Aug 2016, 05:11

Re: Help with exclude folders from loop

12 Dec 2017, 07:16

i still get the same total size :(
User avatar
jeeswg
Posts: 6902
Joined: 19 Dec 2016, 01:58
Location: UK

Re: Help with exclude folders from loop

12 Dec 2017, 09:01

- How about this?

Code: Select all

q:: ;list number/size of files on C drive
vDir1 := "C:"
vList := ""
Loop, Files, % vDir1 "\*", D
{
	if !(A_LoopFileName = "Windows")
	&& !InStr(A_LoopFileName, "Program Files")
		vList .= A_LoopFileFullPath "|"
}
vList := SubStr(vList, 1, -1)
MsgBox, % StrReplace(vList, "|", "`n")

vCount := 0, vSize := 0
Loop, Parse, vList, |
	Loop, Files, % A_LoopField "\*", FR
		vCount += 1, vSize += A_LoopFileSize
MsgBox, % vCount " " vSize
return
- Some methods for this that I've seen, loop through every file on the drive, but just don't increment the variables for paths that contain certain strings. Which is clearly slower than it needs to be. (It's better to not loop through the folders that you want to exclude.)
homepage | tutorials | wish list | fun threads | donate
WARNING: copy your posts/messages before hitting Submit as you may lose them due to CAPTCHA
Osprey
Posts: 453
Joined: 18 Nov 2017, 05:50

Re: Help with exclude folders from loop

12 Dec 2017, 17:52

As jeeswg suggested, try using the newer syntax for looping files, assuming that you have a recent version of AutoHotkey.

Here's the simplest code that works for me:

Code: Select all

C_Drive = C:\
ExcludeList = C:\Program Files,C:\Windows,C:\$Recycle.Bin

Loop, Files, %C_Drive%\*.*, R
  If A_LoopFileLongPath not contains %ExcludeList%
    C_DriveSize += %A_LoopFileSize%
User avatar
Tomer
Posts: 366
Joined: 21 Aug 2016, 05:11

Re: Help with exclude folders from loop

17 Dec 2017, 09:27

sorry for the late reply,
tnx Osprey & jeeswg. now it works great!
User avatar
jeeswg
Posts: 6902
Joined: 19 Dec 2016, 01:58
Location: UK

Re: Help with exclude folders from loop

18 Dec 2017, 12:01

I have written some code re. displaying a friendly file size, using the Winapi, and via pure AHK:
Floor/Ceil able to handle decimal places (cf. Excel's Trunc function) - AutoHotkey Community
https://autohotkey.com/boards/viewtopic ... 77#p189277
WinAPI - GetXxxFormat Functions - AutoHotkey Community
https://autohotkey.com/boards/viewtopic ... 76#p189276
homepage | tutorials | wish list | fun threads | donate
WARNING: copy your posts/messages before hitting Submit as you may lose them due to CAPTCHA
User avatar
Tomer
Posts: 366
Joined: 21 Aug 2016, 05:11

Re: Help with exclude folders from loop

20 Dec 2017, 03:45

ok so thanks to your help i came up with the following code,

2 questions please:
1. i want to show and exclude files aswell, should i really need looping the drive 2 times for this ?
2. i failed to exclude files extensions, need help with that too (in my code sample im trying to exclude .txt and .exe extensions)

Code: Select all

vDir1 := "C:"
vList := ""
Loop, Files, % vDir1 "\*", D ; folders
{
	if !(A_LoopFileName = "Windows")
	&& !InStr(A_LoopFileName, "Users") && !InStr(A_LoopFileName, "Program Files") && !InStr(A_LoopFileName, "ProgramData") && !InStr(A_LoopFileName, "$Recycle.Bin")
		vList .= A_LoopFileFullPath "|"
}

Loop, Files, % vDir1 "\*", F ; files
{
	if !(A_LoopFileName = "*.txt")
	&& !InStr(A_LoopFileName, "*.exe")
		vList .= A_LoopFileFullPath "|"
}

vList := SubStr(vList, 1, -1)
MsgBox, % StrReplace(vList, "|", "`n")

TotalSize := 0
Loop, Parse, vList, |
	Loop, Files, % A_LoopField "\*", FR
		TotalSize += %A_LoopFileSize%


    ; in KB
KB_Drive := Round(((TotalSize /1024)), 0)
   ; in MB
MB_Drive := Round(((TotalSize /1024)/1024), 2)
   ; in GB
GB_Drive := Round(((TotalSize /1024)/1024/1024), 2)


if KB_Drive  = 0
{
MsgBox, empty
}
else
if KB_Drive < 1024
{
    MsgBox, %KB_Drive% KB
}
else if MB_Drive > 1024
{
    MsgBox, %GB_Drive% GB
}
else
{
    MsgBox, %MB_Drive% MB
}

ExitApp
User avatar
jeeswg
Posts: 6902
Joined: 19 Dec 2016, 01:58
Location: UK

Re: Help with exclude folders from loop

20 Dec 2017, 06:00

Code: Select all

;this won't work, a = b compares literal text,
;it cannot handle wildcards (i.e. ? or *):
	if !(A_LoopFileName = "*.txt")
	&& !InStr(A_LoopFileName, "*.exe")
;so do this instead:
	if !(A_LoopFileExt = "txt")
	&& !(A_LoopFileExt = "exe")

;with looping, if you loop through the list of
;folders, that won't include files on the root of C,
;so add one loop for those:
vDir1 := "C:"
TotalSize := 0
Loop, Files, % vDir1 "\*", F
	TotalSize += A_LoopFileSize
MsgBox, % TotalSize
homepage | tutorials | wish list | fun threads | donate
WARNING: copy your posts/messages before hitting Submit as you may lose them due to CAPTCHA
User avatar
Tomer
Posts: 366
Joined: 21 Aug 2016, 05:11

Re: Help with exclude folders from loop

20 Dec 2017, 10:27

Jeeswg ty! will check it

UPDATE:

I came up with this and its working fine,
one problem now that files stored in C:\ not include in the total size (only folders) :(
i see you added something about this but couldnt understand it..
also, im still using 2 separate loops for folders and files

folders: Loop, Files, % vDir1 "\*", D
Files: Loop, Files, % vDir1 "\*", F

dunno if it could be 1 loop for both folders and files..
if you can post your full code suggestion i'll be thankful
tnx!


Code: Select all

vDir1 := "C:"
vList := ""
Loop, Files, % vDir1 "\*", D ; folders
{
	if !(A_LoopFileName = "Windows")
	&& !InStr(A_LoopFileName, "Users") && !InStr(A_LoopFileName, "Program Files") && !InStr(A_LoopFileName, "ProgramData") && !InStr(A_LoopFileName, "$Recycle.Bin")
		vList .= A_LoopFileFullPath "|"
}

Loop, Files, % vDir1 "\*", F ; files
{
	if !(A_LoopFileExt = "txt")
	&& !(A_LoopFileExt = "exe")
	vList .= A_LoopFileFullPath "|"
}

vList := SubStr(vList, 1, -1)
MsgBox, % StrReplace(vList, "|", "`n")

TotalSize := 0
Loop, Parse, vList, |
	Loop, Files, % A_LoopField "\*", FR
		TotalSize += %A_LoopFileSize%


    ; in KB
KB_Drive := Round(((TotalSize /1024)), 0)
   ; in MB
MB_Drive := Round(((TotalSize /1024)/1024), 2)
   ; in GB
GB_Drive := Round(((TotalSize /1024)/1024/1024), 2)


if KB_Drive  = 0
{
MsgBox, empty
}
else
if KB_Drive < 1024
{
    MsgBox, %KB_Drive% KB
}
else if MB_Drive > 1024
{
    MsgBox, %GB_Drive% GB
}
else
{
    MsgBox, %MB_Drive% MB
}

ExitApp
Last edited by Tomer on 24 Dec 2017, 04:36, edited 6 times in total.
SirRFI
Posts: 404
Joined: 25 Nov 2015, 16:52

Re: Help with exclude folders from loop

20 Dec 2017, 17:51

I suggest making array or two arrays (for paths and files) to be excluded, and then use continue in loop to skip element upon matching thing from array, rather than making big ifs. Depends on your need really.
Use

Code: Select all

[/c] forum tag to share your code.
Click on [b]✔[/b] ([b][i]Accept this answer[/i][/b]) on top-right part of the post if it has answered your question / solved your problem.
User avatar
jeeswg
Posts: 6902
Joined: 19 Dec 2016, 01:58
Location: UK

Re: Help with exclude folders from loop  Topic is solved

24 Dec 2017, 07:40

@Tomer: Think about this:
C:\Dir1 C:\Dir2 C:\Dir3 C:\File1 C:\File2 C:\File3
1) List folders on the root of C, but exclude Dir2. 1 loop.
C:\Dir1 C:\Dir3
2) Get the size of the files on the root of C. 1 loop.
3) For each folder in the list folders, get the size of all files. A file loop within a parsing loop.
Think through the script below until it fully makes sense to you.

Code: Select all

vDir1 := "C:"
vList := ""
Loop, Files, % vDir1 "\*", D ; folders
{
	if !(A_LoopFileName = "Windows")
	&& !InStr(A_LoopFileName, "Users") && !InStr(A_LoopFileName, "Program Files") && !InStr(A_LoopFileName, "ProgramData") && !InStr(A_LoopFileName, "$Recycle.Bin")
		vList .= A_LoopFileFullPath "|"
}

vList := SubStr(vList, 1, -1)
MsgBox, % StrReplace(vList, "|", "`n")

TotalSize := 0
Loop, Files, % vDir1 "\*", F ; files
	if !(A_LoopFileExt = "txt")
	&& !(A_LoopFileExt = "exe")
		TotalSize += A_LoopFileSize
Loop, Parse, vList, |
	Loop, Files, % A_LoopField "\*", FR
		if !(A_LoopFileExt = "txt")
		&& !(A_LoopFileExt = "exe")
			TotalSize += A_LoopFileSize

    ; in KB
KB_Drive := Round(((TotalSize /1024)), 0)
   ; in MB
MB_Drive := Round(((TotalSize /1024)/1024), 2)
   ; in GB
GB_Drive := Round(((TotalSize /1024)/1024/1024), 2)

if KB_Drive  = 0
{
MsgBox, empty
}
else
if KB_Drive < 1024
{
    MsgBox, %KB_Drive% KB
}
else if MB_Drive > 1024
{
    MsgBox, %GB_Drive% GB
}
else
{
    MsgBox, %MB_Drive% MB
}

ExitApp
homepage | tutorials | wish list | fun threads | donate
WARNING: copy your posts/messages before hitting Submit as you may lose them due to CAPTCHA
User avatar
Tomer
Posts: 366
Joined: 21 Aug 2016, 05:11

Re: Help with exclude folders from loop

24 Dec 2017, 08:41

Thanks jeeswg works like a charm! :D

only 1 thing: now the MsgBox, % StrReplace(vList, "|", "`n") shows only the folders (not files aswell :( ) before it gives the total size,
i fixed it with the code added, but if you have fatser method it'll be great!

Code: Select all

vDir1 := "C:"
vList := ""
Loop, Files, % vDir1 "\*", D ; folders
{
	if !(A_LoopFileName = "Windows")
	&& !InStr(A_LoopFileName, "Users") && !InStr(A_LoopFileName, "Program Files") && !InStr(A_LoopFileName, "ProgramData") && !InStr(A_LoopFileName, "$Recycle.Bin")
		vList .= A_LoopFileFullPath "|"
		
}

Loop, Files, % vDir1 "\*", F ; files
{
	if !(A_LoopFileExt = "txt")
	&& !(A_LoopFileExt = "exe")
	vList .= A_LoopFileFullPath "|"
}

vList := SubStr(vList, 1, -1)
MsgBox, % StrReplace(vList, "|", "`n")



TotalSize := 0
Loop, Files, % vDir1 "\*", F ; files
	if !(A_LoopFileExt = "txt")
	&& !(A_LoopFileExt = "exe")
		TotalSize += A_LoopFileSize
Loop, Parse, vList, |
	Loop, Files, % A_LoopField "\*", FR
		if !(A_LoopFileExt = "txt")
		&& !(A_LoopFileExt = "exe")
			TotalSize += A_LoopFileSize


    ; in KB
KB_Drive := Round(((TotalSize /1024)), 0)
   ; in MB
MB_Drive := Round(((TotalSize /1024)/1024), 2)
   ; in GB
GB_Drive := Round(((TotalSize /1024)/1024/1024), 2)

if KB_Drive  = 0
{
MsgBox, empty
}
else
if KB_Drive < 1024
{
    MsgBox, %KB_Drive% KB
}
else if MB_Drive > 1024
{
    MsgBox, %GB_Drive% GB
}
else
{
    MsgBox, %MB_Drive% MB
}

ExitApp


User avatar
jeeswg
Posts: 6902
Joined: 19 Dec 2016, 01:58
Location: UK

Re: Help with exclude folders from loop

24 Dec 2017, 08:52

- vList is supposed to be a list of folders, without any files in it. I would create a separate variable for displaying the list of folders/files on the root of C. I don't know why exactly you would want to display a list of some of the files/folders on the root of C.
- I would just get rid of the MsgBox, I only put the MsgBox there for diagnostic reasons, so that you would know which folders would be looped through, and to show that certain folders would be omitted from the search.
- In terms of 'faster', listing the files on the root of C, should hardly take any time.

Code: Select all

vList2 := vList
Loop, Files, % vDir1 "\*", F ; files
{
	if !(A_LoopFileExt = "txt")
	&& !(A_LoopFileExt = "exe")
		vList2 .= A_LoopFileFullPath "|"
}
vList2 := SubStr(vList2, 1, -1)
MsgBox, % StrReplace(vList2, "|", "`n")
homepage | tutorials | wish list | fun threads | donate
WARNING: copy your posts/messages before hitting Submit as you may lose them due to CAPTCHA
User avatar
Tomer
Posts: 366
Joined: 21 Aug 2016, 05:11

Re: Help with exclude folders from loop

24 Dec 2017, 09:19

i want to display a list of the folders and files which i not exluded,
so i know what the total size i get for.

its ok as it is now thank you very much

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: ArkuS, mikeyww and 311 guests