Search code examples
windowsvbavbscriptwindows-scripting

VbScript to trim trailing pipes off file at 35 pipes


So I was initially writing this in batch, but the number of tokens usable with ASCII were too small, i guess it only allows 26, and I need 35 pipes to remain in my output file.

I am new to VBScript, but basically I want it to read in the original file, do some magic to select the first character in the file to the last 35 pipes (and everything between, even in the space between the two pipes is blank). Then output that file to another file, while retaining the integrity of original.

Here is my code so far:

' **************
' ** Anthony B.
' **************
' ** PipeDropper
' **************
Dim WshShell, oExec
Set wshShell = CreateObject("WScript.Shell")
Set objFSO = CreateObject("Scripting.FileSystemObject")

Set objInputFile = objFSO.OpenTextFile("C:\Users\aborgetti\Desktop\Pipe Delimiter Project\oauthrn.cms",1)
Set objOutputFile = objFSO.OpenTextFile("C:\Users\aborgetti\Desktop\Pipe Delimiter Project\output.txt",8,True)

Do until objInputFile.AtEndofStream
    strcomputer = objInputFile.ReadLine
    strCommand = "dsquery computer -name " & strComputer
    Set oExec = WShShell.Exec(strCommand)
    If (oExec.Status = 0) Then
        If (oExec.stdOut.ReadAll = "") Then
            objOutputFile.WriteLine(strComputer)
        End If
    End If
Loop

objInputFile.Close
objOutputFile.Close

Issues

This line Set oExec = WShShell.Exec(strCommand) says it can't find the file that's specified...so I'm not sure why that's bad.

And then where do I go from here?

Thanks in advance!

UPDATE

Here is a line that would be in the file, usually anywhere from 10-100 times...

RE|922124607|1 |KimV|HOS99999|Y|N|2014-04-02 15:49:14|2014-04-02 15:49:58|Y|2014-04-02 00:00:00|R9815|01|1 |2014-04-02 00:00:00|493.90||||2016-04-02|N||HOS99999|||06|PROV99999|2014-04-02 15:48:20|2014-04-02||R9815|2014-04-02 00:00:00|2016-04-02 00:00:00||||||98960|06 |08|6|6|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||02|||||||Z4|2014-04-02 15:49:58|04|001|01|***|PMER|***|***|2013-08-01||||||||||||||||||||||||||||||||||||||||

Everything after the last 01 needs to go...so:

|***|PMER|***|***|2013-08-01||||||||||||||||||||||||||||||||||||||||

Should be:

Start of line ...  |***|PMER|***|***|2013-08-01 

Solution

  • I doubt that your problem

    1. has anything to do with pipes
    2. the code you published is the code that caused the error

    There are two ways to get an error 424 "Object required" for a statement like

    Set a = b.c(d)
    

    The first (and obvious): b isn't an object. The second: the return value of b.c(d) isn't an object.

    Your code condensed:

    Set wshShell = CreateObject("WScript.Shell")
    strCommand = "hostname"
    Set oExec = WShShell.Exec(strCommand)
    WScript.Echo oExec.Stdout.Readline()
    

    runs without a problem. A slight change:

    Set wshShell = CreateObject("WScript.Shell")
    strCommand = "hostname"
    Set oExec = WShShel1.Exec(strCommand)
    WScript.Echo oExec.Stdout.Readline()
    

    fails with "Object required". Do you spot the typo? Why not let "Option Explicit" help you:

    Option Explicit
    Dim wshShell   : Set wshShell = CreateObject("WScript.Shell")
    Dim strCommand : strCommand = "hostname"
    Dim oExec      : Set oExec = WShShel1.Exec(strCommand)
    WScript.Echo oExec.Stdout.Readline()
    

    output:

    ...\22871772.vbs(4, 18) Microsoft VBScript runtime error: Variable is undefined: 'WShShel1'
    

    If you made sure that no typo caused the problem, you'll have to check whether the variable is changed/assigned to between initialization and use:

    Option Explicit
    Dim wshShell   : Set wshShell = CreateObject("WScript.Shell")
    Dim strCommand : strCommand = "hostname"
    '...
    wShSheLL = "oops"
    '...
    Dim oExec      : Set oExec = WShShell.Exec(strCommand)
    WScript.Echo oExec.Stdout.Readline()
    

    again fails with error 424.

    Update wrt the pipes:

    I'd choose Split() (with count set to #pipes you want + 1) to deal with the trailing pipes:

    >> n = 8 + 1
    >> s = "|abc123|1*|004|**gobbligook|001|%|2014-01-01|||||||||||||"
    >> a = Split(s, "|", n)  ' <-- 9th elm get all the junk
    >> WScript.Echo UBound(a)
    >> a(n - 1) = "" ' <-- zap the junk
    >> WScript.Echo Join(a, "|")
    >>
    8
    |abc123|1*|004|**gobbligook|001|%|2014-01-01|
    

    To delete a tail of |'s, a RegExp seems more appropriate:

    >> set r = New RegExp
    >> r.Pattern = "\|+$"
    >> WScript.Echo r.Replace(s, "")
    >>
    |abc123|1*|004|**gobbligook|001|%|2014-01-01