VBS script runs when variable is hard coded but not when variable comes from an argument drag and drop - vbscript

I hope that some brains out there can solve this frustrating puzzle for me.
The script (cobbled together from various bits from on the net) works perfectly when run by double clicking, the file to be split is hard coded.
When dragging and dropping the file onto the script, there is an error for 'File not found'
Please help!
I have tried the answer provided, but then the script runs without failure, but also does not output the three files as it does when 'textFile' value is hard coded.
if WScript.Arguments.Count <> 0 then
textFile = WScript.Arguments(0)
else
textFile = "multi2.txt"
end if
saveTo = ""
writeTo = ""
strNewLine = "%_N_"
headingPattern = "(%_N_)"
dim fileFrom, regex, fileTo
Set fso = CreateObject("Scripting.FileSystemObject")
set fileFrom = fso.OpenTextFile(textFile)
set regex = new RegExp
set fileTo = nothing
with regex
.Pattern = headingPattern
.IgnoreCase = false
.Global = true
end with
while fileFrom.AtEndOfStream <> true
line = fileFrom.ReadLine
set matches = regex.Execute(line)
if matches.Count > 0 then
strCheckForString = UCase("%")
strNewLine = "%_N_"
StrContents = Split(fso.OpenTextFile(textFile).ReadAll, vbNewLine)
If (Left(UCase(LTrim(line)),Len(strCheckForString)) = strCheckForString) Then
line = Right(line, len(line)-4)
line1 = Left(line, len(line)-4)
writeTo = saveTo & (line1 & ".arc")
if not(fileTo is nothing) then fileTo.Close()
set fileTo = fso.CreateTextFile(writeTo)
fileTo.WriteLine(strNewLine & line)
else
fileTo.WriteLine(line)
End If
else
fileTo.WriteLine(line)
end if
wend
fileFrom.Close()
set fileFrom = nothing
set fso = nothing
set regex = nothing
The text file looks like this:
%_N_160_SP01_MPF
;$PATH=/_N_WKS_DIR/_N_AFO160_WPD
blah blah blah
%_N_160_SP02_MPF
;$PATH=/_N_WKS_DIR/_N_AFO160_WPD
blah blah blah
%_N_160_SP99_MPF
;$PATH=/_N_WKS_DIR/_N_AFO160_WPD
blah blah blah

It looks like your code is supposed to extract the filename from the first argument:
textFile = Right(WScript.Arguments(0), len(WScript.Arguments(0))-44)
and then open the file using just the filename:
set fileFrom = fso.OpenTextFile(textFile)
OpenTextFile is looking for a relative path below the current working directory, unless it's provided with an absolute path. When you run the script by double-clicking it, the working directory is the folder from which you launch the script. When you drop a file onto the script, the working directory may be something entirely different.
If your input file is located in the same folder as the script, that would explain why it works when started via double-click, but not when dropping the file on the script. In the latter case the script would be looking for multi2.txt in the wrong place.
You can verify that by adding the following lines at the beginning of your script.
WScript.Echo CreateObject("WScript.Shell").CurrentDirectory
WScript.Echo CreateObject("Scripting.FileSystemObject").GetParentFolderName(WScript.ScriptFullName)
I suspect you'll get two different paths.
You can fix this issue by not removing the path from the argument. Change this:
if WScript.Arguments.Count <> 0 then
textFile = Right(WScript.Arguments(0), len(WScript.Arguments(0))-44)
'textFile = (chr(34) & textFile & chr(34))
else
textFile = "multi2.txt"
end if
into this:
If WScript.Arguments.Count <> 0 Then
textFile = WScript.Arguments(0)
Else
textFile = "multi2.txt"
End If
and the code should work as expected.

Related

How to Check if a File does NOT exist and display one overall response using a For Loop

I'm trying to write a VBScript that will check whether a file exists in a folder or not based on a partial number. If anything in the folder has this number in the string it can continue, if not an error needs to display saying it's not in the system. I've gotten a code that lets me know that the file DOES exist, but I can't get a NOT version to work. Any ideas?
Dim FSO, str1, fileName
str1 = "001234"
Set FSO = CreateObject("Scripting.FileSystemObject")
Set objFolder = FSO.GetFolder("C:\Users\GDoe\Desktop\FolderA\")
For Each objFile In objFolder.Files
fileName = objFile.Name
If InStr(fileName, str1) Then
MsgBox("Proceed")
Exit For
End If
Next
Unfortunately the FileSystemObject's FileExists method does not support wildcards, so the straightforward approach is not possible here.
The code you posted in your question is basically how one would check for the existence of a file with a partial name with VBScript and the FileSystemObject. You can modify that code into a check for the absence of a file with some minor changes. Define a variable before the loop and set it to False, then instead of displaying a message box set that vriable to True when you find a matching file:
fileFound = False
For Each objFile In objFolder.Files
fileName = objFile.Name
If InStr(fileName, str1) Then
fileFound = True
Exit For
End If
Next
If fileFound Then
MsgBox("Proceed")
Else
MsgBox("File doesn't exist.")
End If
Alternatively, you could shell out and check the exit code of the dir command:
Function FileExists(path, namepart)
Set sh = CreateObject("WScript.Shell")
rc = sh.Run("cmd /c dir ""*" & path & "\" & namepart & "*""", 0, True)
FileExists = Not CBool(rc)
End Function
dir returns 0 if it finds matching file(s) and 1 if it doesn't. CBool() converts the integer return code into a boolean value (0 → False, 1 → True). The negation then corrects the logic from "false if found" to "true if found".
Of course you could also name the function FileMissing and remove the negation, so that the function returns True if no matching file is found. That's just a matter of what logic works best in your code.
Note that you need to run the command with cmd /c, because dir is a cmd.exe builtin command, not an executable.
I actually just found a way to answer my own question but if there's a better way I'd also love to know.
Dim FSO, str1, fileName
str1 = "-001239"
Set FSO = CreateObject("Scripting.FileSystemObject")
Set objFolder = FSO.GetFolder("C:\Users\GDoe\Desktop\FolderA\")
For Each objFile In objFolder.Files
fileName=objFile.Name
If InStr(fileName, str1) Then
MsgBox("Proceed")
Exit For
End If
Next
If InStr(fileName, str1) = 0 Then
MsgBox("File doesn't Exist")
End If
I took the rule that if string2 is not found in an InStr command it returns 0. Setting the result = 0 shows if I don't have the file.

800A0401 - expected end of statement on "For Output"

I am getting the error
800A0401 - expected end of statement
in VBScript.
Please clarify on what is wrong.
Const ForReading = 1
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objTextFile = objFSO.OpenTextFile("C:\Users\RAJDIQ\Desktop\Macros\11082017\SingleFile.txt", ForReading)
strLine = objTextFile.ReadLine
Set folder = objFSO.GetFolder("C:\Users\RAJDIQ\Desktop\Macros\11082017\")
Set outfile = objFSO.OpenTextFile("C:\Users\RAJDIQ\Desktop\Macros\11082017\comparedel.txt")
myFile ="C:\Users\RAJDIQ\Desktop\Macros\11082017\Output.txt"
Open myFile for Output As #1
t = 0
Do Until outfile.AtEndOfStream
strLine = outfile.ReadLine
If InStr(strLine, substrToFind) <> 0 Then
t = t+1
Else
[ Lines = Lines & t & ","
Write #1, Lines]
End If
Loop
MsgBox "Complete"
You're confusing VBA with VBScript. Open myFile for Output As #1 isn't valid VBScript code, and neither is Write #1 or the square brackets. In VBScript you handle files via the FileSystemObject.
Set outputFile = objFSO.OpenTextFile(myFile, 2, True)
'stuff
outputFile.WriteLine "something"
'more stuff
outputFile.Close
The parameter 2 opens the file for writing, the parameter True ensures the file is created in case it's missing.
Please check the documentation for further details.

No output from VBS script when variable is an argument but outputs correctly when variable is hard coded

This is strange:
The script works perfectly when run by double clicking [outputs the (split)arc files], when textFile is hard coded.
When dragging and dropping the file onto the script, there are no errors but also no output files.
The script definitely runs when using drag and drop, I added a simple message box to the line just before writeTo, to confirm that it does reach that point.
No files output when dragging and dropping though, only works correctly when textFile is hard coded.
Someone please help!
if WScript.Arguments.Count <> 0 then
textFile = WScript.Arguments(0)
else
textFile = "multi2.txt"
end if
saveTo = ""
writeTo = ""
strNewLine = "%_N_"
headingPattern = "(%_N_)"
dim fileFrom, regex, fileTo
Set fso = CreateObject("Scripting.FileSystemObject")
set fileFrom = fso.OpenTextFile(textFile)
set regex = new RegExp
set fileTo = nothing
with regex
.Pattern = headingPattern
.IgnoreCase = false
.Global = true
end with
while fileFrom.AtEndOfStream <> true
line = fileFrom.ReadLine
set matches = regex.Execute(line)
if matches.Count > 0 then
strCheckForString = UCase("%")
strNewLine = "%_N_"
StrContents = Split(fso.OpenTextFile(textFile).ReadAll, vbNewLine)
If (Left(UCase(LTrim(line)),Len(strCheckForString)) = strCheckForString) Then
line = Right(line, len(line)-4)
line1 = Left(line, len(line)-4)
writeTo = saveTo & (line1 & ".arc")
if not(fileTo is nothing) then fileTo.Close()
set fileTo = fso.CreateTextFile(writeTo)
fileTo.WriteLine(strNewLine & line)
else
fileTo.WriteLine(line)
End If
else
fileTo.WriteLine(line)
end if
wend
fileFrom.Close()
set fileFrom = nothing
set fso = nothing
set regex = nothing
The text file looks like this:
%_N_160_SP01_MPF
;$PATH=/_N_WKS_DIR/_N_AFO160_WPD
blah blah blah
%_N_160_SP02_MPF
;$PATH=/_N_WKS_DIR/_N_AFO160_WPD
blah blah blah
%_N_160_SP99_MPF
;$PATH=/_N_WKS_DIR/_N_AFO160_WPD
blah blah blah
You are generating the output files, but not where you think. As you are not indicating where to generate the files, they are generated in the "current directory" for the started script process. And this "current directory" is not always the same:
If you double click over a .vbs file, the current directory for the operation is the folder in where the script is located.
If you drag and drop a file over a .vbs file, the current directory is the current active directory of the explorer instance that is handling the drop operation. Note that this directory is not what the explorer window shows, but the active directory of the explorer.exe process (usually %systemroot%\system32, but can change)
You have to determine where you want your files and explicitly indicate it on file creation.
So, if you need the files created in the .vbs folder use
writeTo = fso.BuildPath( _
fso.GetFile( WScript.ScriptFullName ).ParentFolder.Path _
, saveTo & (line1 & ".arc") _
)
If you need the files created in the same folder where the input file is, use
writeTo = fso.BuildPath( _
fso.GetFile( textFile ).ParentFolder.Path _
, saveTo & (line1 & ".arc") _
)
Or explicitly indicate where to create the files.

VB script run time error input past end of file

I'm taking a scripting class and im having some issues with my script. According to the lab assignment all my syntax is correct. However i keep getting a input past end of file error on line 60,1. I've starred at the program forever and checked all lines letter for letter for quite some time with no luck. Any help would be greatly appreciated. Here is the script.
dim ipAddress(5,3)
ipAddress(0,0)="192.168.10.11"
ipAddress(0,1)="192.168.10.12"
ipAddress(0,2)="192.168.10.13"
ipAddress(0,3)="192.168.10.14"
ipAddress(1,0)="192.168.10.19"
ipAddress(1,1)="192.168.10.20"
ipAddress(1,2)="192.168.10.21"
ipAddress(1,3)="192.168.10.22"
ipAddress(2,0)="192.168.10.27"
ipAddress(2,1)="192.168.10.28"
ipAddress(2,2)="192.168.10.29"
ipAddress(2,3)="192.168.10.30"
ipAddress(3,0)="192.168.10.35"
ipAddress(3,1)="192.168.10.36"
ipAddress(3,2)="192.168.10.37"
ipAddress(3,3)="192.168.10.38"
ipAddress(4,0)="192.168.10.43"
ipAddress(4,1)="192.168.10.44"
ipAddress(4,2)="192.168.10.45"
ipAddress(4,3)="192.168.10.46"
ipAddress(5,0)="192.168.10.51"
ipAddress(5,1)="192.168.10.52"
ipAddress(5,2)="192.168.10.53"
ipAddress(5,3)="192.168.10.54"
const READ = 1
const WRITE = 2
const APPEND = 8
const ASCII = 0
dim fileName
fileName = "IP_Addresses.csv"
dim ipAddrStr
ipAddrStr = ""
dim fso
Set fso = Wscript.CreateObject("Scripting.FileSystemObject")
Set ipFileObj = fso.CreateTextFile(fileName,True,ASCII)
For room = 0 to 5
For computer = 0 to 3
ipAddrSr = CStr(room+100) & "," & CStr(computer+1) & "," ipAddress(room,computer)
& vbCrlf
ipFileObj.write(ipAddrStr)
Next
Next
ipFileObj.close
Set ipFileObj = fso.OpenTextFile(fileName,READ,ASCII)
WScript.Echo ipFileObj.ReadAll **' this is line 60**
ipFileObj.Close
As you don't use "Option Explicit", you get what you deserve: You (try to) concatenate the lines into ipAddrSr but write ipAddrStr to the file. So nothing gets written to the file.
Fix the syntax error and the bad name to:
ipAddrStr = CStr(room+100) & "," & CStr(computer+1) & "," & ipAddress(room,computer) & vbCrlf
Assuming that the file isn't empty, perhaps you need to specify the directory the file is in? I think this can be done either in your script:
fileName = "c:\your_directory\IP_Addresses.csv"
Or if you run it in the command line via cscript...
cscript.exe your.vbs "c:\your_directory\IP_Addresses.csv"
You can check the file size before executing your Echo if you like...
if fileName.size > 0 then
Set ipFileObj = fso.OpenTextFile(fileName,READ,ASCII)
WScript.Echo ipFileObj.ReadAll **' this is line 60**
ipFileObj.Close
else
WScript.Echo = "File was empty"
end if
See details of passing an argument to your script here.

Remove parts of a string and copy the rest back to a file with vbscript

I would like to remove the unwanted text from each string in a file.
the input string looks like this
username^time stamp^don't need this printed on printer name more useless info pages printed:some number
I want to remove everything else but keep the username,time stamp,printer name and some number.Then write each line to a file so the output looks like this
username timestamp printername some number
This is the code I'm working with
Set fs = CreateObject("Scripting.FileSystemObject")
sf = "C:\test.txt"
Set f = fs.OpenTextFile(sf, 1) ''1=for reading
s = f.ReadAll
segments = Split(s,"^",-1)
s= segments(1,)
f.Close
Set f = fs.OpenTextFile(sf, 2) ''2=ForWriting
f.Write s
f.Close
There's always a moment that somebody asks "Why not use a regular expression?". This is that moment.
Try this:
Dim re, s, match, matches
s = "Chuck Norris^12-12-2012^don't need this printed on HAL9000 more useless info pages printed:42 "
Set re = new regexp
re.pattern = "(.*)\^(.*)\^.*printed on (\w+).*pages printed:(\d+).*"
re.Global = True
Set matches = re.Execute(s)
Set match = matches(0)
msgbox "username=" & match.submatches(0)
msgbox "time stamp=" & match.submatches(1)
msgbox "printer=" & match.submatches(2)
msgbox "pages printed=" & match.submatches(3)
Neat huh? And I bet you'll figure out how to implement it in your existing code.
Code:
Const csSep = "^"
'username^time^(other arbitrary junk)^printer name^(other arbitrary junk)^page count
Dim sJunk : sJunk = "kurt^01:02:03^some junk^nec p7^nix^123"
WScript.Echo sJunk
Dim aParts : aParts = Split(sJunk, csSep)
Dim sNetto : sNetto = Join(Array(aParts(0),aParts(1),aParts(3),aParts(5)), csSep)
WScript.Echo sNetto
output:
kurt^01:02:03^some junk^nec p7^nix^123
kurt^01:02:03^nec p7^123

Resources