Wednesday, February 9, 2011

Scrip to Get File Properties

'Get File Properties
On Error Resume Next
dim objFSO
dim objFile

strFile="c:\file.txt"

set objFSO=CreateObject("Scripting.FileSystemObject")
set objFile=objFSO.GetFile(strFile)

wscript.Echo "File Name:" & vbtab & objFile.Name
wscript.Echo "Short File Name:" & vbtab & objFile.ShortName
wscript.Echo "File Path:" & vbtab & objFile.Path
wscript.Echo "Date Created:" & vbtab & objFile.DateCreated
wscript.Echo "Date Last Accessed:" & vbtab & objFile.DateLastAccessed
wscript.Echo "Date Last Modified:" & vbtab & objFile.DateLastModified
wscript.Echo "File Size:" & vbtab & objFile.Size
wscript.Echo "File Attributes:"

if objFile.Attributes AND 0 then wscript.Echo " Normal"
if objFile.Attributes AND 1 then wscript.Echo " Read-only"
if objFile.Attributes AND 2 then wscript.Echo " Hidden"
if objFile.Attributes AND 4 then wscript.Echo " System"
if objFile.Attributes AND 8 then wscript.Echo " Volume"
if objFile.Attributes AND 16 then wscript.Echo " Directory"
if objFile.Attributes AND 32 then wscript.Echo " Archive Bit is set"
if objFile.Attributes AND 1024 then wscript.Echo " Alias"
if objFile.Attributes AND 2048 then wscript.Echo " Compressed"

set objFile=Nothing
set objFSO=Nothing

Script to get Drive Properties

'Get Drive Properties
On Error Resume Next
dim objFSO
dim objDrv
'drive we want to know about
strDrv="c:\"
set objFSO=CreateObject("Scripting.FileSystemObject")
'get reference to drive
set objDrv=objFSO.GetDrive(strDrv)
'list properties
'if volume name isn't defined then state that
if objDrv.VolumeName="" then
 wscript.Echo "Volume Name:" & vbtab & "NOT DEFINED"
else
 wscript.Echo "Volume Name:" & vbtab & objDrv.VolumeName
end if
wscript.Echo "Serial Number:" & vbtab & objDrv.SerialNumber
Select Case objDrv.DriveType
 Case 0  strType="Unknown"
 Case 1  strType="Removable"
 Case 2  strType="Fixed"
 Case 3  strType="Remote"
 Case 4  strType="CDROM"
 Case 5  strType="RamDisk"
 Case Else strType="Unknown"
end Select
wscript.Echo "Drive Type:" & vbtab  & strType
wscript.Echo "Is Ready:" & vbtab & objDrv.IsReady
wscript.Echo "File System:" & vbtab & objDrv.FileSystem
wscript.Echo "Total Size (bytes):" & vbtab & objDrv.TotalSize
wscript.Echo "Available Space (bytes):" & vbtab & objDrv.AvailableSpace
wscript.Echo "Free Space (bytes):" & vbtab & objDrv.FreeSpace

set objFSO=Nothing
set objDrv=Nothing

Script to Create a folder and delete a folder

'Create FolderOn Error Resume Next
dim objFSO

'new folder to create.  The parent folder must already exists
strFldr="C:\Neeraj"

set objFSO=CreateObject("Scripting.FileSystemObject")
objFSO.CreateFolder(strFldr)

set objFSO=Nothing

'Delete folder
'ANY FILES OR SUBDIRECTORIES WILL ALSO BE DELETED

On Error Resume Next
dim objFSO
'folder to delete
strFldr="C:\Neeraj"

set objFSO=CreateObject("Scripting.FileSystemObject")
objFSO.DeleteFolder(strFldr)

set objFSO=Nothing

Script to Write a text in a file

'Write to existing text file
On Error Resume Next
dim objFSO
dim objFile

Const ForWriting=2
'specify filename and path for file to open
strFile="c:\file.txt"

set objFSO=CreateObject("Scripting.FileSystemObject")
'if file already exists, value of TRUE forces overwriting file
set objFile=objFSO.OpenTextFile(strFile,ForWriting)
objFile.WriteLine Now & " this is a new line"

objFile.Close

set objFile=Nothing
set objFSO=Nothing

Script to read text from file

'Read Text File
On Error Resume Next
dim objFSO
dim objFile

Const ForReading=1
strFile="C:\file.txt"

set objFSO=CreateObject("Scripting.FileSystemObject")
set objFile=objFSO.OpenTextFile(strFile,ForReading)

'read in each line of data until you reach the end of the file
do While objFile.AtEndOfStream<>True
      strEntry=objFile.ReadLine
      'you can now do what ever you want with the line
      'as referenced with the strEntry variable such as
      'echoing it back (e.g. wscript.Echo strEntry) or passing it
      'as a variable to a function of subroutine (e.g. MyFunction strEntry)
     wscript.Echo strFile
loop

'close file
objFile.Close

set objFSO=Nothing
set objFile=Nothing

Script to Move a file from one Place to another

'Move File
On Error Resume Next
dim objFSO
dim objFile

'specify filename and path for file to move
strFile="c:\file.txt"
'specify target destination
strDest="c:\Neeraj\"

set objFSO=CreateObject("Scripting.FileSystemObject")
'move will fail if file already exists in target destination
objFSO.MoveFile strFile,strDest

if err.number=0 then
       wscript.Echo "Successfully moved " & strFile & " to " & strDest
else
       wscript.Echo "Failed to move " & strFile & " to " & strDest
end if

err.Clear
set objFSO=Nothing

Script to Copy File from one Place to another

'Copy File from one Place to another
On Error Resume Next
dim objFSO
dim objFile

'specify filename and path for file to copy
strFile="c:\file.txt"
'specify target destination
strDest="c:\Neeraj\"

set objFSO=CreateObject("Scripting.FileSystemObject")
'set last parameter to TRUE if you want to overwrite
'an existing file with the same name
objFSO.CopyFile strFile,strDest,TRUE

if err.number=0 then
      wscript.Echo "Successfully copied " & strFile & " to " & strDest
else
      wscript.Echo "Failed to copy " & strFile & " to " & strDest
end if

set objFSO=Nothing