Windows Vista Forums

replace files in list

  1. #1


    balzer Guest

    replace files in list

    two text files, doc1.txt and doc2.txt
    document1.txt contains code which have names (absde.exe, etc) inside double
    quotes.

    ReturnAPP_B ("absde.exe")
    ReturnAPP_B ("dhdjd.exe")
    ReturnAPP_B ("fhgfjfj.exe")

    Document2.txt contains list of files, each from new line
    file1.exe
    file2.exe
    files3.exe
    ....

    The task is just replace all files in doc1.txt inside pattern with files
    from doc2.txt , i.e. replace "absde.exe" with "file1.exe", "dhdjd.exe" with
    "file2.exe"and so on.
    The quantity of files in doc1.txt and doc2.txt is n ot the same, doc1.txt
    have more files, so the rest files in doc1.txt can be left as it, without
    replacing.
    How to make this with VBScript?

    Thanks.


      My System SpecsSystem Spec

  2.   


  3. #2


    ekkehard.horner Guest

    Re: replace files in list

    balzer schrieb:

    > two text files, doc1.txt and doc2.txt
    > document1.txt contains code which have names (absde.exe, etc) inside
    > double quotes.
    >
    > ReturnAPP_B ("absde.exe")
    > ReturnAPP_B ("dhdjd.exe")
    > ReturnAPP_B ("fhgfjfj.exe")
    >
    > Document2.txt contains list of files, each from new line
    > file1.exe
    > file2.exe
    > files3.exe
    > ...
    >
    > The task is just replace all files in doc1.txt inside pattern with
    > files from doc2.txt , i.e. replace "absde.exe" with "file1.exe",
    > "dhdjd.exe" with "file2.exe"and so on.
    > The quantity of files in doc1.txt and doc2.txt is n ot the same,
    > doc1.txt have more files, so the rest files in doc1.txt can be left as
    > it, without replacing.
    > How to make this with VBScript?
    >
    > Thanks.
    >
    I'm not sure that I understand your specs/problem (and your data/formats)
    correctly, but to get you started:

    Option Explicit

    Dim goFS : Set goFS = CreateObject( "Scripting.FileSystemObject" )

    WScript.Quit doMain00()

    Function doMain00()
    doMain00 = 1 ' assume error
    Dim reTo : Set reTo = New RegExp
    reTo.Global = True
    reTo.IgnoreCase = True
    reTo.MultiLine = True
    reTo.Pattern = "^\w+\.exe"
    Dim reFrom : Set reFrom = New RegExp
    reFrom.Global = True
    reFrom.IgnoreCase = True
    reFrom.Pattern = """\w+\.exe"""
    Dim sSource : sSource = goFS.OpenTextFile( ".\doc1.txt" ).ReadAll()
    WScript.Echo sSource
    Dim aRpl : aRpl = getRplTo( reTo, ".\doc2.txt" )
    addRplFrom aRpl, reFrom, sSource
    WScript.Echo "=====>"
    WScript.Echo applyRpl( aRpl, sSource )
    doMain00 = 0 ' reached this, so seems ok
    End Function

    Function getRplTo( reX, sFSpec )
    Dim oMTS : Set oMTS = reX.Execute( goFS.OpenTextFile( sFSpec ).ReadAll() )
    ReDim aRVal( oMTS.Count - 1, 1 )
    Dim nIdx
    For nIdx = 0 To UBound( aRVal )
    aRVal( nIdx, 1 ) = oMTS( nIdx ).Value
    Next
    getRplTo = aRVal
    End Function

    Sub addRplFrom( aRpl, reX, sSource )
    Dim oMTS : Set oMTS = reX.Execute( sSource )
    Dim nIdx
    For nIdx = 0 To UBound( aRpl )
    aRpl( nIdx, 0 ) = oMTS( nIdx ).Value
    Next
    End Sub

    Function applyRpl( aRpl, ByVal sSource )
    Dim nIdx
    For nIdx = 0 To UBound( aRpl )
    sSource = Replace( sSource, aRpl( nIdx, 0 ), qq( aRpl( nIdx, 1 ) ) )
    Next
    applyRpl = sSource
    End Function

    Function qq( sTxt )
    qq = """" & sTxt & """"
    End Function

    output:

    C:\wis\_vbs\0506\dev\forum\balzer
    cscript balzer.vbs
    contains code which have names (absde.exe, etc) inside double quotes.

    ReturnAPP_B ("absde.exe")
    ReturnAPP_B ("dhdjd.exe")
    ReturnAPP_B ("fhgfjfj.exe")

    The quantity of files in doc1.txt and doc2.txt is n ot the same,
    doc1.txt have more files, so the rest files in doc1.txt can be left as
    it, without replacing.

    ReturnAPP_B ("nottobechanged.exe")
    ReturnAPP_B ("keepmeto.exe")

    But what about a to-be-replaced exe - eg dhdjd.exe - occuring more
    than once

    ReturnAPP_B ("fhgfjfj.exe")


    =====>
    contains code which have names (absde.exe, etc) inside double quotes.

    ReturnAPP_B ("file1.exe")
    ReturnAPP_B ("file2.exe")
    ReturnAPP_B ("files3.exe")

    The quantity of files in doc1.txt and doc2.txt is n ot the same,
    doc1.txt have more files, so the rest files in doc1.txt can be left as
    it, without replacing.

    ReturnAPP_B ("nottobechanged.exe")
    ReturnAPP_B ("keepmeto.exe")

    But what about a to-be-replaced exe - eg dhdjd.exe - occuring more
    than once

    ReturnAPP_B ("files3.exe")


    I hope that based on this the discussion of specs (e.g. is it correct
    to map the nth occurrence of a To-exe to the nth occurrence of a
    From-exe) and implementation details (just ask for explanations)
    easier.

      My System SpecsSystem Spec

  4. #3


    balzer Guest

    Re: replace files in list


    "ekkehard.horner" <ekkehard.horner@newsgroup> wrote in message
    news:4bd8478b$0$6882$9b4e6d93@newsgroup-online.net...

    > balzer schrieb:

    >> two text files, doc1.txt and doc2.txt
    >> document1.txt contains code which have names (absde.exe, etc) inside
    >> double quotes.
    >>
    >> ReturnAPP_B ("absde.exe")
    >> ReturnAPP_B ("dhdjd.exe")
    >> ReturnAPP_B ("fhgfjfj.exe")
    >>
    >> Document2.txt contains list of files, each from new line
    >> file1.exe
    >> file2.exe
    >> files3.exe
    >> ...
    >>
    >> The task is just replace all files in doc1.txt inside pattern with files
    >> from doc2.txt , i.e. replace "absde.exe" with "file1.exe", "dhdjd.exe"
    >> with "file2.exe"and so on.
    >> The quantity of files in doc1.txt and doc2.txt is n ot the same, doc1.txt
    >> have more files, so the rest files in doc1.txt can be left as it, without
    >> replacing.
    >> How to make this with VBScript?
    >>
    >> Thanks.
    >>
    > I'm not sure that I understand your specs/problem (and your data/formats)
    > correctly, but to get you started:
    >
    > Option Explicit
    >
    > Dim goFS : Set goFS = CreateObject( "Scripting.FileSystemObject" )
    >
    > WScript.Quit doMain00()
    >
    > Function doMain00()
    > doMain00 = 1 ' assume error
    > Dim reTo : Set reTo = New RegExp
    > reTo.Global = True
    > reTo.IgnoreCase = True
    > reTo.MultiLine = True
    > reTo.Pattern = "^\w+\.exe"
    > Dim reFrom : Set reFrom = New RegExp
    > reFrom.Global = True
    > reFrom.IgnoreCase = True
    > reFrom.Pattern = """\w+\.exe"""
    > Dim sSource : sSource = goFS.OpenTextFile( ".\doc1.txt" ).ReadAll()
    > WScript.Echo sSource
    > Dim aRpl : aRpl = getRplTo( reTo, ".\doc2.txt" )
    > addRplFrom aRpl, reFrom, sSource
    > WScript.Echo "=====>"
    > WScript.Echo applyRpl( aRpl, sSource )
    > doMain00 = 0 ' reached this, so seems ok
    > End Function
    >
    > Function getRplTo( reX, sFSpec )
    > Dim oMTS : Set oMTS = reX.Execute( goFS.OpenTextFile(
    > sFSpec ).ReadAll() )
    > ReDim aRVal( oMTS.Count - 1, 1 )
    > Dim nIdx
    > For nIdx = 0 To UBound( aRVal )
    > aRVal( nIdx, 1 ) = oMTS( nIdx ).Value
    > Next
    > getRplTo = aRVal
    > End Function
    >
    > Sub addRplFrom( aRpl, reX, sSource )
    > Dim oMTS : Set oMTS = reX.Execute( sSource )
    > Dim nIdx
    > For nIdx = 0 To UBound( aRpl )
    > aRpl( nIdx, 0 ) = oMTS( nIdx ).Value
    > Next
    > End Sub
    >
    > Function applyRpl( aRpl, ByVal sSource )
    > Dim nIdx
    > For nIdx = 0 To UBound( aRpl )
    > sSource = Replace( sSource, aRpl( nIdx, 0 ), qq( aRpl( nIdx, 1 ) ) )
    > Next
    > applyRpl = sSource
    > End Function
    >
    > Function qq( sTxt )
    > qq = """" & sTxt & """"
    > End Function
    >
    > output:
    >
    > C:\wis\_vbs\0506\dev\forum\balzer
    > cscript balzer.vbs
    > contains code which have names (absde.exe, etc) inside double quotes.
    >
    > ReturnAPP_B ("absde.exe")
    > ReturnAPP_B ("dhdjd.exe")
    > ReturnAPP_B ("fhgfjfj.exe")
    >
    > The quantity of files in doc1.txt and doc2.txt is n ot the same,
    > doc1.txt have more files, so the rest files in doc1.txt can be left as
    > it, without replacing.
    >
    > ReturnAPP_B ("nottobechanged.exe")
    > ReturnAPP_B ("keepmeto.exe")
    >
    > But what about a to-be-replaced exe - eg dhdjd.exe - occuring more
    > than once
    >
    > ReturnAPP_B ("fhgfjfj.exe")
    >
    >
    > =====>
    > contains code which have names (absde.exe, etc) inside double quotes.
    >
    > ReturnAPP_B ("file1.exe")
    > ReturnAPP_B ("file2.exe")
    > ReturnAPP_B ("files3.exe")
    >
    > The quantity of files in doc1.txt and doc2.txt is n ot the same,
    > doc1.txt have more files, so the rest files in doc1.txt can be left as
    > it, without replacing.
    >
    > ReturnAPP_B ("nottobechanged.exe")
    > ReturnAPP_B ("keepmeto.exe")
    >
    > But what about a to-be-replaced exe - eg dhdjd.exe - occuring more
    > than once
    >
    > ReturnAPP_B ("files3.exe")
    >
    >
    > I hope that based on this the discussion of specs (e.g. is it correct
    > to map the nth occurrence of a To-exe to the nth occurrence of a
    > From-exe) and implementation details (just ask for explanations)
    > easier.
    ----------

    does not work, just open this open Windows Script Hosy window with unchanged
    content of doc1.txt, when close, open windows with "=====>", then again with
    content of doc1.txt, then close. I just meant replace file names in doc1
    with relevant files from doc2, doc1 has 254 records, doc1 - 250 recors, so
    difference only 4 line, not a problem add this 4 manually.


      My System SpecsSystem Spec

  5. #4


    ekkehard.horner Guest

    Re: replace files in list

    balzer schrieb:
    [...]

    > No, does not work for me. I wasn't fully clear. The doc1.txt contain
    > vaste vbscript code, where these lines
    >
    > ReturnAPP_B ("a1.exe")
    > ReturnAPP_B ("a2.exe")
    > ...
    > are only the *part* of the full code, so this lines is inside a code,
    > not full content of doc1.txt.
    >
    > Instead, the doc2.txt contains only n lines like
    >
    > file1.exe
    > file2.exe
    > ...
    > and so on.
    > No other strings or code in doc2.txt, only this list.
    >
    Then my first script solves your problem, provided it's correct to replace
    the nth <whatever>.exe in doc1.txt with the nth occurrence (=line) of
    <newname>.exe in doc2.txt.

    But I suspect that you really need to specify a mapping between .exe
    names in doc2.txt - like

    absde.exe,file1.exe
    ....
    fhgfjfj.exe,files3.exe
    ....
    old254.exe,new254.exe

      My System SpecsSystem Spec

  6. #5


    balzer Guest

    Re: replace files in list


    "ekkehard.horner" <ekkehard.horner@newsgroup> wrote in message
    news:4bd87f46$0$6761$9b4e6d93@newsgroup-online.net...

    > balzer schrieb:
    > [...]

    > > No, does not work for me. I wasn't fully clear. The doc1.txt contain
    > > vaste vbscript code, where these lines
    > >
    > > ReturnAPP_B ("a1.exe")
    > > ReturnAPP_B ("a2.exe")
    > > ...
    > > are only the *part* of the full code, so this lines is inside a code,
    > > not full content of doc1.txt.
    > >
    > > Instead, the doc2.txt contains only n lines like
    > >
    > > file1.exe
    > > file2.exe
    > > ...
    > > and so on.
    > > No other strings or code in doc2.txt, only this list.
    > >
    > Then my first script solves your problem, provided it's correct to replace
    > the nth <whatever>.exe in doc1.txt with the nth occurrence (=line) of
    > <newname>.exe in doc2.txt.
    >
    > But I suspect that you really need to specify a mapping between .exe
    > names in doc2.txt - like
    >
    > absde.exe,file1.exe
    > ...
    > fhgfjfj.exe,files3.exe
    > ...
    > old254.exe,new254.exe
    ----------

    Then it will be easier manually to replace each of these 254 lines, one by
    one, via manual copy & paste method. VBScript does not help here.


      My System SpecsSystem Spec

  7. #6


    ekkehard.horner Guest

    Re: replace files in list

    balzer schrieb:

    > "ekkehard.horner" <ekkehard.horner@newsgroup> wrote in message
    > news:4bd87f46$0$6761$9b4e6d93@newsgroup-online.net...

    >> balzer schrieb:
    >> [...]

    >>> No, does not work for me. I wasn't fully clear. The doc1.txt contain
    >>> vaste vbscript code, where these lines
    >>>
    >>> ReturnAPP_B ("a1.exe")
    >>> ReturnAPP_B ("a2.exe")
    >>> ...
    >>> are only the *part* of the full code, so this lines is inside a code,
    >>> not full content of doc1.txt.
    >>>
    >>> Instead, the doc2.txt contains only n lines like
    >>>
    >>> file1.exe
    >>> file2.exe
    >>> ...
    >>> and so on.
    >>> No other strings or code in doc2.txt, only this list.
    >>>
    >> Then my first script solves your problem, provided it's correct to replace
    >> the nth <whatever>.exe in doc1.txt with the nth occurrence (=line) of
    >> <newname>.exe in doc2.txt.
    >>
    >> But I suspect that you really need to specify a mapping between .exe
    >> names in doc2.txt - like
    >>
    >> absde.exe,file1.exe
    >> ...
    >> fhgfjfj.exe,files3.exe
    >> ...
    >> old254.exe,new254.exe
    > ----------
    >
    > Then it will be easier manually to replace each of these 254 lines, one by
    > one, via manual copy & paste method. VBScript does not help here.
    >
    It may be easier for you, but not for a person willing/being able to specify
    a problem correctly and to get/learn the necessary VBScript knowledge/skills.
    So don't blame the programming language, just because you can't use it.

      My System SpecsSystem Spec


replace files in list
Similar Threads
Thread Forum
find in files and replace PowerShell
find and replace a specific string in multiple files VB Script
Copy files and replace older versions? Vista General
Access Vista Install DVD Files-Replace Corrupt Files Vista General
Files merge when I replace a folder? Vista file management