Distinguish between "file doesn't exist" and "access denied" - vbscript

In a VBScript, I want to know if a file exists:
Set fso = CreateObject("Scripting.FileSystemObject")
If Not (fso.FileExists(file)) Then
msg = " doesn't exist."
End If
My files are on internal network.
Is there a way to distinguish:
file really doesn't exist
access denied
I try with fso.OpenTextFile but the result for these two cases is always: Err.Number = 5.

To distinguish between non-existing and non-accessible files you need .FileExists and .OpenTextFile:
Option Explicit
Dim goFS : Set goFS = CreateObject("Scripting.FileSystemObject")
Function ReadFile(p, ByRef m)
If goFS.FileExists(p) Then
Dim aErr
On Error Resume Next
Set ReadFile = goFS.OpenTextFile(p)
aErr = Array(Err.Number, Err.Description)
On Error GoTo 0
If aErr(0) Then
m = p & " - " & aErr(1)
Set ReadFile = Nothing
Else
m = ""
End If
Else
Set ReadFile = Nothing
m = p & " - no such file"
End If
End Function
Dim p, m
For Each p In Split("e:\roots.own e:\nosuchfile e:\dirsbf.tmp")
Dim tsIn : Set tsIn = ReadFile(p, m)
If tsIn Is Nothing Then
WScript.Echo "fail", m
Else
' read from tsIn
tsIn.Close
WScript.Echo "ok"
End If
Next
Output:
cscript 35338634.vbs
fail e:\roots.own - Permission denied
fail e:\nosuchfile - no such file
ok
Thanks to Ansgar's observation, the function can be improved:
Function ReadFile(p, ByRef m)
Dim aErr
On Error Resume Next
Set ReadFile = goFS.OpenTextFile(p)
aErr = Array(Err.Number, Err.Description)
On Error GoTo 0
If aErr(0) Then
m = p & " - " & aErr(1)
Set ReadFile = Nothing
Else
m = ""
End If
End Function

Related

Error Handling in VBScript to check for number of files in a folder

I have written a script to check that 4 files exist in a folder. I need to use the below error handling for this particular small piece of code.
This code checks various folders to see if it contains 4 files. If it does then good if it doesn't then its not good.
Code:
Const intOK = 0
Const intCritical = 2
Const intError = 3
Const ForReading=1,ForWriting=2,ForAppending=8
Dim filename,filenamemov,emailaddr
Dim arrFolders(17)
Dim argcountcommand,dteToday
Dim arg(4)
Dim strMailServer,Verbose,strExt,numfiles,intIndex
Dim intcount : intCount = 0
Dim stroutput
numfiles = 4 'how many minutes old are the files
Verbose = 0 '1 FOR DEBUG MODE, 0 FOR SILENT MODE
arrFolders(0) = "D:\AS2\Inbound\WESSEX"
arrFolders(1) = "D:\AS2\Inbound\EATWELL"
arrFolders(2) = "D:\AS2\Inbound\TURNER\"
For intIndex = 0 To UBound(arrFolders)
pt "Checking folder: " & arrFolders(intIndex)
If arrFolders(intIndex) = "" Then
pt "Empty folder value!"
Exit For
Else
Call checkfiles(arrFolders(intIndex))
End If
Next
If objFolder.Files.Count < 4 Then
WScript.Echo "CRITICAL - " & intCount & " File(s) over " & numfiles & "
minutes in " & stroutput
WScript.Quit(intCritical)
Else
WScript.Echo "OK - No directory contains less than 4 files"
WScript.Quit(intOK)
End If
Sub checkfiles(folderspec)
'check If any files exist on folder
On Error Resume Next
Dim objFso : Set objFso = CreateObject("Scripting.FileSystemObject")
'you can take this as input too using InputBox
'this will error If less than 4 files exist.
Dim objFolder : Set objFolder = objFso.GetFolder(strFolderPath)
If objfolder.Files.Count = 4 Then
MsgBox "This is Correct"
Else
MsgBox "This isnt Correct"
End If
Sub pt(txt)
If Verbose = 1 Then
WScript.Echo txt
End If
End Sub
If i understand your question, you want to
check a set of folders kept in an array for the number of files they contain
if any of the folders has less than 4 files, an error message should be displayed
the folder array CAN contain empty values and the routine should skip those
Apparently you have more plans considering all 'extra' variables you have in your code like Const ForReading=1,ForWriting=2,ForAppending=8 and Dim filename,filenamemov,emailaddr, but I left them out here, because they have nothing to do with the question at hand.
Option Explicit
Const intOK = 0
Const intCritical = 2
Const intError = 3
Dim Verbose, path, fileCount, minCount, intCount, errCount
Dim objFso, objFolder, intResult, strResult
Verbose = 0 '1 FOR DEBUG MODE, 0 FOR SILENT MODE
minCount = 4 'The minimal number of files each folder should have
Dim arrFolders(17)
arrFolders(0) = "D:\AS2\Inbound\WESSEX"
arrFolders(1) = "D:\AS2\Inbound\EATWELL"
arrFolders(2) = "D:\AS2\Inbound\TURNER\"
Set objFso = CreateObject("Scripting.FileSystemObject")
intResult = intOK 'assume all is well
strResult = "" 'to accumulate critical errors for each folder
intCount = 0 'the total running count of folders we have checked
errCount = 0 'the total count of errors (folders with less than 4 files) we have found
For Each path In arrFolders
If Len(path) > 0 Then
intCount = intCount + 1
WScript.Echo "Checking folder: " & path
Set objFolder = objFso.GetFolder(path)
fileCount = objfolder.Files.Count
'considering the "OK - No directory contains less than 4 files" message
'in your original post, this test needs to do a 'Greater Than Or Equal To', so use >=
If fileCount >= minCount Then
WScript.Echo "This is correct: " & path
Else
WScript.Echo "This is NOT correct: " & path
strResult = strResult & vbNewLine & "CRITICAL - " & fileCount & " File(s) in folder " & path
intResult = intCritical
errCount = errCount + 1
End If
End if
Next
'clean up used objects
Set objFolder = Nothing
Set objFso = Nothing
If errCount > 0 Then
'we have folders with errors
WScript.Echo strResult
Else
'This message implies that a folder is also 'Good' when more than 4 files exist
WScript.Echo "OK - All " & intCount & " directories contain at least " & minCount & " files"
End If
WScript.Quit(intResult)

How Can I pause speak command in vbscript? I have to play it from the same paused position

How Can I pause speak command in vbscript? I have to play it from the same paused position.
Code block:
Dim Speak, Path
Path = "string"
Path = "C:\Users\sony\Desktop\TheReunion.txt"
const ForReading = 1
Set objFileToRead = CreateObject("Scripting.FileSystemObject").OpenTextFile(Path,ForReading)
strFileText = objFileToRead.ReadAll()
Set Speak=CreateObject("sapi.spvoice")
Speak.Speak strFileText
objFileToRead.Close
Set objFileToRead = Nothing
You need to call the speak method asynchronously before using the pause and resume methods as mentioned by LotPings in the comments.
Code:
Dim Speak, Path
Path = "string"
Path = "C:\Users\sony\Desktop\TheReunion.txt"
const ForReading = 1
Set objFileToRead = CreateObject("Scripting.FileSystemObject").OpenTextFile(Path,ForReading)
strFileText = objFileToRead.ReadAll()
Set Speak=CreateObject("sapi.spvoice")
Speak.Speak strFileText,1 '1=Asynchronous. Click the link below for other possible values "SpeechVoiceSpeakFlags"
'Due to async call to speak method, we can proceed with the code execution while the voice is being played in the background. Now we can call pause and resume methods
wscript.sleep 5000 'voice played for 5000ms
Speak.pause 'paused
wscript.sleep 4000 'remains paused for 4000ms
Speak.resume 'resumes
objFileToRead.Close
Set objFileToRead = Nothing
SpeechVoiceSpeakFlags
Intrigue in this led me to take inspiration from Kira's answer and develop it somewhat (in a bad, novice kind of way), to achieve the pause/resume objective interactively, the code below works for me, and hopefully it's of some help to you...
option explicit
dim strpath, fso, strfile, strtxt, user, voice, flag
flag = 2
call init
sub init
do while len(strpath) = 0
strpath = inputbox ("Please enter the full path of txt file", "Txt to Speech")
if isempty(strpath) then
wscript.quit()
end if
loop
'strpath = "C:\Users\???\Desktop\???.txt"
set fso = createobject("scripting.filesystemobject")
on error resume next
set strfile = fso.opentextfile(strpath,1)
if err.number = 0 then
strtxt = strfile.readall()
strfile.close
call ctrl
else
wscript.echo "Error: " & err.number & vbcrlf & "Source: " & err.source & vbcrlf &_
"Description: " & err.description
err.clear
call init
end if
end sub
sub ctrl
user = msgbox("Press ""OK"" to Play / Pause", vbokcancel + vbexclamation, "Txt to Speech")
select case user
case vbok
if flag = 0 then
voice.pause
flag = 1
call ctrl
elseif flag = 1 then
voice.resume
flag = 0
call ctrl
else
call spk
end if
case vbcancel
wscript.quit
end select
end sub
sub spk
'wscript.echo strtxt
set voice = createobject("sapi.spvoice")
voice.speak strtxt,1
flag = 0
call ctrl
end sub

subscript out of range error in vbscript

Can someone look at the below script and tell me why it's throwing this error subscript out of range error in vbscript ..In the text file there are two entries it writes to the file correctly but then it throws an error while exiting the loop so it never calls the other function..I think it's trying to run 3 times but there are just 2 entries in the text file
The text file is in this format
Format.css Shared
Design.css Shared
Dim strInputPath1
Dim txsInput1,txsOutput
Dim FSO
Dim Filename
Set FSO = CreateObject("Scripting.FileSystemObject")
strOutputPath = "C:\txt3.txt"
Set txsOutput = FSO.CreateTextFile(strOutputPath)
Set re = New RegExp
re.Pattern = "\s+"
re.Global = True
Set f = FSO.OpenTextFile("C:\Users\spadmin\Desktop\Main\combination.txt")
Do Until f.AtEndOfStream
tokens = Split(Trim(re.Replace(f.ReadLine, " ")))
extension = Split(tokens(0),".")
strInputPath1 = "C:\inetpub\wwwroot\Test\files\" & tokens(1) & "\" & extension(1) & "\" & tokens(0)
Set txsInput1 = FSO.OpenTextFile(strInputPath1, 1)
WScript.Echo strInputPath1
txsOutput.Writeline txsInput1.ReadAll
Loop
WScript.Echo "Calling"
txsInput1.Close
txsOutput.Close
f.Close
Call CreateCSSFile()
''''''''''''''''''''''''''''''''''''
' Merge Css Files
''''''''''''''''''''''''''''''''''''
Sub CreateCSSFile()
WScript.Echo "Called"
Dim FilenameCSS
Dim strInputPathCSS
Dim txsInputCSS,txsOutputCSS
Dim FSOCSS
Set FSOCSS = CreateObject("Scripting.FileSystemObject")
strOutputPathCSS = "C:\txt4.txt"
Set txsOutputCSS = FSOCSS.CreateTextFile(strOutputPath)
Set re = New RegExp
re.Pattern = "\s+"
re.Global = True
Set fCSS = FSOCSS.OpenTextFile("C:\Users\spadmin\Desktop\TestingTheWebService\combination.txt")
Do Until fCSS.AtEndOfStream
tokensCSS = Split(Trim(re.Replace(fCSS.ReadLine, " ")))
extensionCSS = Split(tokensCSS(0),".")
strInputPathCSS = "C:\inetpub\wwwroot\EpsShared\c\" & tokensCSS(1) & "\" & extensionCSS(1) & "\" & tokensCSS(0)
Set txsInputCSS = FSOCSS.OpenTextFile(strInputPathCSS, 1)
txsOutputCSS.Writeline txsInputCSS.ReadAll
Loop
fCSS.Close
txsInputCSS.Close
txsOutputCSS.Close
Set FSOCSS = Nothing
End Sub
If your file contains trailing blank lines, applying Split() may return arrays with less than 2 elements. In that case token(1) should throw a 'subscript out of range' error.
You should always check, if Split() workes as expected:
tokens = Split(Trim(re.Replace(f.ReadLine, " ")))
If 1 = UBound(tokens) Then
extension = Split(tokens(0),".")
If 1 = UBound(extension) Then
strInputPath1 = "..." & tokens(1) & "..."
Else
... parse error ...
End If
Else
... parse error or just trailing blank lines? ...
End If

To run two or more .VBS script parallely

I have two .vbs file say a.vbs and b.vbs.Now both are written for the same Excel,but would work on 2 different sheets.So can we run those in parallel?
EDIT
a.vbs will update sheet2 and b.vbs will update sheet3.But for both source sheet is sheet1.
Please advice how to set such environment
CODE A
Option Explicit
Dim objExcel1
Dim strPathExcel1
Dim objSheet1,objSheet2
Dim IntRow1,IntRow2
Dim ColStart
Set objExcel1 = CreateObject("Excel.Application")'Object for Condition Dump
strPathExcel1 = "D:\AravoVB\Copy of Original Scripts\CopyofGEWingtoWing_latest_dump_21112012.xls"
objExcel1.Workbooks.open strPathExcel1
Set objSheet1 = objExcel1.ActiveWorkbook.Worksheets(1)
Set objSheet2 = objExcel1.ActiveWorkbook.Worksheets("Bad Data")
objExcel1.ScreenUpdating = False
objExcel1.Calculation = -4135 'xlCalculationManual
IntRow2=2
IntRow1=4
Do Until IntRow1 > objSheet1.UsedRange.Rows.Count
ColStart = objExcel1.Application.WorksheetFunction.Match("Parent Business Process ID", objSheet1.Rows(3), 0) + 1
Do Until ColStart > objSheet1.UsedRange.Columns.Count And objSheet1.Cells(IntRow1,ColStart) = ""
If objSheet1.Cells(IntRow1,ColStart + 1) > objSheet1.Cells(IntRow1,ColStart + 5) And objSheet1.Cells(IntRow1,ColStart + 5) <> "" Then
objSheet1.Range(objSheet1.Cells(IntRow1,1),objSheet1.Cells(IntRow1,objSheet1.UsedRange.Columns.Count)).Copy
objSheet2.Range(objSheet2.Cells(IntRow2,1),objSheet2.Cells(IntRow2,objSheet1.UsedRange.Columns.Count)).PasteSpecial
IntRow2=IntRow2+1
Exit Do
End If
ColStart=ColStart+4
Loop
IntRow1=IntRow1+1
Loop
objExcel1.ScreenUpdating = True
objExcel1.Calculation = -4105 'xlCalculationAutomatic
CODE B
Option Explicit
Dim objExcel1
Dim strPathExcel1
Dim objSheet1,objSheet2
Dim IntRow1,IntRow2
Dim Flag
Dim IntColTemp,IntRowTemp
Dim Strcmp1,Strcmp2
Flag=0
IntColTemp=1
IntRowTemp=3
Set objExcel1 = CreateObject("Excel.Application")'Object for Condition Dump
If Err.Number <> 0 Then
On Error GoTo 0
Wscript.Echo "Excel application not found."
Wscript.Quit
End If
strPathExcel1 = "D:\VA\CopyofGEWingtoWing_latest_dump_21112012.xls"
objExcel1.Workbooks.open strPathExcel1
Set objSheet1 = objExcel1.ActiveWorkbook.Worksheets(1)
Set objSheet2 = objExcel1.ActiveWorkbook.Worksheets(2)
IntRow1=4
IntRow2=1
Do While objSheet1.Cells(IntRow1, 1).Value <> ""
objSheet2.Cells(IntRow2, 1).Value = objSheet1.Cells(IntRow1, 1).Value
IntColTemp=1
Flag=0
'This will travarse to the Parent Business Process ID column horizantally in the excel.
Do While Flag=0
If objSheet1.Cells(IntRowTemp,IntColTemp).Value="Parent Business Process ID" Then
Flag=1
End If
IntColTemp=IntColTemp+1
Loop
IntColTemp=IntColTemp-1
'MsgBox(IntColTemp)
Strcmp1=trim(objSheet1.Cells(IntRow1, 1).Value)
Strcmp2=trim(objSheet1.Cells(IntRow1,IntColTemp).Value)
If Strcmp1=Strcmp2 Then
objSheet2.Cells(IntRow2, 2).Value="Parent"
Else
objSheet2.Cells(IntRow2, 2).Value="child"
End If
IntRow1=IntRow1+1
IntRow2=IntRow2+1
Loop
Working on two different sheets should be possible by putting something like this in both of your scripts:
strPathExcel1 = "D:\CopyofGEWingtoWing_latest_dump_21112012.xls"
On Error Resume Next
Set objExcel1 = GetObject(, "Excel.Application") ' attach to running instance
If Err.Number = 429 Then ' if that fails
Err.Clear
Set objExcel1 = CreateObject("Excel.Application") ' create new instance
If Err Then ' if that still fails
WScript.Echo Err.Description & " (0x" & Hex(Err.Number) & ")"
WScript.Quit 1 ' report error and terminate
End If
objExcel1.Workbooks.Open strPathExcel1
End If
On Error Goto 0
However, I doubt that this approach would gain you enough performance to justify the additional complexity.
In CODE A replace the lines
Set objExcel1 = CreateObject("Excel.Application")'Object for Condition Dump
strPathExcel1 = "D:\AravoVB\Copy of Original Scripts\CopyofGEWingtoWing_latest_dump_21112012.xls"
objExcel1.Workbooks.open strPathExcel1
with the above code block.
In CODE B replace the lines
Set objExcel1 = CreateObject("Excel.Application")'Object for Condition Dump
If Err.Number <> 0 Then
On Error GoTo 0
Wscript.Echo "Excel application not found."
Wscript.Quit
End If
strPathExcel1 = "D:\VA\CopyofGEWingtoWing_latest_dump_21112012.xls"
objExcel1.Workbooks.open strPathExcel1
with the above code block.

How to check for registry value using VbScript

How can I check for registry value using VbScript?
Function ReadFromRegistry(strRegistryKey, strDefault)
Dim WSHShell, value
On Error Resume Next
Set WSHShell = CreateObject("WScript.Shell")
value = WSHShell.RegRead( strRegistryKey )
If err.number <> 0 Then
readFromRegistry = strDefault
Else
readFromRegistry = value
End If
Set WSHShell = Nothing
End Function
Usage :
str = ReadfromRegistry("HKEY_LOCAL_MACHINE\SOFTWARE\Adobe\ESD\Install_Dir", "ha")
WScript.echo "returned " & str
Original post
Try something like this:
Dim windowsShell
Dim regValue
Set windowsShell = CreateObject("WScript.Shell")
regValue = windowsShell.RegRead("someRegKey")
This should work for you:
Dim oShell
Dim iValue
Set oShell = CreateObject("WScript.Shell")
iValue = oShell.RegRead("HKLM\SOFTWARE\SOMETHINGSOMETHING")
Try this. This script gets current logged in user's name & home directory:
On Error Resume Next
Dim objShell, strTemp
Set objShell = WScript.CreateObject("WScript.Shell")
strTemp = "HKEY_CURRENT_USER\Volatile Environment\USERNAME"
WScript.Echo "Logged in User: " & objShell.RegRead(strTemp)
strTemp = "HKEY_CURRENT_USER\Volatile Environment\USERPROFILE"
WScript.Echo "User Home: " & objShell.RegRead(strTemp)
Set objShell = WScript.CreateObject("WScript.Shell")
skey = "HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\{9A25302D-30C0-39D9-BD6F-21E6EC160475}\"
with CreateObject("WScript.Shell")
on error resume next ' turn off error trapping
sValue = .regread(sKey) ' read attempt
bFound = (err.number = 0) ' test for success
end with
if bFound then
msgbox "exists"
else
msgbox "not exists"
End If

Resources