VBS Operation

上一篇 / 下一篇  2010-05-17 23:05:28 / 个人分类:VBScript

Option Explicit
On Error Resume Next
Dim colDrives 'the collection that comes from WMI
Dim drive  
'an individual drive in the collection

Const DriveType = 3 'Local drives. From the SDK
Set colDrives =_
GetObject("winmgmts:").ExecQuery("select size,freespace " &_
 "from Win32_LogicalDisk where DriveType =" & DriveType)

For Each drive In colDrives 'walks through the collection
WScript.Echo "Drive: " & drive.DeviceID
WScript.Echo "Size: " & drive.size
WScript.Echo "Freespace: " & drive.freespace
Next

'Create a FSO instance,then create FSO perpoty,using For Each function to get the drives info

'This is another method to get the drives information for current machine
Set bjFSO = CreateObject("Scripting.FileSystemObject")
Set colDrives = objFSO.Drives
For Each objDrive In colDrives
If objDrive.IsReady = True Then
Wscript.Echo "Drive letter: " & objDrive.DriveLetter
Wscript.Echo "Free space: " & objDrive.FreeSpace
Else
Wscript.Echo "Drive letter: " & objDrive.DriveLetter
End If
Next

'call the function CreateFolderandFile
CreateFolderandFile "folder1","folder2"


Function CreateFolderandFile(folder1,folder2)
Dim objFSO,objFolder,objFile
Set bjFSO=CreateObject("Scripting.FileSystemObject")
'seach the folder,if exist delete,or create
If (objFSO.FolderExists("folder1")and objFSO.FolderExists("folder2")) Then
objFSO.DeleteFolder("folder1")
objFSO.DeleteFolder("folder2")
Else
objFSO.CreateFolder("folder1")
objFSO.CreateFolder("folder2")
End If
'create txt file under the directory,if exist delete,or create
If objFSO.FileExists("folder1\ScriptLog.txt") Then
objFSO.DeleteFile("folder1\ScriptLog.txt")
Else
objFSO.CreateTextFile("folder1\ScriptLog.txt")
End If
'open the txt file and writing
Set bjFile=objFSO.OpenTextFile("folder1\ScriptLog.txt",ForWriting)
objFile.WriteBlankLines(2) 'writing blanklines
objFile.Write (InputBox("Please input the character","Write operation",vbYesNo))'input for user
objFile.WriteLine("This is an example for writing txt file")'write the string to file
objFSO Now 'write the current time
objFile.Close
objFSO.MoveFile "folder1\ScriptLog.txt","folder2\"'move the file to folder2
Set bjFile=objFSO.OpenTextFile("folder2\Script.txt",ForAppending)'appending content
objFile.WriteLine("This is appending line")
Wscript.Echo "Reading file beginning:"
Do While objFile.AtEndOfStream = False
strLine = objFile.ReadLine
objFile.Close
Wscript.Echo strLine
Loop
End Function


TAG:

 

评分:0

我来说两句

Open Toolbar