Solved

Read a Text File and build array

Posted on 2004-08-24
11
220 Views
Last Modified: 2013-11-18
Hi Guys,

I need some serious help in getting a VB script together that can:

Part One
open a text file (sites.txt) then read the file line by line excluding the 1st and last line. Then I want to store the values between the quotation marks in an array.

Sites.txt
-------------------------------------------------------------------------------
<Sites Count="10">
  <URL="http://t8.abcd.com" Owner="EXT\ellynaj.ba" />
  <URL="http://t8.abcd.com/carrierevaluk" Owner="DM1\lishmj" SecondaryOwner="DM1\ennisc" />
  <URL="http://t8.abcd.com/engineering" Owner="DM1\ lishmj.dv" />
  <URL="http://t8.abcd.com/goclaims" Owner="DM1\treadawaym" SecondaryOwner="DM1\llewellynaj" />
  <URL="http://t8.abcd.com/groupaudit" Owner="DM1\studc" SecondaryOwner=" DM1\ ellynaj " />
  <URL="http://t8.abcd.com/groupauditsarbanes" Owner="DM1\ studc " SecondaryOwner="DM1\llewellynaj" />
  <URL="http://t8.abcd.com/gsvhelp" Owner="DM1\ ellynaj" SecondaryOwner="DM1\glenn_da" />
  <URL="http://t8.abcd.com/iod" Owner="DM1\studdsc" SecondaryOwner="DM1\funks" />
  <URL="http://t8.abcd.com/is_gov" Owner="DM1\studc" SecondaryOwner="DM1\bosspr" />
  <URL="http://t8.abcd.com/wol" Owner="DM1\studc" SecondaryOwner="DM1\bosspr" />
</Sites>
-------------------------------------------------------------------------------

Part Two
From the script run a backup program + site name & specify the backup file name using each of the values/ sites stored in the array. The file name should not contain the http:// part of value in the array. I would also like to include a date at the end of the filename. As an example the script should run the following 10 times as there are 10 sites listed in the file.

Backup.exe –s http://t8.abcd.com –f Daily_backup_t8.abcd.com(mmddyyyy).dat
Backup.exe –s http://t8.abcd.com/carrierevaluk –f Daily_backup_t8.abcd.com/carrierevaluk (mmddyyyy).dat

etc. etc

Hopefully this is possible to do via VBscript.

Thanks very much, I appreciate everyone’s suggestions and feedback
0
Comment
Question by:Herschel_Solomon
  • 5
  • 5
11 Comments
 
LVL 8

Expert Comment

by:mladenovicz
ID: 11880052
Part One (maybe you will need some additional checks, but this is just rough exampe)

Dim oFSO
Dim oTextStream
Dim strLine
Dim i
Dim arrURLs()
Dim arrOwners()
Dim arrSecOwners()
Dim sTmp
Dim arrTmp
Dim Pos1, Pos2
Dim Counter

    Set oFSO = CreateObject("Scripting.FileSystemObject")
    Set oTextStream = oFSO.OpenTextFile("C:\test.txt", _
                                           1, _
                                           True, _
                                           0)
   
    While Not oTextStream.AtEndOfStream
        strLine = oTextStream.ReadLine
        If Left(strLine, 6) <> "<Sites" And Left(strLine, 7) <> "</Sites" Then
           
            strLine = Trim(strLine)
            strLine = Replace(strLine, "<", "")
            strLine = Replace(strLine, " />", "")
            Debug.Print strLine
           
            Counter = Counter + 1
           
            Pos1 = InStr(strLine, """")
            Pos2 = InStr(Pos1 + 1, strLine, """")
           
            ReDim Preserve arrURLs(Counter)
            arrURLs(Counter) = Mid(strLine, Pos1 + 1, Pos2 - Pos1 - 1)
           
            Pos1 = InStr(Pos2 + 1, strLine, """")
            Pos2 = InStr(Pos1 + 1, strLine, """")
           
            ReDim Preserve arrOwners(Counter)
            arrOwners(Counter) = Mid(strLine, Pos1 + 1, Pos2 - Pos1 - 1)
           
            Pos1 = InStr(Pos2 + 1, strLine, """")
            Pos2 = InStr(Pos1 + 1, strLine, """")
           
            ReDim Preserve arrSecOwners(Counter)
            If Pos1 > 0 And Pos2 > 0 Then
                arrSecOwners(Counter) = Mid(strLine, Pos1 + 1, Pos2 - Pos1 - 1)
            Else
                arrSecOwners(Counter) = ""
            End If
           
           
        End If
    Wend
   
    oTextStream.Close
   
    Set oTextStream = Nothing
    Set oFSO = Nothing
0
 
LVL 15

Expert Comment

by:Colosseo
ID: 11880081
Hi there

I know you mentioned an array but i didnt see the need for it as rather than populate an array the code just runs the backups as it goes

You will have to specify the location of your sites.txt and backup.exe

also we might need to tweak the str_To_Run variable in the run_Backup sub. But give it a go and let me know

Dim obj_FSO
Dim obj_Shell

Dim str_Site_File
Dim str_Backup_EXE

Set obj_FSO = CreateObject("Scripting.FileSystemObject")
Set obj_Shell = CreateObject("WScript.Shell")

str_Site_File = "c:\sites.txt"
str_Backup_EXE = "c:\backup.exe"


' If the sites file exists
If obj_FSO.FileExists(str_Site_File) Then
  ' read the sites file and run the backups
  read_Site_File
End If

' read each line of the file extracting the correct information
Sub read_Site_File()

  Dim obj_Site_File
  Dim str_Current_Line

  ' Open the sites file
  Set obj_Site_File = obj_FSO.OpenTextFile(str_Site_File)

  ' While not at the end of the file
  While Not obj_Site_File.AtEndOfStream
    ' Read the current line
    str_Current_Line = obj_Site_File.ReadLine
    ' if this line containts URL= then extract the url
    If inStr(str_Current_Line,"URL=") > 0 Then
      str_Current_Line = mid(str_Current_Line,inStr(str_Current_Line,"""")+1)
      str_Current_Line = Left(str_Current_Line,inStr(str_Current_Line,"""")-1)

      ' run the backup.exe with the correct parameters
      run_Backup str_Current_Line
     
    End If

  WEnd

  ' Close the sites file and tidyup
  obj_Site_File.close
  Set obj_Site_File = Nothing

End Sub

' Build the exe string and run
Sub run_Backup(str_URL)

  Dim str_To_Run
 
  ' build the string to run
  str_To_Run = str_Backup_EXE & " –s " & str_URL & " -f Daily_backup_" & Mid(str_URL,inStr(str_URL,"http://") + 7) & "(" & replace(date,"/","") & ").dat"

  ' run the backup
  obj_Shell.Run str_To_Run
 
End Sub

' tidy up
Set obj_Shell = Nothing
Set obj_FSO = Nothing

Regards

Scott
0
 

Author Comment

by:Herschel_Solomon
ID: 11882086
Hi mladenovicz,

Thanks for your code in solving Part one, I only needed the arrURLs array. Sorry I should have been clearer in my post. I played around with it and it seems to work quite well.
0
 

Author Comment

by:Herschel_Solomon
ID: 11882188
Hi Scott,

I tried your code as well, however the backup command does not run. I made sure that the paths are being specified correctly. I was wondering if there is perhaps a way that I can echo the str_To_Run string to the screen or to a text file, so that I can see what the completed command would look like. Also for testing I might even be able to copy the completed command/ string to the command prompt to test that we are generating the string correctly.

Thanks for you help thus far.

Herschel
0
 
LVL 15

Expert Comment

by:Colosseo
ID: 11882251
Hi

Yeah im sure its just a problem with the run line and the paramenters in the exe command

If you change the rubBackup sub to this

' Build the exe string and run
Sub run_Backup(str_URL)

  Dim str_To_Run
 
  ' build the string to run
  str_To_Run = str_Backup_EXE & " –s " & str_URL & " -f Daily_backup_" & Mid(str_URL,inStr(str_URL,"http://") + 7) & "(" & replace(date,"/","") & ").dat"

  msgbox str_To_Run

  ' run the backup
  'obj_Shell.Run str_To_Run
 
End Sub

That will show the command that has been build and stop it running.

What is backup.exe if i can get the exe then i can download the exe then i can test the code

Cheers

Scott
0
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 

Author Comment

by:Herschel_Solomon
ID: 11885276
Hi Scott

The backup.exe is sts-adm it’s from the Share Point Team services resource kit. You will need to run sts-adm against a share point site. I recommend using ping as a substitute. So sites.txt now contains the following
Sites.txt
-------------------------------------------------------------------------------
<Sites Count="3">
  <URL="http://www.yahoo.com" Owner="DM1\lishmj" SecondaryOwner="DM1\ennisc" />
  <URL="http://www.google.co.uk " Owner="DM1\ lishmj.dv" />
  <URL="http://www.altavista.com" Owner="DM1\ lishmj.dv" />
</Sites>
-------------------------------------------------------------------------------
Run ping against only the www portion of the lines contained in sites.txt.
Create and name the output/results as PingResults_ www.yahoo.com(mmddyyyy).txt  etc. etc.

I hope this will help the testing,

Thanks

Herschel
0
 
LVL 15

Expert Comment

by:Colosseo
ID: 11887954
Hi Hershel

I managed to get the code running using ping so i have hopefully made the same changes to the backup line

running the following line should ping each url in turn and output to a txt file on the c:

str_To_Run = "cmd /c ""ping"" " & str_URL & " > c:\Daily_backup_" & Mid(str_URL,inStr(str_URL,"http://") + 7) & "(" & replace(date,"/","") & ").txt"

hopefully :) this like will run backup.exe and put the dat file in the root of c: ... you can of course change this is/when we get it working!

str_To_Run = "cmd /c """ & str_Backup_EXE & """ –s " & str_URL & " -f c:\Daily_backup_" & Mid(str_URL,inStr(str_URL,"http://") + 7) & "(" & replace(date,"/","") & ").dat"

Give it a shot and let me know

Scott
0
 

Author Comment

by:Herschel_Solomon
ID: 11891276
Hi Scott

I got the script working, however I still need two things sorted.

I need to run the backup command one at a time(right now it opens command prompt windows for every site/ url). This will become an issue later as the PC would not be able to cope with the throughput required.

Also the str_To_Run needs one small tweak, I need the "/" removed when it's backing up a URL like "http://t8.abcd.com/carrierevaluk", right now it attempts to name the file including the "/". (Daily_backup_t8.abcd.com/carrierevaluk(mmddyyyy).dat) We can either replace the "/" with a "-" or remove it all together as it's just the name of the backup file.

Other than that it's running very well.

I will allocate the points after these last two fixes. I would also like to thank you for your work thus far.

Regards

Herschel
0
 
LVL 15

Expert Comment

by:Colosseo
ID: 11891371
Hi Herchel

good point about the / I was using your 3 sites example so didnt see that problem sorry.

here is the modified str_Run line

   str_To_Run = "cmd /c """ & str_Backup_EXE & """ –s " & str_URL & " -f c:\Daily_backup_" & Replace(Mid(str_URL,inStr(str_URL,"http://") + 7) & "(" & date,"/","") & ").dat"

this replaces all / with nothing

I will have a look at making each command run one after the other rather than all at the same time and get back to you

Cheers

Scott
0
 
LVL 15

Accepted Solution

by:
Colosseo earned 500 total points
ID: 11891425
Hi Herchel

this might be the answer

  ' run the backup
  obj_Shell.Run str_To_Run,,1


the 1 makes the code wait until whatever the run is executing completes

Works for me using the ping command

Let me know

Scott
0
 

Author Comment

by:Herschel_Solomon
ID: 11894100
Scott,

I just wanted to say thank you so much for getting this code together, it works brilliantly.

Regards

Herschel
0

Featured Post

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
Browsers only know CSS so your awesome SASS code needs to be translated into normal CSS. Here I'll try to explain what you should aim for in order to take full advantage of SASS.
The viewer will learn the benefit of using external CSS files and the relationship between class and ID selectors. Create your external css file by saving it as style.css then set up your style tags: (CODE) Reference the nav tag and set your prop…
HTML5 has deprecated a few of the older ways of showing media as well as offering up a new way to create games and animations. Audio, video, and canvas are just a few of the adjustments made between XHTML and HTML5. As we learned in our last micr…

708 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now