user1724708
user1724708

Reputation: 1469

How to get the file size of any file type using VBScript even if the file name is modified?

I just started using VBScript today and I'm attempting to get the file size of any file type. My script only returns the file size of the file name provided, but if the file name changes, it renders an error: "file not found"

Here is the script:

CONST bytesToKb = 1024
strFile = "X:\My Docs\Reports\Report1.txt"

SET objFSO = CREATEOBJECT("Scripting.FileSystemObject")
SET objFile = objFSO.GetFile(strFile)

WScript.StdOut.WriteLine CINT(objFile.Size / bytesToKb) 

how can I modify the second line to accept any file name, in case the file name Report1.txt is changed to demo.txt? The path to the file will remain the same, just the file name will change. ...Thanks in advance

Upvotes: 1

Views: 9568

Answers (1)

Hackoo
Hackoo

Reputation: 18827

You can fix your Main Folder and check all files on it by a loop :

Const bytesToKb = 1024
Set objFSO = CreateObject("Scripting.FileSystemObject")
MainFolder = "E:\TestFolder"
Set objFolder = objFSO.GetFolder(MainFolder)
For each objFile in objFolder.Files
    wscript.echo objFile.Name & vbTab & CINT(objFile.Size/bytesToKb) & " kb"
Next

Upvotes: 4

Related Questions