+ Post New Thread
Page 1 of 2 12 LastLast
Results 1 to 15 of 23
Scripts Thread, vbs file manipulation in Coding and Web Development; in the past ive just nicked bits of code from various sources to do what im trying to do but ...
  1. #1


    Join Date
    Mar 2009
    Location
    Leeds
    Posts
    6,591
    Thank Post
    228
    Thanked 856 Times in 735 Posts
    Rep Power
    296

    vbs file manipulation

    in the past ive just nicked bits of code from various sources to do what im trying to do but my code has just become a nightmare. Im now trying to redo it and do the coding myself so i can document it properly and understand what its doing. To this end ive been doing it in bits but now am a bit stuck as to how to bring bits back together. Atm what its supposed to do is read in data from a text file strip out data i want to keep junk the rest the add the new version of the data back and rewrite to a file so if files contained say

    pc01,hp compaq 610,4gb,120gb,80gb,serialno,12-12-2010,3,12-12-2013

    to start with it should end up with

    pc01,hp compaq 610,8gb,120gb,80gb,serialno,12-12-2010,3,12-12-2013

    the last 3 commas stay the rest is replaced.

    Now i can manage the replace bit from a variable code below
    Code:
    testinput="pc01,hp compaq 610,4gb,120gb,80gb,serialno,12-12-2010,3,12-12-2013"
    wscript.echo testinput
    warranry="01-01-1910,0,01-01-1910"
    myArray = Split(testinput,",")
    enddata= myArray(0) & "," & myArray(1) &"," & myArray(2) & "," & myArray(3) &"," & myArray(4) & "," & myArray(5) &"," & warranry
    'wscript.echo myArray(0)
    'wscript.echo myArray(1)
    'wscript.echo myArray(2)
    'wscript.echo myArray(3)
    'wscript.echo myArray(4)
    'wscript.echo myArray(5)
    'wscript.echo myArray(6)
    'wscript.echo myArray(7)
    'wscript.echo myArray(8)
    
    'wscript.echo myArray(0) & "," & myArray(1) &"," & myArray(2) & "," & myArray(3) &"," & myArray(4) & "," & myArray(5) &"," & myArray(6) & "," & myArray(7) &"," & myArray(8)
    
    wscript.echo enddata
    and have managed to do replace on txt files (though i suspect this isnt the right answer) code below

    Code:
    Const ForReading = 1
    Const ForWriting = 2
    dim existing,newdata
    
    existing= "Jim,"
    newdata="bob,"
    
    Set objFSO = CreateObject("Scripting.FileSystemObject")
    Set objFile = objFSO.OpenTextFile("C:\Users\me\Desktop\Text.txt", ForReading)
    
    strText = objFile.ReadAll
    objFile.Close
    'wscript.echo strtext
    strNewText = Replace(strText, existing, newdata)
    
    Set objFile = objFSO.OpenTextFile("C:\Users\me\Desktop\Text.txt", ForWriting)
    objFile.WriteLine strNewText
    objFile.Close
    i appear to of just hit a brick wall in getting it to search for something in a text file and just replace what i want rather than globally replacing say jim with bob (and that appears to be case sensitive which ideally i could live without)

    i intend to make the mac address or serial no the first column in the file later as that should always be unique to a specific pc its possible pc1 may change over time.

    i suspect the problem is im just looking at it wrong

    im assuming i need to somehow read each line of the text file into a variable check the first bit it against another variable then if it matches read the whole line into an array rebuild a variable with the wanted contents of the array the bits i want to replace and then dump it back out to the text file

  2. #2

    Steve21's Avatar
    Join Date
    Feb 2011
    Location
    Swindon
    Posts
    2,696
    Thank Post
    335
    Thanked 515 Times in 483 Posts
    Rep Power
    179
    Bit confused on what you actually want

    i appear to of just hit a brick wall in getting it to search for something in a text file and just replace what i want rather than globally replacing say jim with bob (and that appears to be case sensitive which ideally i could live without)
    So you only want to replace one line? Not all lines? Only ones that match case?

    Steve

  3. #3


    Join Date
    Mar 2009
    Location
    Leeds
    Posts
    6,591
    Thank Post
    228
    Thanked 856 Times in 735 Posts
    Rep Power
    296
    Quote Originally Posted by Steve21 View Post
    Bit confused on what you actually want



    So you only want to replace one line? Not all lines? Only ones that match case?

    Steve
    sorry its probably not that clear
    if the file contains say


    pc01,hp compaq 610,4gb,120gb,80gb,serialno,12-12-2010,3,12-12-2013
    pc02,hp compaq 610,4gb,120gb,80gb,serialno,12-12-2010,3,12-12-2013
    pc03,hp compaq 610,4gb,120gb,80gb,serialno,12-12-2010,3,12-12-2013
    pc04,hp compaq 610,4gb,120gb,80gb,serialno,12-12-2010,3,12-12-2013
    pc05,hp compaq 610,4gb,120gb,80gb,serialno,12-12-2010,3,12-12-2013

    i want it to look for a specific line/pc say pc01 throw away the data before 12-12-2010 re add altered data (another part of the script not shown but hats working fine) and 12-12-2010,3,12-12-2013 to the line where it came from update and close the file so the end result should be something like (exaggerated for effect)

    pc01,hp compaq 610,800gb,1500gb,1300gb,serialno,12-12-2010,3,12-12-2013
    pc02,hp compaq 610,4gb,120gb,80gb,serialno,12-12-2010,3,12-12-2013
    pc03,hp compaq 610,4gb,120gb,80gb,serialno,12-12-2010,3,12-12-2013
    pc04,hp compaq 610,4gb,120gb,80gb,serialno,12-12-2010,3,12-12-2013
    pc05,hp compaq 610,4gb,120gb,80gb,serialno,12-12-2010,3,12-12-2013

  4. #4
    hallb15's Avatar
    Join Date
    Jan 2012
    Location
    Leeds
    Posts
    405
    Thank Post
    462
    Thanked 231 Times in 149 Posts
    Rep Power
    74
    Why?
    What's the project actually for?
    I only ask because I once got stuck on a similar scripting task which became so over-complicated that no-one could understand it. After taking a step back I realised I could achieve what I was trying to do with a simple spreadsheet that took 10 mins to create and 10 seconds to update once in a while.

  5. #5


    Join Date
    Mar 2009
    Location
    Leeds
    Posts
    6,591
    Thank Post
    228
    Thanked 856 Times in 735 Posts
    Rep Power
    296
    the idea is a script pulls data about a pc every time it boots up so thats the
    pc01,hp compaq 610,4gb,120gb,80gb,serialno,
    bit (thats easy and works fine) then i would like to add warranty info at the end (which obviously will need to be manually added and i dont want it wiped out every time the script is run hence junk the first bit keep the last then rewrite). Ive got an old script that does a lot of this but it cant do the warranty bit and as said the codes been added to and bodged over the years so much its hard to see what does what so ive decided to start from scratch (it was also designed for 1 file per pc but thats not ideal as with 1 file total i can give it a web interface to quickly look at specific data which means office staff can say quickly look at pc age/warranty status and i can quickly look to see if any pcs say have naff all hdd space free or naff all ram etc

    idwally it needs to be automated as much as possible as im only in most schools once a week and im terrible at remembering to update this stuff

  6. #6

    Steve21's Avatar
    Join Date
    Feb 2011
    Location
    Swindon
    Posts
    2,696
    Thank Post
    335
    Thanked 515 Times in 483 Posts
    Rep Power
    179
    Like this?

    Original:

    Code:
    pc01,hp compaq 610,4gb,120gb,80gb,serialno,12-12-2010,3,12-12-2013
    pc02,hp compaq 610,4gb,120gb,80gb,serialno,12-12-2010,3,12-12-2013
    pc03,hp compaq 610,4gb,120gb,80gb,serialno,12-12-2010,3,12-12-2013
    pc04,hp compaq 610,4gb,120gb,80gb,serialno,12-12-2010,3,12-12-2013
    pc05,hp compaq 610,4gb,120gb,80gb,serialno,12-12-2010,3,12-12-2013
    pc01,hp compaq 610,4gb,120gb,80gb,serialno,12-12-2010,3,12-12-2013
    Final:

    Code:
    pc01,hp compaq 610,800gb,1500gb,1300gb,serialno,12-12-2010,3,12-12-2013
    pc02,hp compaq 610,4gb,120gb,80gb,serialno,12-12-2010,3,12-12-2013
    pc03,hp compaq 610,4gb,120gb,80gb,serialno,12-12-2010,3,12-12-2013
    pc04,hp compaq 610,4gb,120gb,80gb,serialno,12-12-2010,3,12-12-2013
    pc05,hp compaq 610,4gb,120gb,80gb,serialno,12-12-2010,3,12-12-2013
    pc01,hp compaq 610,800gb,1500gb,1300gb,serialno,12-12-2010,3,12-12-2013
    Put two in as a test.

    Code:
    dim existing,newdata
    dim outputArray, message
    
    FindString="pc01"
    NewData="pc01,hp compaq 610,800gb,1500gb,1300gb,serialno,"
    
    Set objFS = CreateObject("Scripting.FileSystemObject")
    Set objTS = objFS.OpenTextFile("C:\Test\stedtest.txt", 1)
    strContents = objTS.ReadAll
    objTS.Close
    
    arrLines = Split(strContents, vbNewLine)
    Set objTS = objFS.OpenTextFile("C:\Test\stedtest.txt", 2)
    
    For i=0 To UBound(arrLines)
       If instr(arrLines(i),findstring) Then
    	outputArray = split(arrLines(i),",")
    	objTS.writeline NewData & outputArray(6) & "," & outputArray(7) & "," & outputArray(8)
       else
    	objTS.WriteLine arrLines(i)
       End If
    Next
    Obviously your NewData will be whatever you want to put into it, but it just reads the file line by line, splits the data into an array then uses the bits you want to keep.

    Steve
    Last edited by Steve21; 5th March 2014 at 11:17 AM.

  7. Thanks to Steve21 from:

    sted (6th March 2014)

  8. #7


    Join Date
    Mar 2009
    Location
    Leeds
    Posts
    6,591
    Thank Post
    228
    Thanked 856 Times in 735 Posts
    Rep Power
    296
    Code:
    dim existing,newdata
    dim outputArray, message
    
    FindString="pc01"
    NewData="pc01,hp compaq 610,800gb,1500gb,1300gb,serialno,"
    Code:
    Set objFS = CreateObject("Scripting.FileSystemObject")
    Set objTS = objFS.OpenTextFile("C:\Users\me\Desktop\test.txt", 1)
    strContents = objTS.ReadAll
    objTS.Close
    opens text file for reading and places entire contents in strcontents then closes file?
    Code:
    arrLines = Split(strContents, vbNewLine)
    Set objTS = objFS.OpenTextFile("C:\Users\me\Desktop\test.txt", 2)
    open file for writing
    Code:
    For i=0 To UBound(arrLines)
       If instr(arrLines(i),findstring) Then
    	outputArray = split(arrLines(i),",")
    	objTS.writeline NewData & outputArray(6) & "," & outputArray(7) & "," & outputArray(8)
       else
    	objTS.WriteLine arrLines(i)
       End If
    Next
    presumably loop until out of data
    outpuit array spkit into line i
    dump to text file?

    fails on line 18 character 2 subscript out of range: '[number 7]' and wipes the text file of all data
    edit sorry that was me and a cruddy text file without right number of end bits lol
    Last edited by sted; 6th March 2014 at 08:38 AM.

  9. #8


    Join Date
    Mar 2009
    Location
    Leeds
    Posts
    6,591
    Thank Post
    228
    Thanked 856 Times in 735 Posts
    Rep Power
    296
    right been having a play it will now create a file if one dosent exist and pupulate it with header rows. What im now tring to do is if it cant find say pc01 then write data to the file. I tried changing

    else
    objTS.WriteLine arrLines(i)
    End If

    to objTS.WriteLine "boo" as a test and ended up with it wiping out the existing file contents and the file contained

    boo
    boo
    boo
    boo

    current code

    Code:
    'on error resume next
    
    dim existing,newdata
    dim outputArray, message
    dim filenamepath
    
    FindString="pc09"
    NewData="pc01,hp compaq 610,800gb,1500gb,1300gb,serialno,"
    filenamepath="c:\Users\datacable\Desktop\test.txt"
    headers = "name,make model,ram size,hdd size,hdd free,serialno,next,more next,yet more next"
    
    Set objFS = CreateObject("Scripting.FileSystemObject")
    
    If objFS.FileExists(filenamepath) Then
    	wscript.echo "file exists"
    else
    	wscript.echo "no file here"
    	Set objFS = objFS.CreateTextFile(filenamepath)
    	objFS.close
    	Set objFS = CreateObject("Scripting.FileSystemObject")
    	Set objFS = objFS.OpenTextFile(filenamepath, 2)
    	objFS.writeline headers
    	objFS.close
    end if
    
    Set objFS = CreateObject("Scripting.FileSystemObject")
    
    Set objTS = objFS.OpenTextFile(filenamepath, 1)
    strContents = objTS.ReadAll
    objTS.Close
    
    arrLines = Split(strContents, vbNewLine)
    Set objTS = objFS.OpenTextFile(filenamepath, 2)
    
    For i=0 To UBound(arrLines)
       If instr(arrLines(i),findstring) Then
    	outputArray = split(arrLines(i),",")
    	objTS.writeline NewData & outputArray(6) & "," & outputArray(7) & "," & outputArray(8)
       else
    	objTS.WriteLine arrLines(i)
       End If
    Next

  10. #9

    Steve21's Avatar
    Join Date
    Feb 2011
    Location
    Swindon
    Posts
    2,696
    Thank Post
    335
    Thanked 515 Times in 483 Posts
    Rep Power
    179
    So basically if the pc it's trying to write data for doesn't exist then add the line?

    Steve

  11. Thanks to Steve21 from:

    sted (7th March 2014)

  12. #10

    Steve21's Avatar
    Join Date
    Feb 2011
    Location
    Swindon
    Posts
    2,696
    Thank Post
    335
    Thanked 515 Times in 483 Posts
    Rep Power
    179
    Basically you can't change my loopy bit as that's searching "per line" in the array, so if you're outputting Boo it'll do it for every line.

    You need to add a check and then if it's not found by the end it'll add it on.

    Going back to my original code example:

    Code:
    dim existing,newdata
    dim outputArray, message
    dim Found
    
    Found="0"
    
    FindString="pc09"
    NewData="pc01,hp compaq 610,800gb,1500gb,1300gb,serialno,"
    
    Set objFS = CreateObject("Scripting.FileSystemObject")
    Set objTS = objFS.OpenTextFile("C:\Test\stedtest.txt", 1)
    strContents = objTS.ReadAll
    objTS.Close
    
    arrLines = Split(strContents, vbNewLine)
    Set objTS = objFS.OpenTextFile("C:\Test\stedtest.txt", 2)
    
    For i=0 To UBound(arrLines)
       If instr(arrLines(i),findstring) Then
    	Found="1"
    	outputArray = split(arrLines(i),",")
    	objTS.writeline NewData & outputArray(6) & "," & outputArray(7) & "," & outputArray(8)
       else
    	objTS.WriteLine arrLines(i)
       End If
    
    Next
    
    If Found="0" then
    objTS.WriteLine "NewStuff"
    end if

  13. Thanks to Steve21 from:

    sted (7th March 2014)

  14. #11


    Join Date
    Mar 2009
    Location
    Leeds
    Posts
    6,591
    Thank Post
    228
    Thanked 856 Times in 735 Posts
    Rep Power
    296
    Quote Originally Posted by Steve21 View Post
    So basically if the pc it's trying to write data for doesn't exist then add the line?

    Steve
    thats the one cheers. You can tell ive learnt to code vbs by nicking it from websites cant you lol. One day i will sit down and learn it properly or at this point might take the plunge and go straight to powershell. If only there were more hours in the day that i get paid to do this lol

  15. #12

    Steve21's Avatar
    Join Date
    Feb 2011
    Location
    Swindon
    Posts
    2,696
    Thank Post
    335
    Thanked 515 Times in 483 Posts
    Rep Power
    179
    Tbh that's the lazy way of doing it :P But it works well enough for what you're doing.

    Steve

  16. #13


    Join Date
    Mar 2009
    Location
    Leeds
    Posts
    6,591
    Thank Post
    228
    Thanked 856 Times in 735 Posts
    Rep Power
    296
    Quote Originally Posted by Steve21 View Post
    Tbh that's the lazy way of doing it :P But it works well enough for what you're doing.

    Steve
    dosent need to be perfect just needs to work lol

  17. #14


    Join Date
    Mar 2009
    Location
    Leeds
    Posts
    6,591
    Thank Post
    228
    Thanked 856 Times in 735 Posts
    Rep Power
    296
    thanks works but its adding a blank line after newstuff

  18. #15

    Steve21's Avatar
    Join Date
    Feb 2011
    Location
    Swindon
    Posts
    2,696
    Thank Post
    335
    Thanked 515 Times in 483 Posts
    Rep Power
    179
    Use Write for writing, or writeline for write+newline

    e.g. objTS.Write "NewStuff"

    Steve

  19. Thanks to Steve21 from:

    sted (7th March 2014)

SHARE:
+ Post New Thread
Page 1 of 2 12 LastLast

Similar Threads

  1. Visio Shortcut Opens Multiple VBS Files?
    By DaveP in forum How do you do....it?
    Replies: 0
    Last Post: 14th July 2010, 12:48 PM
  2. Preventing Bat and vbs files
    By BSOD in forum Windows Server 2000/2003
    Replies: 7
    Last Post: 28th January 2010, 06:18 PM
  3. Encrypting VBS files
    By Newton in forum Scripts
    Replies: 2
    Last Post: 28th April 2009, 02:37 PM
  4. Kids Running VBS files
    By tomscaper in forum Scripts
    Replies: 8
    Last Post: 9th January 2008, 12:36 PM
  5. VBS File takes long time to load 1st time?
    By Samson in forum Scripts
    Replies: 3
    Last Post: 4th September 2007, 07:40 AM

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •