Computer Hope

Software => Computer programming => Topic started by: sun_os on July 02, 2010, 04:16:33 AM

Title: Batch file to check the folder size
Post by: sun_os on July 02, 2010, 04:16:33 AM
Hello All,

I don't have programming background , but I need to write the vbs / batch file to check the folder size and display the size

May I know what is the syntax to do ? >:(

Title: Re: Batch file to check the folder size
Post by: Sidewinder on July 02, 2010, 08:00:35 AM
This is a VBS solution. You will be prompted for the directory name.

Code: [Select]
Set fso = CreateObject("Scripting.FileSystemObject")

Do
    WScript.StdOut.Write "Please enter directory name: "
    strFolder = WScript.StdIn.ReadLine
    If fso.FolderExists(strFolder) Then
Exit Do
    Else
  WScript.StdOut.Write "Invalid Directory ... Try Again" & vbCrLf
    End If
Loop

Set f = fso.GetFolder(strFolder)
WScript.Echo f.Path & ": " & FormatNumber(f.Size,0,,TriStateTrue) & " bytes"

Save the script with a VBS extension and run from the command line as:
cscript scriptname.vbs Do not run with WScript as stdin and stdout are not supported.

The can also be done with a batch file by scraping the folder size off a directory listing but the VBS solution is so much more elegant. ;D
Title: Re: Batch file to check the folder size
Post by: ghostdog74 on July 07, 2010, 08:44:42 PM
download coreutils (http://gnuwin32.sourceforge.net/packages/coreutils.htm) if you can. then just simply use the du command
Code: [Select]
C:\test>du -sk c:\tmp
28      c:\tmp
Title: Re: Batch file to check the folder size
Post by: marvinengland on July 08, 2010, 11:16:07 AM
Hello All,

I don't have programming background , but I need to write the vbs / batch file to check the folder size and display the size

May I know what is the syntax to do ? >:(



Install the Unix OS  and the du -sk command will be available.
Title: Re: Batch file to check the folder size
Post by: Salmon Trout on July 08, 2010, 11:21:48 AM
Install the Unix OS

But then he can't run a batch file. Marvin just gets dumber and dumber.
Title: Re: Batch file to check the folder size
Post by: marvinengland on July 08, 2010, 11:19:51 AM

I don't have a programming background , but I need to write the vbs / batch file to check the folder size and display the size

May I know what is the syntax to do ? >:(



http://en.wikipedia.org/wiki/Dual_boot

We can have more than one OS installed on our machine.

"Multi boot
From Wikipedia, the free encyclopedia  (Redirected from Dual boot)
Jump to: navigation, search
"Multiboot" redirects here. For the specification, see Multiboot Specification.
 This article does not cite any references or sources.
 
GRUB, with entries for Ubuntu and Windows Vista, an example of dual bootingMulti-boot or Multi-booting is the act of installing multiple operating systems on a computer, and being able to choose which one to boot when starting the computer. The term dual-booting refers to the common configuration of exactly two operating systems. Multi-booting requires a program called a boot loader."


http://en.wikipedia.org/wiki/Dual_boot
Title: Re: Batch file to check the folder size
Post by: BC_Programmer on July 08, 2010, 11:37:15 AM
Marvin apparently doesn't realize that installing a separate OS doesn't give the previous OS the magical ability to use commands and features from the second OS.

perhaps Marvin should actually learn about the concepts he suggests rather then copy-pasting directly from wikipedia.

Perhaps Marvin can suggest a place to get this "UNIX" OS, which to my understanding was not necessarily available on the desktop market as he implies? Perhaps Marvin should read about the definition of the term "LINUX variant" to which he is clearly referring?

Perhaps Marvin should not give a solution when the same solution has already been offered? Ghostdog already provided the information regarding the "du" command, and also provided a link that would allow this "du" program to run on a windows System. Did Marvin not read previous posts? Perhaps reading previous posts has become difficult for Marvin?


Anyway, nonsense aside, I prefer the VBS solution, because it requires nothing to be downloaded. The du solution is much shorter. clearly it depends on the particular needs. The requirements state that the size is to be displayed, but make no mention that the filename itself, which is shown from the du output, should be. (I imagine there is a switch or possibly some other modification that could be made to supress it).
Title: Re: Batch file to check the folder size
Post by: ghostdog74 on July 08, 2010, 01:50:07 PM
because it requires nothing to be downloaded.
GNU tools only need to be downloaded once. They are just .exe executables

Quote
The du solution is much shorter. clearly it depends on the particular needs. The requirements state that the size is to be displayed, but make no mention that the filename itself, which is shown from the du output, should be. (I imagine there is a switch or possibly some other modification that could be made to supress it).
du already comes with various features and switch options eg to display human readable file size, or you can decide to follow shortcuts or not, among others. du has been used in the Unix world for decades. If OP does not need the file names, simply pipe to sed/awk to remove. It is definitely not a big issue.
Code: [Select]
du ....  | gawk "{total+=$1}END{print \"total size: \"total }"

I am not saying these can't be done with vbscript, just that i like the conciseness.
Title: Re: Batch file to check the folder size
Post by: ghostdog74 on July 08, 2010, 01:53:46 PM
Install the Unix OS  and the du -sk command will be available.
you do not need to install an entire OS to use du. du is just  a program, and Unix tools have already been ported to run on Windows.
Title: Re: Batch file to check the folder size
Post by: sun_os on July 08, 2010, 10:21:43 PM
Thank for all reply the question and suggestion

Acutally, I run the bat file to check the file size as I need to get the size to determine the Microsoft window KBXXX is not used up the workstation disk size.

I thnk the bat file script is the best to do that, if I don't want to install the package, just write the script. How can I do that ? ;D

Title: Re: Batch file to check the folder size
Post by: Sidewinder on July 09, 2010, 12:03:38 AM
The VBS solution posted earlier did not require you to install or download anything. All you needed to do was copy/paste the code into your editor, save the file and run according to the instructions.

This is a batch solution. Same instructions: copy/paste into your editor (notepad is fine), save the file with a bat extension and run from the command prompt using the name you saved it with. You will be prompted for the directory name at runtime.

Code: [Select]
@echo off
setlocal
set /p dirName=Enter Directory Name:

for %%v in ("%dirName%") do (
  if not exist %%~sv\nul echo "%dirName%" is NOT a directory & goto :eof
)

for /f "tokens=3-4" %%v in ('dir "%dirName%" /s ^| find /i "file(s)"') do (
  set bytes=%%v


echo Folder: %dirName% contains %bytes% bytes

 8)
Title: Re: Batch file to check the folder size
Post by: sun_os on July 11, 2010, 02:22:52 AM
That fine ! Thank for you recommediation

I will try to use the script, thank for you helpful  ::)
Title: Re: Batch file to check the folder size
Post by: sun_os on July 11, 2010, 09:57:29 PM
Hi Sidewinder

I copy the code to notpad , rename vbs extention, run it. The window pop up error:
invalid character  source vbscript compilation error

By the way, I want to check the folder size and output to xls / cvs

What is the code I need to fill in to exist code

Thanks
Title: Re: Batch file to check the folder size
Post by: Salmon Trout on July 12, 2010, 12:10:40 AM
Hi Sidewinder

I copy the code to notpad , rename vbs extention, run it. The window pop up error:
invalid character  source vbscript compilation error

Quote from: sidewinder
This is a batch solution
Title: Re: Batch file to check the folder size
Post by: sun_os on July 14, 2010, 01:33:09 AM
What can I solve the problem
Title: Re: Batch file to check the folder size
Post by: Sidewinder on July 14, 2010, 05:46:28 AM
This thread is way past it's sell-by-date.

What can I solve the problem

If you are using the VBS solution, you need to follow the instructions in Post #1. If you are using the BAT solution you need to follow the instructions in Post #10. While the instructions are similar, they are not interchangeable.

If you are still having problems, please post the code you are using. Also show us how you are running the code. Both scripts are specifically writing to the console, so I'm not understanding how you got a pop-up error unless you ran the VBScript without specifying CScript and it defaulted to WScript.

 8)
Title: Re: Batch file to check the folder size
Post by: sun_os on July 14, 2010, 09:56:45 PM
Set fso = CreateObject("Scripting.FileSystemObject")

Do
    WScript.StdOut.Write "\\HDC-NT-MAIL01\D$" :
    strFolder = WScript.StdIn.ReadLine
    If fso.FolderExists(strFolder) Then
   Exit Do
    Else
     WScript.StdOut.Write "Invalid Directory ... Try Again" & vbCrLf
    End If
Loop

"Error window box messages"

Line :4
Char:5
Error: The handle is invalid.
Code: 80070006
Source :(null)

   
Set f = fso.GetFolder(strFolder)
WScript.Echo f.Path & ": " & FormatNumber(f.Size,0,,TriStateTrue) & " bytes"

(http://)


Title: Re: Batch file to check the folder size
Post by: Sidewinder on July 15, 2010, 06:27:07 AM
This started out as such a simple request. The script prompted the user for a directory name and reported back the directory size.

What is this: "\\HDC-NT-MAIL01\D$" : It looks like a UNC path, but in this context is used as a prompt.

Let's get back to basics: Try copying and pasting the original code and let's get that running. We can make any changes later. The code is generic so it will run as is on any machine.

Code: [Select]
Set fso = CreateObject("Scripting.FileSystemObject")

Do
    WScript.StdOut.Write "Please enter directory name: "
    strFolder = WScript.StdIn.ReadLine
    If fso.FolderExists(strFolder) Then
Exit Do
    Else
  WScript.StdOut.Write "Invalid Directory ... Try Again" & vbCrLf
    End If
Loop

Set f = fso.GetFolder(strFolder)
WScript.Echo f.Path & ": " & FormatNumber(f.Size,0,,TriStateTrue) & " bytes"

Save the script with a VBS extension and run from the command prompt as:
cscript scriptname.vbs Do not run with WScript as stdin and stdout are not supported.

You also need to follow instructions. CScript does not produce popup errors; errors go to the console.

 8)

Title: Re: Batch file to check the folder size
Post by: sun_os on July 15, 2010, 10:49:55 PM
Hi Sidewinder

I rename to vbs extention, copy the original code and run on the command prompt . the window message box show

error: The handle is invalid
Code 80070006
Source (null)

May I know what is the problem issue

Title: Re: Batch file to check the folder size
Post by: Salmon Trout on July 16, 2010, 12:13:54 AM
Code 80070006

You are running the script with wscript.exe, which cannot use stdin and stdout. You need to run it with cscript.exe.


Title: Re: Batch file to check the folder size
Post by: sun_os on July 16, 2010, 12:46:51 AM
But the script is vbs

I rename to cscript.vbs. run in command prompt. the same symptoms
Title: Re: Batch file to check the folder size
Post by: BC_Programmer on July 16, 2010, 02:23:35 AM
But the script is vbs

I rename to cscript.vbs. run in command prompt. the same symptoms

No. you aren't understanding.

you are running the VBS script using the executable, wscript.exe.

you need to use cscript.exe.
Title: Re: Batch file to check the folder size
Post by: sun_os on July 16, 2010, 09:48:16 AM
You mean that I rename the exe extention and run, I try to rename, the same symptom message box pop up
Title: Re: Batch file to check the folder size
Post by: Salmon Trout on July 16, 2010, 11:00:45 AM
Quote
But the script is vbs

For running vbs scripts, there are two (2) "script engines". One is called Wscript.exe and one is called Cscript.exe. If you just run a vbs script by clicking on it in Windows Explorer, you'll most likely get the default Wscript.exe script engine. You do not want this. This particular script will not work with Wscript. You need to run it with the Cscript script engine. Open a command prompt in the folder where the script is. Let's pretend the script is called Myscript.vbs. Either change the name to that or substitute the real name. Type the following and hit Enter:

Code: [Select]
cscript //nologo Myscript.vbs
Title: Re: Batch file to check the folder size
Post by: sun_os on July 17, 2010, 11:04:50 PM
Dear All,

I can run the command by cscript //d xxx.vbs , it show the size in bytes.

If the size is output to file which is txt or csv format and bytes to Megabytes.   How can I do that

Could you give me th link to learn the cscript

Thanks  ;D
Title: Re: Batch file to check the folder size
Post by: Sidewinder on July 19, 2010, 05:51:38 AM
If the size is output to file which is txt or csv format and bytes to Megabytes.   How can I do that

Part of the problem is that your specs are not clear. Which do you want, a txt file or a csv file? In addition to the onscreen display, the following snippet will produce a csv file. Each time you run the script, an additional entry will be appended to the csv file.

Code: [Select]
Const ForAppending = 8

Set fso = CreateObject("Scripting.FileSystemObject")
Set fs = fso.OpenTextFile("c:\temp\DirSize.csv", ForAppending, True)

Do
    WScript.StdOut.Write "Please enter directory name: "
    strFolder = WScript.StdIn.ReadLine
    If fso.FolderExists(strFolder) Then
Exit Do
    Else
  WScript.StdOut.Write "Invalid Directory ... Try Again" & vbCrLf
    End If
Loop

Set f = fso.GetFolder(strFolder)
WScript.Echo f.Path & ": " & FormatNumber((f.Size / 1048576),4,,TriStateTrue) & " MB"
fs.WriteLine f.Path & "," & Replace(FormatNumber((f.Size / 1048576),4), ",", "")
fs.Close

Could you give me th link to learn the cscript

CScript is an engine that interprets VBScripts (among others) and runs them within the cmd console

WScript is an engine that interprets VBScripts (among others) and runs them within Windows.

VBScript is a language you use to write scripts which in turn can be run by CScript or WScipt.  Start here (http://msdn.microsoft.com/library/d1wf56tt.aspx) for a VBScript reference. Of course you can always ask questions on the forum, just be more specific as to what you need.

Good luck.  8)
Title: Re: Batch file to check the folder size
Post by: sun_os on July 19, 2010, 08:59:40 AM
Hi Sidewinder

Thank you for your help

I run the program and check the csv format
but the file size has difference

e.g.

c:\document and setting\administrator>cscript //d check.vbs

c:\document and setting\administrator: 10,182,572,281 MB

The output is C:\Documents and Settings\Administrator 9710.8577

Anything I can edit the code

Thank you much


Title: Re: Batch file to check the folder size
Post by: nuckinfutz on July 19, 2010, 09:14:47 AM
Follow up question for all.

I too do not have much experience with batch files, but i am learning. the dilemma i am having is similar to the original poster?

here is what i need help with:

i need to write a batch file (preferably .bat not .vbs) that will give me the folder size of multiple directories including all sub-files and folders in MB or GB and i would like it to total the sizes. i do not need it to create an out.txt file with the results. on screen display would be fine.

any and all help is appreciated.

thank you,

NF
Title: Re: Batch file to check the folder size
Post by: ghostdog74 on July 19, 2010, 09:25:48 AM
see my first post. Use du with -H for human readable file size.
Title: Re: Batch file to check the folder size
Post by: nuckinfutz on July 19, 2010, 09:39:01 AM
ghostdog74,

thank you for the reply, however, i am in a situation where i cannot download anything to be put on the computers where i will need to run this batch file.

any other thoughts?

thanks,

NF
Title: Re: Batch file to check the folder size
Post by: Sidewinder on July 19, 2010, 11:43:22 AM
@sun_os:

Where did this come from?

Quote
c:\document and setting\administrator: 10,182,572,281 MB

The output is C:\Documents and Settings\Administrator 9710.8577

Are you sure the 10,182,572,281 represents MB (which is an awful lot) or just bytes (more reasonable)

It turns out that 10,182,572,281 bytes is 9710.8577 MB. Imagine that!

The file size for both the onscreen display and the csv line are computed the same way. I cannot reproduce the error. Need more info.

@nuckinfutz :

Please start your own thread. This marathon thread has gone on for 29 posts with no end in sight. Your post is only complicating things.

 8)

Title: Re: Batch file to check the folder size
Post by: sun_os on July 20, 2010, 07:37:35 AM
Hi Sidewinder,

How can I start the New Thread here?

Could you tell me, is that mean send you the personal messages ?

I copy your code and run cscript //d check.vbs , check with csv format which is not match the console information



Title: Re: Batch file to check the folder size
Post by: Sidewinder on July 20, 2010, 08:26:06 AM
How can I start the New Thread here?

That was for nuckinfutz who tried to hijack your thread.

Could you tell me, is that mean send you the personal messages ?

No. I very rarely even check if I have messages much less answer them. I prefer to post solutions that everyone can read.

I copy your code and run cscript //d check.vbs , check with csv format which is not match the console information

I cannot reproduce your results:

Quote
C:\Temp>cscript untitled.vbs
Microsoft (R) Windows Script Host Version 5.7
Copyright (C) Microsoft Corporation. All rights reserved.

Please enter directory name: d:\wfc\testlib
D:\WFC\Testlib: 1.6922 MB

C:\Temp>type dirsize.csv
D:\WFC\Testlib,1.6922

Unless I'm missing something, the console and the csv file matches. The only difference is the format of the output, but the data is the same. You wanted a csv file (comma separated values).

CSV files are useful when you need to input the data into a spreadsheet or database program. They are also easy to parse.

 8)
Title: Re: Batch file to check the folder size
Post by: sun_os on July 20, 2010, 09:25:23 AM
Hi
I capture the screen shot for your reference

C:\temp>cscript check.vbs
Microsoft (R) Windows Script Host Version 5.6
Copyright (C) Microsoft Corporation 1996-2001. All rights reserved.


Please enter directory name: c:\downloads
C:\Downloads: 5,203,235,307 MB

C:\temp>type disk*
系統找不到指定的檔案。

C:\temp>type dir*

DirSize.csv

C:\Documents and Settings\Administrator,9710.8577
C:\Downloads,4962.1919
C:\Downloads,4962.1919

C:\temp>

That is output

Thanks
Title: Re: Batch file to check the folder size
Post by: Sidewinder on July 20, 2010, 10:51:53 AM
Please check your code. I'm not understanding any of this. It appears the line that produces the console output has a problem.

The line produces the console output:
Quote
WScript.Echo f.Path & ": " & FormatNumber((f.Size / 1048576),4,,TriStateTrue) & " MB"

This line produces the CSV file output:
Quote
fs.WriteLine f.Path & "," & Replace(FormatNumber((f.Size / 1048576),4), ",", "")

Also check this line:
Quote
Set fs = fso.OpenTextFile("c:\temp\DirSize.csv", ForAppending, True)
The above line shows the path to the CSV file. If you changed it that's fine as long as you know where the CSV file is.

If this doesn't turn up anything, please post the script you are using. If you made any changes leave them in. Need to see the actual code that runs.

 8)
Title: Re: Batch file to check the folder size
Post by: sun_os on July 20, 2010, 08:59:56 PM
Hi Sidewinder

I run the same script on the another workstation, it is no problem on the size checking. I think something wrong on my workstation.

Last time, I request to check the directory size, the code is focus on that. If the script can let me check the size of drive or directory folder option.  I need to use compond condition if....if.... then....elsif  condition

Thanks
Title: Re: Batch file to check the folder size
Post by: sun_os on July 21, 2010, 01:18:31 AM
Hi Sidewinder

I run the same script on the another workstation, it is no problem on the size checking. I think it may be  something wrong with my workstation.

Last time I have the problem on the script to do the check the size, but I get message from my boss, he needs to check the case is run the script can show all the size of directory at once.

I check the all KB / MSI folder size under the directory e:\Patches$\KBXXXX\xxxxx and output to csv with the size, but it is different folder in the KBxxxx folder directory. The script is complex and need to to in effort to do that?

Thanks




Title: Re: Batch file to check the folder size
Post by: Sidewinder on July 21, 2010, 08:21:39 AM
Glad to see you got the script working. Not sure what could have caused the error on you other machine. VBScript is very forgiving where objects and variables are destroyed when the script ends. An exception is application objects (Word, Excel, etc) where they must be Quit, otherwise you'll find them in your task list long after the script ends.

Quote
Last time I have the problem on the script to do the check the size, but I get message from my boss, he needs to check the case is run the script can show all the size of directory at once.

Not sure what you're asking. I tweaked the script to display the subfolders. I also added a unit of measure to the CSV file so the numbers make more sense.

Be aware that the FileSystemObject recurses the directory tree when computing the size property. Each folder size includes all of it's subfolders. The numbers you see are correct but may appear inflated.

It's possible to retrieve the files from a recursive folder search, but the total size of the files will probably not add up to the size of the folder unless the folder has no subfolders. This would be easier in batch, however batch arithmetic only deals with integers. This can lead to some wildly inaccurate results. Hey! 700k bytes here, 700k bytes there, and pretty soon you're talking about some serious real estate. ;D

Code: [Select]
Const ForAppending = 8

Set fso = CreateObject("Scripting.FileSystemObject")
Set fs = fso.OpenTextFile("c:\temp\DirSize.csv", ForAppending, True)

Do
    WScript.StdOut.Write "Please enter directory name: "
    strFolder = WScript.StdIn.ReadLine
    If fso.FolderExists(strFolder) Then
Exit Do
    Else
  WScript.StdOut.Write "Invalid Directory ... Try Again" & vbCrLf
    End If
Loop

Set f = fso.GetFolder(strFolder)
WScript.Echo f.Path & ": " & GetEditSize(f.Size)
fs.WriteLine f.Path & "," & Replace(GetEditSize(f.Size), " ", ",")
GetThePaths(f)

fs.Close


Sub GetThePaths(Folder)
  For Each Subfolder in Folder.SubFolders
WScript.Echo SubFolder.Path & ": " & GetEditSize(SubFolder.Size)
    fs.WriteLine SubFolder.Path & "," & Replace(GetEditSize(SubFolder.Size), " ", ",")
GetThePaths Subfolder
  Next
End Sub

Function GetEditSize(intSize)
If intSize => (1024*1024*1024*1024) Then
GetEditSize = Round(FormatNumber((intSize/(1024*1024*1024)), 4,, TriStateTrue), 2) & " TB"
ElseIf intSize => (1024*1024*1024) Then
GetEditSize = Round(FormatNumber((intSize/(1024*1024*1024)), 4,, TriStateTrue), 2) & " GB"
ElseIf intSize => (1024*1024) Then
GetEditSize = Round(FormatNumber((intSize/(1024*1024)), 4,, TriStateTrue), 2) & " MB"
ElseIf intsize => 1024 Then
GetEditSize = Round(FormatNumber((intSize/1024), 4,, TriStateTrue), 2) & " KB"
Else
GetEditSize = intSize & " Bytes"
End If
End Function

I'm sure we'' here from you again. 8)
Title: Re: Batch file to check the folder size
Post by: Salmon Trout on July 21, 2010, 08:33:58 AM
batch arithmetic only deals with integers.

... and runs out of steam at 2 G (32-bit signed integer)
Title: Re: Batch file to check the folder size
Post by: sun_os on July 24, 2010, 04:06:43 AM
Hi Sidewinder, :D

Great !

I 'm appreciate your helpful and support the code. I can get the result what I want. 

Are you programme ?  You are specialist for vb script. Except for Microsoft link, what is other link for script which is useful for beiginner.  VBscript. Batch , Shell script


Thank you very much !   ;D
Title: Re: Batch file to check the folder size
Post by: Sidewinder on July 24, 2010, 05:35:59 AM
Are you programme ?  You are specialist for vb script.

I'm retired now after being in IT my entire career from EAM operator to systems administrator. Currently I run a small business in the local village helping some of the small business owners with their software needs and day to day PC operations.

For batch coding I always recommend this site (http://www.allenware.com/icsw/icswidx.htm)

For VBScript check out this tutorial (http://www.quackit.com/vbscript/tutorial/)

Also do a search on your hard drive for a file named script56.chm. This is a VBScript reference with lots of examples. Never leave home without it. ;D If you can't find it on your hard drive, you can download it from here (http://www.microsoft.com/downloads/details.aspx?familyid=01592c48-207d-4be1-8a76-1c4099d7bbb9&displaylang=en)

Good luck.  8)

Title: Re: Batch file to check the folder size
Post by: sun_os on July 24, 2010, 10:39:31 AM

I come from H.K. and work for system engineer.  My employment is only for the three months contract. The IT  in H.K. is no long term career, company prefer to recruit the outsource to support the inhouse I.T operation. 

I have a dream to start the own business but it is a lot of competitor in Hong Kong. The profit margin is  not very good in Hong Kong.

May I know where are you living ?


Enjoy your life and retirement !