bstjohn

Any way to make DirGetSize exclude hidden/system files?

4 posts in this topic

#1 ·  Posted (edited)

I'm working on a script to copy a subset of files and folders from a PC to a server.  I'm using the extended info from DirGetSize to show how many files and folders and total size that will be copied (copy being done by RoboCopy).  Then I run DirGetSize against the destination when the copy is done to compare against the DirGetSize from the copy source so I can know if anything got skipped.  I'm finding that a lot of stuff is getting skipped.  This lead me to realize that it's because DirGetSize is including files and folders that are hidden and/or system, and I'm excluding these in RoboCopy.  Is there anyway to excluded these from DirGetSize?   I've been unable to find a UDF as yet, and I'd like to do something a little more elegant than piping the output from DIR into a file and reading that back.

 

 

Edited by bstjohn

Share this post


Link to post
Share on other sites



#include <File.au3>

Local $aFiles = _FileListToArrayRec(@ScriptDir & "\", "*", $FLTAR_FILESFOLDERS, $FLTAR_RECUR)
Local $iFullSize = DirGetSize(@ScriptDir)

ConsoleWrite("Beginning size: " & ($iFullSize) / 1024 & @LF)    ; / 1024 to convert to kb, easier to read

For $i = 1 To $aFiles[0]
    Local $iAttributes = FileGetAttrib(@ScriptDir & "\" & $aFiles[$i])
    Local $iFileSize = FileGetSize(@ScriptDir & "\" & $aFiles[$i])
    Local $iDirSize = DirGetSize(@ScriptDir & "\" & $aFiles[$i])

    If (StringInStr($iAttributes, "S")) Then
        If (StringInStr($iAttributes, "D")) Then
            ConsoleWrite("Folder '" & $aFiles[$i] & "' is a system file, subtracting from full size" & @LF)
            $iFullSize -= $iDirSize
        Else
            ConsoleWrite("File '" & $aFiles[$i] & "' is a system file, subtracting from full size" & @LF)
            $iFullSize -= $iFileSize
        EndIf
    EndIf
Next

ConsoleWrite("Adjusted size: " & ($iFullSize) / 1024 & @LF) ; / 1024 to convert to kb, easier to read

Something like this should get you going. You'll have to figure out a way to account for if the current file being checked is a directory and it's subtracted from the full size, to exclude all files under that directory. I didn't account for hidden because I believe hidden files are still copied. I could be wrong, if I am just add another StringInStr for "H".

Share this post


Link to post
Share on other sites
3 hours ago, InunoTaishou said:

I didn't account for hidden because I believe hidden files are still copied. I could be wrong, if I am just add another StringInStr for "H".

RoboCopy's /XA switch let's you pick which attributes to exclude, so I'm excluding system and hidden.  So I'll add another StringInStr for "H" and that'll do it.

This puts me on a good track.  Thanks for the mental kickstart!

Share this post


Link to post
Share on other sites

InunoTaishou-

I've found that _FileListToArray() has the inherent ability to filter the system and hidden files:

Add one or more of the following to $iReturn to omit files/folders with that attribute
+ $FLTAR_NOHIDDEN (4) - Hidden files and folders
+ $FLTAR_NOSYSTEM (8) - System files and folders
+ $FLTAR_NOLINK (16) - Link/junction folders

That makes things all the easier.  Thanks for pointing me in the right direction.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now

  • Similar Content

    • BlazerV60
      By BlazerV60
      Hello all,
      I've been trying to figure out how to launch Google Chrome in the background (hidden) but it doesn't seem possible. I've tried the following methods:
      ShellExecute("C:\Program Files (x86)\Google\Chrome\Application\chrome.exe", "", "", "", @SW_HIDE) Also I've tried:
      ShellExecute(@ComSpec, "/c start chrome.exe","","",@SW_HIDE) Lastly I tried:
      RunWait('"'&@ProgramFilesDir&'\Google\Chrome\Application\chrome.exe" --silent-launch',@ScriptDir)  
      But all of them launch my chrome browser without hiding it. Does anyone know a workaround for this or if AutoIT just can't Chrome? 
      Bonus points if you know how to make it launch chrome hidden and make it go to https://www.autoitscript.com
       
      Thank you,
      Brian
    • nacerbaaziz
      By nacerbaaziz
      Hi dear
      I have a question
      about the display language of the system
      How can I get the current display language and how can I change it by autoit
      Of course, if this is possible
      Greetings to all and hope you help me
    • nss
      By nss
      Hi all,
       
      I'm making an app that among other things has the system info feature, where it displays stuff like the system version, build, CPU and ram stats, etc.
      So, I haven't been able to find anywhere a way to access the following things (is there even?):
      Windows version (not like WIN_10, but the version like 1607, or 1511 Windows edition like professional, home, education, etc. information whether the Windows is registered or not (optionally the registrant's name) the type of the currently logged-in user (e.g., administrator) computer manufacturer and model (e.g., dell inc. Inspiron n5110 boot mode (e.g., safe, normal) CPU tempreture I'd appreciate a lot if any of you could help me with any of the things from above; it would mean a lot to me.
       
      Have a great day.
    • ces1a
      By ces1a
      I was looking for a way to find out how long my PC had been running a few weeks ago and had trouble finding something that satisfied my needs,  I found a Visual Basic Script that worked but found it too long. But searching trough AutoIt WINAPI help found that it really was as simple as the following little script.
      #include <WinAPISys.au3>
      #include <WinAPIMisc.au3>
      Local $Uptime = _WinAPI_StrFromTimeInterval(_WinAPI_GetTickCount())
      MsgBox(0, '', "PC Uptime ==> " & $UpTime)
      Hope it helps...
    • jguinch
      By jguinch
      Here is my _DirGetSizeEx function. It allows you to get the size of a directory, with additional filters for including, excluding files and folders.
      ; #FUNCTION#==================================================================================================================== ; Name ..........: _DirGetSizeEx ; Description ...: Returns the size in bytes of a file list by extension. ; Syntax ........: _DirGetSizeEx($sDir[, $sMask = "*"[, $iFlag = 0]]) ; Parameters ....: $sDir - The directory to search in. ; $sMask - [optional] Filter for results. Default is "*" (all). ; Filter for result. Multiple filters must be separated by ";" ; Use "|" to separate 3 possible sets of filters: "Include|Exclude|Exclude_Folders" ; Include = Files/Folders to include (default = "*" [all]) ; Exclude = Files/Folders to exclude (default = "" [none]) ; Exclude_Folders = exclude defined folders (default = "" [none]). Only used if $iFlag = 0 or 1 ; $iFlag - [optional] 0 (default) = Returns the size ; 1 = Extended mode is On -> returns an array that contains the file of each file (see Remarks). ; 2 = Don't get the size of files in subdirectories (recursive mode is Off) ; Return values .: Success = The size in bytes, or a single dimension array ; Failure = -1 and sets the @error flag to 1 if the path doesn't exist. ; Author ........: jguinch ; Remarks .......: If you use the extended mode then the array returned from this function is a single dimension array containing the followingelements: ; $aArray[0][0] = Files count ; $aArray[0][1] = Total files size ; $aArray[1][0] = Full name of 1st file ; $aArray[1][1] = Size of 1st file ; $aArray[2][0] = Full name of 2nd file ; $aArray[2][1] = Size of 2nd file ; $aArray[n][1] = Size of nth file ; $aArray[n][1] = Size of nth file ;=============================================================================================================================== Func _DirGetSizeEx($sDir, $sMask = "*", $iFlag = 0); OK If NOT FileExists($sDir) Then Return SetError(1, 0, -1) If NOT StringInStr(FileGetAttrib($sDir), "D") Then Return SetError(1, 0, -1) Local $iExtMode = BitAND($iFlag, 1) > 0 Local $iRecMode = NOT BitAND($iFlag, 2) > 0 Local $aDirs[1] = [ StringRegExpReplace($sDir, "\\$", "") ] Local $iCountDir = 0, $iCountFile = 0, $n = 0, $iSize = 0, $iFullSize = 0 Local $aFiles[1][2] = [[0]] Local $hSearch, $sFileName, $sRegexFilesInclude, $sRegexFilesExclude = "^$", $sRegexFoldersExclude = "^$" Local $sRegexMask = StringReplace( StringReplace( StringReplace($sMask, "|", "\|") , "?", "\E(?:.|.?$)\Q"), "*", "\E.*?\Q") Local $aFilters = StringSplit($sRegexMask, "\|", 3) $sRegexFilesInclude = "(?i)^(?:" & StringRegExpReplace(StringReplace($aFilters[0], ";", "|") , "([^|]+)", "\\Q$1\\E") & ")$" If UBound($aFilters) > 1 Then $sRegexFilesExclude = "(?i)^(?:" & StringRegExpReplace(StringReplace($aFilters[1], ";", "|") , "([^|]+)", "\\Q$1\\E") & ")$" If UBound($aFilters) > 2 Then $sRegexFoldersExclude = "(?i)^(?:" & StringRegExpReplace(StringReplace($aFilters[2], ";", "|") , "([^|]+)", "\\Q$1\\E") & ")$" While 1 $hSearch = FileFindFirstFile( $aDirs[$n] & "\*.*" ) If $hSearch <> -1 Then While 1 $sFileName = FileFindNextFile($hSearch) If @error Then ExitLoop If @Extended Then If NOT StringRegExp($sFileName, $sRegexFoldersExclude) Then $iCountDir += 1 If $iCountDir >= UBound($aDirs) Then Redim $aDirs[UBound($aDirs) * 2] $aDirs[$iCountDir] = $aDirs[$n] & "\" & $sFileName EndIf Else If StringRegExp($sFileName, $sRegexFilesInclude) AND NOT StringRegExp($sFileName, $sRegexFilesExclude) Then $iSize = FileGetSize($aDirs[$n] & "\" &$sFileName) $iFullSize += $iSize If $iExtMode Then $iCountFile += 1 If $iCountFile >= UBound($aFiles) Then Redim $aFiles[UBound($aFiles) * 2][2] $aFiles[$iCountFile][0] = $aDirs[$n] & "\" &$sFileName $aFiles[$iCountFile][1] = $iSize EndIf EndIf EndIf WEnd If NOT $iRecMode Then ExitLoop EndIf FileClose($hSearch) If $n = $iCountDir Then ExitLoop $n += 1 WEnd If NOT $iExtMode Then Return $iFullSize Redim $aFiles[$iCountFile + 1][2] $aFiles[0][0] = $iCountFile $aFiles[0][1] = $iFullSize Return $aFiles EndFunc  
       
      Old functions :
       _DirGetSizeEx-old.au3
       _DirGetSizeByExtension.au3