Friday, April 18, 2014

Windows Scripting (the normal shell) for Ex VB Programmers - Lesson 4–FileSystemObject Contd..

Continuing on FileSystemObject, Let us look at and example to extract the properties of a file. There are some new things introduced in this example

1. SUB. This is for a procedure. you can pass parameteres just like functions but just that you cannot return any values. I have put a simple procedure called Print, that takes a string as a parameter.

2. Parameteres.  You can pass parameteres to a vbscript. Wscript.argument, is where you can see the list.

Sub Print(x)
    wscript.echo x
end sub


set fso=CreateObject("Scripting.FileSystemObject")
set f=fso.GetFile(wscript.arguments(0))
print "Path               " & f.path
print "Date Created       " & f.datecreated
print "Date Last Accessed " & f.datelastaccessed
print "Date last modified " & f.datelastmodified
print "Drive              " & f.drivedir
print "Parent Folder      " & f.parentfolder
print "Size (Bytes)       " & f.size
print "Type of File       " & f.type

A few examples

C:\scripts>folder.vbs c:\53-Photos\ar-1.jpg
Microsoft (R) Windows Script Host Version 5.8
Copyright (C) Microsoft Corporation. All rights reserved.

Path                       C:\53-Photos\ar-1.jpg
Date Created          09/03/2014 19:50:08
Date Last Accessed  09/03/2014 19:50:08
Date last modified  09/03/2014 19:50:08
Drive                      c:
Parent Folder          C:\53-Photos
Size (Bytes)             66089
Type of File            JPEG Image

C:\scripts>folder folder.vbs
Microsoft (R) Windows Script Host Version 5.8
Copyright (C) Microsoft Corporation. All rights reserved.

Path               C:\scripts\folder.vbs
Date Created       18/04/2014 00:01:02
Date Last Accessed 18/04/2014 00:01:02
Date last modified 18/04/2014 01:06:49
Drive              C:
Parent Folder      C:\scripts
Size (Bytes)       491
Type of File       VBScript Script File

C:\scripts>

You can try an example to show the file size in MB or GB depending on the size of the file. Or may be list an output like the DIR command.

No comments: