오늘은 디렉토리 사이즈 구하는 방법에 대해서 포스팅 해볼까 합니다
파일 크기 구하는 것은 그 전에 올렸던 적이 있는데
종종 폴더 용량을 구할 일이 생기기도 합니다.. (저는 그게 오늘이었읍니다...)
저번과 비슷하게 오늘도 아주 쉽습니다
우선 테스트 폴더를 생성하구요
그 안에 파일을 넣어줬습니다.
![](http://t1.daumcdn.net/tistory_admin/static/images/no-image-v1.png)
![](http://t1.daumcdn.net/tistory_admin/static/images/no-image-v1.png)
strDir = "C:\RPA\testFolder"
New DirectoryInfo(strDir).EnumerateFiles().Sum(Function(a) a.Length)
코드 설명을 해보자면..
New DirectoryInfo(strDir)
DirectoryInfo를 구한 뒤
.EnumerateFiles()
Directory안에 있는 파일들을 Enumerate로 가져옵니다.
Function(a) a.Length
저번에 파일 크기를 구할 때 사용했던 Length를 사용해서 모든 파일 크기를 구합니다.
Sum(Function(a) a.Length)
방금 구한 파일들의 크기를 모두 더합니다.
![](http://t1.daumcdn.net/tistory_admin/static/images/no-image-v1.png)
![](http://t1.daumcdn.net/tistory_admin/static/images/no-image-v1.png)
위 프로세스를 수행하면 byte 단위의 폴더의 크기를 알 수 있습니다.
용량 단위는 byte --> KB --> MB --> GB 순입니다.
편한 단위로 보려면 1024씩 나눠주면 됩니다.
![](http://t1.daumcdn.net/tistory_admin/static/images/no-image-v1.png)
![](http://t1.daumcdn.net/tistory_admin/static/images/no-image-v1.png)
+) 2022.11.16
위의 방법은 단일 폴더 안의 모든 파일의 크기를 구하고 있다.
폴더 안의 폴더 안의 폴더 안의 파일들의 크기는 누락된다........
안에 폴더가 몇 개 있든지 폴더의 깊이가 얼마나 되든지 사이즈를 구할 방법이 있다.
New DirectoryInfo("path").GetFiles("*", SearchOption.AllDirectories).Sum(Function(a) a.Length)
MB 단위로 반올림을 하고 싶다면 아래와 같이~
System.Math.Round(New DirectoryInfo(strDir).GetFiles("*", SearchOption.AllDirectories).Sum(Function(a) a.Length)/1024/1024, 1)
위의 방법보다... 훨씬 괜찮은 방법! 작년의 나보다 발전했다고 친다. 오케이
끝~~!
'RPA' 카테고리의 다른 글
[UiPath] Activate가 안될 때 (3) | 2022.11.16 |
---|---|
[UiPath] 디렉토리의 파일명만 가져오기 (3) | 2021.12.28 |
[Zapier] zapier 알아보기 (0) | 2021.11.16 |
[UiPath] String.Format 사용하기 (0) | 2021.07.23 |
[UiPath] iterator에서 원하는 값 찾기 (0) | 2021.05.23 |