Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Other Programming > VBScript
| Search | Today's Posts | Mark Forums Read
VBScript For questions and discussions related to VBScript.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the VBScript section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
  #1 (permalink)  
Old September 19th, 2007, 09:25 AM
Registered User
 
Join Date: Sep 2007
Location: , , .
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Default Doesn't work properly for multiple file arguments.

I have pieced together some code that works as intended for a single file argument dropped on the VBScript icon. It does not work properly for multiple file arguments, however. Might someone please help me to adjust the code to work for multiple files? I am very much a beginner, so I am sure there is much in the code that could be done differently and better, but here is what I have at this point:

Code:
Dim objArgs
Dim strFile, strFile2, strFile3, strFile4
Dim objFSO
Dim sText, sFound, sTextadd, sText9, sText11, sTexty
Dim sTexty0, sTexty1, sTexty2, sTexty3, RsTexty1
Dim NewTextLine, NArray
Dim arr, x, y
Dim objFile
Dim strFileBase, strFileExt

Const ForReading = 1
Const ForWriting = 2
Const ForAppending = 8


On Error Resume Next
Set objArgs = WScript.Arguments

For i = 0 To objArgs.Count - 1
    strFile = objArgs(i)
    Set objFSO = CreateObject("Scripting.FileSystemObject")
    strFileBase = objFso.GetBaseName (strFile)
    strFileExt = objFso.GetExtensionName (strFile)
    strFile2 = objFso.GetParentFolderName(strFile)
    strTmpFile = strFile2 & "\TmpStart.txt"
    strTmpFile2 = strFile2 & "\TmpEnd.txt"
    strTmpFile3 = strFile2 & "\Reversed.txt"

    strFile3 = strFile2 & "\BACKUP"
    CreateFullPath strFile3
    CopyFile strFile, strFile3

    strFile4 = strFile2 & "\CHANNELS"
    CreateFullPath strFile4

    If objFSO.FileExists (strTmpFile) Then objFSO.DeleteFile (strTmpFile)'TmpStart.txt
    If objFSO.FileExists (strTmpFile2) Then objFSO.DeleteFile (strTmpFile2)'TmpEnd.txt
    If objFSO.FileExists (strTmpFile3) Then objFSO.DeleteFile (strTmpFile3)'Reversed.txt

    Set objFile = objFSO.CreateTextFile (strTmpFile2,ForReading)'TmpEnd.txt
    objFile.Close
    Set objFile = objFSO.CreateTextFile (strTmpFile3,ForReading)'Reversed.txt
    objFile.Close
    Set objFile = objFSO.OpenTextFile (strFile, ForReading)'Original nc1 file
    sText = objFile.ReadAll
    objFile.Close

    arr = Split(sText,vbCrLf)
    sText9 = arr (8)
    sText11 = arr (10)
    If Instr(1, sText9,"C",vbTextCompare) Then    'IF#1
        For y = 1 To UBound(arr)
            sTextadd = Trim(sTextadd & vbCrLf & arr(y))
            If Instr(1,(arr (y)),"BO",vbTextCompare) Then'IF#2
                Do Until InStr (1, arr (y + 1), "EN", vbTextCompare)<> 0        
                    sTexty = arr (y + 1)
                    sTexty = Trim (sTexty)
                    Do While InStr (1, sTexty, "  ")
                        sTexty = Replace (sTexty, "  ", " ")
                    Loop
                    arr2 = Split (sTexty," ",-1)
                    sTexty0 = arr2 (0)
                    sTexty1 = arr2 (1)
                    sTexty2 = arr2 (2)
                    sTexty3 = arr2 (3)
                    sTexty1 = Left (sTexty1, Len (sTexty1) - 1)
                    RsTexty1 = sText11 - sTexty1
                    NArray = Array (sTexty0, RsTexty1 & "o", sTexty2, sTexty3)
                    NewTextLine = Join (NArray, "      ")
                    Set objFSO = CreateObject("Scripting.FileSystemObject")
                    Set objFile = objFSO.OpenTextFile (strTmpFile2,ForAppending)'TmpEnd.txt
                    objFile.Write NewTextLine & vbCrLf
                    objFile.Close
                    Set objFile = Nothing
                    Set objFSO = Nothing
                    y = y + 1
                Loop
                READUP    
                Set objFSO = CreateObject("Scripting.FileSystemObject")
                Set objFile = objFSO.CreateTextFile (strTmpFile,ForWriting)'TmpStart.txt
                objFile.Write "ST" & sTextadd
                objFile.Close
                Set objFile = Nothing
                Set objFSO = Nothing
                CombineFiles
            End If''''''''#2
        Next'For y = 1 To UBound(arr)
    End If'If #1
Next
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''

sub CreateFullPath (byval path)
    dim parent
    path   = objfso.GetAbsolutePathname(path)
    parent = objfso.GetParentFolderName(path)

    if not objfso.FolderExists(parent) then
        CreateFullPath parent
    end if

    if not objfso.FolderExists(path) then
        objfso.CreateFolder(path)
    end if
end sub


''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''

Sub CopyFile(FileSpec, NewFolder)
  On Error Resume Next
  If Right(NewFolder,1) <> "\" Then NewFolder = NewFolder & "\"
  CheckForFile = objFSO.FileExists(FileSpec)
  CheckForFolder = objFSO.FolderExists(NewFolder)
  If CheckForFolder = TRUE Then
    If CheckForFile = TRUE Then
      FileName = objFSO.GetFileName(FileSpec)
      NewFileName = NewFolder & FileName
      If objFSO.FileExists(NewFileName) = TRUE Then
        ClearAttributes NewFileName
      End If
    End If
    objFSO.CopyFile FileSpec, NewFolder, TRUE
  End If 
End Sub


''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''

Sub ClearAttributes(FileName)
  On Error Resume Next
  CheckforFile = objFSO.FileExists(FileName)
  If CheckforFile = TRUE Then
    Set objf = objFSO.GetFile(FileName)
    'Clear hidden, system, or read-only attributes if necessary
    If objf.attributes and 1 Then objf.attributes = objf.attributes - 1
    If objf.attributes and 2 Then objf.attributes = objf.attributes - 2
    If objf.attributes and 4 Then objf.attributes = objf.attributes - 4
    Set objf = Nothing
  End If
End Sub


''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Sub ReadUP
Dim arrFileLines()
ind = 0
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.OpenTextFile(strTmpFile2, ForReading)'TmpEnd.txt
Do Until objFile.AtEndOfStream
     Redim Preserve arrFileLines(ind)
     arrFileLines(ind) = objFile.ReadLine
     ind = ind + 1
Loop
objFile.Close

For l = Ubound(arrFileLines) to LBound(arrFileLines) Step -1

    Set objFile2 = objFSO.OpenTextFile (strTmpFile3, ForAppending)'Reversed.txt
    objFile2.Write arrFileLines (l) & vbCrLf
    objFile2.Close
Next
Set objFile2 = objFSO.OpenTextFile (strTmpFile3,ForAppending)'Reversed.txt
objFile2.Write "EN"
objFile2.Close
Set objFile = Nothing
Set objFile2 = Nothing
Set objFSO = Nothing
End Sub

''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Sub CombineFiles
Const ForReading = 1
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objOutputFile = objFSO.CreateTextFile (strFile4 &  "\" & strFileBase & "." & strFileExt)'New nc1 file
Set objTextFile = objFSO.OpenTextFile(strTmpFile, ForReading)'TmpStart.txt
strText = objTextFile.ReadAll
objTextFile.Close
objOutputFile.WriteLine strText
Set objTextFile = objFSO.OpenTextFile(strTmpFile3, ForReading)'Reversed.txt
strText = objTextFile.ReadAll
objTextFile.Close
objOutputFile.WriteLine strText
objOutputFile.Close
End Sub
I don't see any way to attach a file, but I can furnish a couple of example files that I am using as arguments, if they would help, and someone could let me know how to go about doing that.

Thank you for any help that can be provided !!!
Reply With Quote
  #2 (permalink)  
Old September 20th, 2007, 07:02 AM
Friend of Wrox
Points: 9,611, Level: 42
Points: 9,611, Level: 42 Points: 9,611, Level: 42 Points: 9,611, Level: 42
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Mar 2004
Location: Washington, DC, USA.
Posts: 3,069
Thanks: 0
Thanked 10 Times in 10 Posts
Default

When you say you are using arguments, I assume that you are passing them in using a DOS-type command line. Is that correct? If so, what do the arguments look like (Sorry if I missed them in all that text).

Is this where the code is going wrong?

For i = 0 To objArgs.Count - 1
    strFile = objArgs(i)



mmcdonal
Reply With Quote
  #3 (permalink)  
Old September 20th, 2007, 08:15 AM
Registered User
 
Join Date: Sep 2007
Location: , , .
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thank you for your reply. I passed the arguments by dragging and dropping file names on the VBScript icon.

I got the code to run properly late yesterday evening. Apparently the line that was causing the problem was: sTextadd = Trim(sTextadd & vbCrLf & arr(y)). sTextadd had not been assigned a value earlier in the program. I added the following line before the loop and if statements, and was almost home: sTextadd = arr (0). (I had to remove the "ST" and "&" from the line : objFile.Write "ST" & sTextadd, as well).

I was kind of surprised by what the actual problem was. It didn't seem correct that this adjustment would fix a problem related to multiple file arguments versus single file arguments. Apparently it effected the flow somehow, though I am so new to this that I can't say how.

The following is the code that I currently have that is working. I have cleanup to do on it yet, however:

Code:
Dim strFile, strFile2, strFile3, strFile4
Dim objFSO, objFile, arr, arr2, y
Dim sText, sTextadd, sText9, sText11, sTexty
Dim sTexty0, sTexty1, sTexty2, sTexty3, RsTexty1
Dim NewTextLine, NArray
Dim strFileBase, strFileExt, strTmpFile, strTmpFile2, strTmpFile3

Const ForReading = 1
Const ForWriting = 2
Const ForAppending = 8

On Error Resume Next
For Each strFile In WScript.Arguments
    Set objFSO = CreateObject("Scripting.FileSystemObject")
    strFileBase = objFso.GetBaseName (strFile)
    strFileExt = objFso.GetExtensionName (strFile)
    strFile2 = objFso.GetParentFolderName(strFile)
    strTmpFile = strFile2 & "\TmpStart.txt"
    strTmpFile2 = strFile2 & "\TmpEnd.txt"
    strTmpFile3 = strFile2 & "\Reversed.txt"

    strFile3 = strFile2 & "\BACKUP"
    CreateFullPath strFile3
    CopyFile strFile, strFile3

    strFile4 = strFile2 & "\CHANNELS"
    CreateFullPath strFile4

    If objFSO.FileExists (strTmpFile) Then objFSO.DeleteFile (strTmpFile)
    If objFSO.FileExists (strTmpFile2) Then objFSO.DeleteFile (strTmpFile2)
    If objFSO.FileExists (strTmpFile3) Then objFSO.DeleteFile (strTmpFile3)

    Set objFile = objFSO.CreateTextFile (strTmpFile2,ForReading)'TmpEnd.txt
    objFile.Close
    Set objFile = objFSO.CreateTextFile (strTmpFile3,ForReading)'Reversed.txt
    objFile.Close
    Set objFile = objFSO.OpenTextFile (strFile, ForReading)'Original nc1 file
    sText = objFile.ReadAll
    objFile.Close

    arr = Split(sText,vbCrLf)
    sText9 = arr (8)
    sText11 = arr (10)
    sTextadd = arr (0)
    If Instr(1, sText9,"C",vbTextCompare) Then    'IF#1
        For y = 1 To UBound(arr)
            sTextadd = Trim(sTextadd & vbCrLf & arr(y))
            If Instr(1,(arr (y)),"BO",vbTextCompare) Then'IF#2
                Do Until InStr (1, arr (y + 1), "EN", vbTextCompare)<> 0        
                    sTexty = arr (y + 1)
                    sTexty = Trim (sTexty)
                    Do While InStr (1, sTexty, "  ")
                        sTexty = Replace (sTexty, "  ", " ")
                    Loop
                    arr2 = Split (sTexty," ",-1)
                    sTexty0 = arr2 (0)
                    sTexty1 = arr2 (1)
                    sTexty2 = arr2 (2)
                    sTexty3 = arr2 (3)
                    sTexty1 = Left (sTexty1, Len (sTexty1) - 1)
                    RsTexty1 = sText11 - sTexty1
                    NArray = Array (sTexty0, RsTexty1 & "o", sTexty2, sTexty3)
                    NewTextLine = Join (NArray, "      ")
                    Set objFSO = CreateObject("Scripting.FileSystemObject")
                    Set objFile = objFSO.OpenTextFile (strTmpFile2,ForAppending)'TmpEnd.txt
                    objFile.Write NewTextLine & vbCrLf
                    objFile.Close
                    Set objFile = Nothing
                    Set objFSO = Nothing
                    y = y + 1
                Loop
                READUP    
                Set objFSO = CreateObject("Scripting.FileSystemObject")
                Set objFile = objFSO.CreateTextFile (strTmpFile,ForWriting)'TmpStart.txt
                objFile.Write sTextadd
                objFile.Close
                Set objFile = Nothing
                Set objFSO = Nothing
                CombineFiles
            End If''''''''#2
        Next'For y = 1 To UBound(arr)
    End If'If #1
    Set y = Nothing
    Set NewTextLine = Nothing
    Set sText = Nothing
    Set arr = Nothing
    Set sTextadd = Nothing
Next
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
sub CreateFullPath (byval path)
    dim parent
    path   = objfso.GetAbsolutePathname(path)
    parent = objfso.GetParentFolderName(path)

    if not objfso.FolderExists(parent) then
        CreateFullPath parent
    end if

    if not objfso.FolderExists(path) then
        objfso.CreateFolder(path)
    end if
end sub
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Sub CopyFile(FileSpec, NewFolder)
  On Error Resume Next
  If Right(NewFolder,1) <> "\" Then NewFolder = NewFolder & "\"
  CheckForFile = objFSO.FileExists(FileSpec)
  CheckForFolder = objFSO.FolderExists(NewFolder)
  If CheckForFolder = TRUE Then
    If CheckForFile = TRUE Then
      FileName = objFSO.GetFileName(FileSpec)
      NewFileName = NewFolder & FileName
      If objFSO.FileExists(NewFileName) = TRUE Then
        ClearAttributes NewFileName
      End If
    End If
    objFSO.CopyFile FileSpec, NewFolder, TRUE
  End If 
End Sub
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Sub ClearAttributes(FileName)
  On Error Resume Next
  CheckforFile = objFSO.FileExists(FileName)
  If CheckforFile = TRUE Then
    Set objf = objFSO.GetFile(FileName)
    'Clear hidden, system, or read-only attributes if necessary
    If objf.attributes and 1 Then objf.attributes = objf.attributes - 1
    If objf.attributes and 2 Then objf.attributes = objf.attributes - 2
    If objf.attributes and 4 Then objf.attributes = objf.attributes - 4
    Set objf = Nothing
  End If
End Sub
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Sub ReadUP
Dim arrFileLines()
ind = 0
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.OpenTextFile(strTmpFile2, ForReading)
Do Until objFile.AtEndOfStream
     Redim Preserve arrFileLines(ind)
     arrFileLines(ind) = objFile.ReadLine
     ind = ind + 1
Loop
objFile.Close

For l = Ubound(arrFileLines) to LBound(arrFileLines) Step -1

    Set objFile2 = objFSO.OpenTextFile (strTmpFile3, ForAppending)'Reversed.txt
    objFile2.Write arrFileLines (l) & vbCrLf
    objFile2.Close
Next
Set objFile2 = objFSO.OpenTextFile (strTmpFile3,ForAppending)'Reversed.txt
objFile2.Write "EN"
objFile2.Close
Set objFile = Nothing
Set objFile2 = Nothing
Set objFSO = Nothing
End Sub
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Sub CombineFiles
Const ForReading = 1
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objOutputFile = objFSO.CreateTextFile (strFile4 &  "\" & strFileBase & "." & strFileExt)'New nc1 file
Set objTextFile = objFSO.OpenTextFile(strTmpFile, ForReading)'TmpStart.txt
strText = objTextFile.ReadAll
objTextFile.Close
objOutputFile.WriteLine strText
Set objTextFile = objFSO.OpenTextFile(strTmpFile3, ForReading)'Reversed.txt
strText = objTextFile.ReadAll
objTextFile.Close
objOutputFile.WriteLine strText
objOutputFile.Close
End Sub
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''

I had a response in another forum that stated that the following lines were "wrong":

    Set y = Nothing
    Set NewTextLine = Nothing
    Set sText = Nothing
    Set arr = Nothing
    Set sTextadd = Nothing

I asked for a clarification, but have not gotten a response yet. I was under the impression that it was good practice to set variables to "Nothing" when done with them. Do you have any thoughts for me as a beginner in this regard? (One of the "cleanup" things that I was going to undertake was to make sure that all my variables were set back to "Nothing" when I was done with them. I don't know, now, wheter I should do that, or not.)

Thanks again for the reply !!
Reply With Quote
  #4 (permalink)  
Old September 20th, 2007, 08:20 AM
Friend of Wrox
Points: 9,611, Level: 42
Points: 9,611, Level: 42 Points: 9,611, Level: 42 Points: 9,611, Level: 42
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Mar 2004
Location: Washington, DC, USA.
Posts: 3,069
Thanks: 0
Thanked 10 Times in 10 Posts
Default

As to the last item then, since your code is working, I don't think it is harmful to do what you are doing by setting them to nothing. When the wscript.exe process ends, the memory space should be vacated anyway, so this might be superfluous. Perhaps that is what the other poster was getting at.

mmcdonal
Reply With Quote
  #5 (permalink)  
Old September 20th, 2007, 08:21 AM
Wrox Author
Points: 13,255, Level: 49
Points: 13,255, Level: 49 Points: 13,255, Level: 49 Points: 13,255, Level: 49
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Oct 2005
Location: Ohio, USA
Posts: 4,104
Thanks: 1
Thanked 64 Times in 64 Posts
Send a message via AIM to dparsons
Default

In regards to objects: Database, FSO, etc you should ALWAYS set their value to Nothing once you are done with them as it can lead to memeory leaks. Variables, on the other hand, I personally don't think it is necessary to set their values to nothing.

================================================== =========
Read this if you want to know how to get a correct reply for your question:
http://www.catb.org/~esr/faqs/smart-questions.html
================================================== =========
Technical Editor for:
Professional Search Engine Optimization with ASP.NET

Professional IIS 7 and ASP.NET Integrated Programming

================================================== =========
Reply With Quote
  #6 (permalink)  
Old September 20th, 2007, 08:33 AM
Registered User
 
Join Date: Sep 2007
Location: , , .
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thank you both for your advice on setting variables to "Nothing". I've got a lot to learn and I greatly appreciate the help !!

Reply With Quote
  #7 (permalink)  
Old September 20th, 2007, 08:41 AM
Friend of Wrox
Points: 9,611, Level: 42
Points: 9,611, Level: 42 Points: 9,611, Level: 42 Points: 9,611, Level: 42
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Mar 2004
Location: Washington, DC, USA.
Posts: 3,069
Thanks: 0
Thanked 10 Times in 10 Posts
Default

VBScript Programmer's Reference, Second Edition is available online. That is a good one for VBScript. It has a lot of tips and tricks.

http://wrox.books24x7.com

mmcdonal
Reply With Quote
  #8 (permalink)  
Old September 20th, 2007, 08:52 AM
Registered User
 
Join Date: Sep 2007
Location: , , .
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thank you for the book recommendation. I have a hard copy of that particular book; it has been very helpful, although often, much of it goes over my head. As I keep coming back to it, more of it begins to make sense.

Knowing about the Wrox online library will be helpful, as well. It will probably be a while before I decide to purchase a subscription, but it's really pretty inexpensive "continuing education". Maybe I can convince my employer to "fork out the cash" somewhere down the line.



Reply With Quote


Similar Threads
Thread Thread Starter Forum Replies Last Post
multiple arguments MunishBhatia C# 2 December 8th, 2006 07:34 AM
Link Button does not work properly nitinp ASP.NET 2.0 Basics 0 November 9th, 2006 06:02 AM
Stored procedures won't work properly... eBilyboy SQL Server 2000 1 December 12th, 2005 11:25 AM
Can't Get Percent Format to Work Properly twsinc Access VBA 2 November 19th, 2004 09:15 AM
Getting the data out with multiple arguments toni_montana PHP Databases 2 September 12th, 2004 06:57 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.