SitePoint Sponsor

User Tag List

Results 1 to 11 of 11
  1. #1
    Rehab is for quiters! spartan's Avatar
    Join Date
    Apr 2002
    Location
    Cape Town, South Africa
    Posts
    343
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Concatenating files in VBscript

    Ok, I have number of files to concat. no checks or any weird stuff, just take all the files and shove them into one big one.

    what is the best way to do this? I dont want to read each line and do a line for line write if there is a easier, faster way out there that I've missed...

    SO.... did I miss a faster easier way?

    anyone?
    Spartan
    ---------------------
    It's like our sergeant told us before one trip into the jungle. Men! Fifty of you are leaving on a mission. Twenty-five of you ain't coming back.
    -Mr.Payne

  2. #2
    ALT.NET - because we need it silver trophybronze trophy dhtmlgod's Avatar
    Join Date
    Jul 2001
    Location
    Scotland
    Posts
    4,836
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Take it this is with good ol' ASP?

  3. #3
    Rehab is for quiters! spartan's Avatar
    Join Date
    Apr 2002
    Location
    Cape Town, South Africa
    Posts
    343
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I suppose you could call it asp, but since ASP really is only response, request, session and 2 or 3 other objects. All of the stuff in between really is vbscript, isn't it...

    so you wont really use 'ASP' as such to do file manipulation would you?

    anyway, this is kinda off the point...

    Q: using the filesystemObject (vbscript, in a asp file), what is the quickest, easiest way to concatenate 2 files?
    Spartan
    ---------------------
    It's like our sergeant told us before one trip into the jungle. Men! Fifty of you are leaving on a mission. Twenty-five of you ain't coming back.
    -Mr.Payne

  4. #4
    Rehab is for quiters! spartan's Avatar
    Join Date
    Apr 2002
    Location
    Cape Town, South Africa
    Posts
    343
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    ok, lets make it even more clear...
    I have a number of files in a directory.

    Loop through the directory, for each file in the dir, put that file into another file called All.txt or whatever. so that in the end you have only one file with all the other files inside it.

    these are all TXT files...BTW
    Spartan
    ---------------------
    It's like our sergeant told us before one trip into the jungle. Men! Fifty of you are leaving on a mission. Twenty-five of you ain't coming back.
    -Mr.Payne

  5. #5
    ALT.NET - because we need it silver trophybronze trophy dhtmlgod's Avatar
    Join Date
    Jul 2001
    Location
    Scotland
    Posts
    4,836
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Ok, wrote this for you

    Hope it does what your needing:
    [vbs]
    <%
    Class contFiles
    Public Files

    Private Sub Class_Initialize()
    Set Files = Server.CreateObject("Scripting.Dictionary")
    End Sub

    Private Sub Class_Terminate()
    If IsObject(Files) Then
    Files.RemoveAll()
    Set Files = Nothing
    End If
    End Sub

    Public Sub addFile(theFileName)
    Files.Add theFileName, theFileName
    End Sub

    Public Default Function makeFile(fileName)
    On Error Resume Next

    theKeys = Files.Keys
    Set FSO = Server.CreateObject("Scripting.FileSystemObject")
    Do Until IDx = Files.Count
    Set TheTextStream = FSO.OpenTextFile(Server.MapPath(theKeys(IDx)), 1)
    html = html & TheTextStream.ReadAll
    IDx = IDx + 1
    Loop

    Set objTxt = FSo.CreateTextFile(Server.MapPath(fileName), true, false)
    objTxt.write(html)
    objTxt.close

    If ( err.Number <> 0 ) Then
    makeFile = False
    Else
    makeFile = True
    End If
    End Function

    End Class

    Set testFiles = New contFiles

    testFiles.addFile("default.aspx")
    testFiles.addFile("events.aspx")
    testFiles.addFile("about.aspx")

    response.write(testFiles.makeFile("test.txt"))
    %>
    [/vbs]

  6. #6
    Rehab is for quiters! spartan's Avatar
    Join Date
    Apr 2002
    Location
    Cape Town, South Africa
    Posts
    343
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    why, thank you very much sir!
    Spartan
    ---------------------
    It's like our sergeant told us before one trip into the jungle. Men! Fifty of you are leaving on a mission. Twenty-five of you ain't coming back.
    -Mr.Payne

  7. #7
    ALT.NET - because we need it silver trophybronze trophy dhtmlgod's Avatar
    Join Date
    Jul 2001
    Location
    Scotland
    Posts
    4,836
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    No problem. Come anytime, pardner.


  8. #8
    \m/ R.I.P. Dimebag! \m/ JimBolla's Avatar
    Join Date
    Dec 2001
    Location
    erie, pa
    Posts
    1,130
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    i'd suggest making a component to call the old DOS command "type" with an output redirector ">>" to append to the file specified

    for instance the following DOS commands:
    Code:
    type file1.txt >> all.txt
    type file2.txt >> all.txt
    type file3.txt >> all.txt
    would munge the three files into all.txt

    unfortunately ASP doesn't have a direct method of calling commands/executables so you'd have to make a COM wrapper to do this. but i suspect this would be much more effiecient that opening each file w/ the FSO.
    -- JIM BOLLA
    Wanna play Halo 2? My XBOX Live gamertag: crowdozer

  9. #9
    SitePoint Wizard wdmny's Avatar
    Join Date
    Jul 2000
    Location
    Here
    Posts
    1,010
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Crowdozer's idea is great. I just tested it out using a simple batch file and it would do exactly what you are wanting. And since it's a batch file, its quicker and easier to do than your ASP.

    ConcatTxt.bat
    Code:
    echo y | del ConcatAll.txt
    type *.htm >> ConcatAll.txt
    Save that to your directory with the text files, execute it, and tada, all your .txt files are now in ConcatAll.txt.

  10. #10
    Rehab is for quiters! spartan's Avatar
    Join Date
    Apr 2002
    Location
    Cape Town, South Africa
    Posts
    343
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Crowdozer, Wes....
    Thanx guys. It works like a charm. first time I've ever seen the '>>' operator.

    cheers all!
    Spartan
    ---------------------
    It's like our sergeant told us before one trip into the jungle. Men! Fifty of you are leaving on a mission. Twenty-five of you ain't coming back.
    -Mr.Payne

  11. #11
    The doctor is in... silver trophy MarcusJT's Avatar
    Join Date
    Jan 2002
    Location
    London
    Posts
    3,509
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Going back to a pure VBScript solution, I would suggest using the ADODB.Stream object instead - two of these objects would be needed in total.. one for reading each individual file, and one for holding the concatenated data & writing it at the end.

    I suspect that it will be faster than both other solutions because the second method incurs unnecessary overheads in opening (and closing) the shell and opening (and closing) the output file multiple times (I'm assuming that you ARE executing the batch file from a script, either via the WScript.Shell object or some other COM+ component), and the first solution uses the FSO, which is never as quick at reading & writing files as the ADODB.Stream object.

    In addition, the ADODB.Stream objects will (of course) also handle binary streams without any problems at all, whereas the FSO may fall over (or at least struggle a bit), since it was never really intended to handle binary data.
    MarcusJT
    - former ASP web developer / former SPF "ASP Guru"
    - *very* old blog with some useful ASP code

    - Please think, Google, and search these forums before posting!


Bookmarks

Posting Permissions

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