Announcement

Collapse
No announcement yet.

creating a batch file to edit a text file

Collapse
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • creating a batch file to edit a text file

    Hi,


    Could you guys help me make a batch file which will replace an entry in the text file - coming from an environment variable?


    Basically it'll just replace the username and password. This is an excerpt of the file:
    --------------------


    line 1
    line 2

    <user username="USER" password="pass00"/>
    line 4
    ...



    --------------------

    and it will replace the word "USER" and "pass00" with the Windows environment variable %username%


    Can anybody here help me?


    Thank you!

  • #2
    Re: creating a batch file to edit a text file

    If you put up the complete batch file, then we can advise you properly.

    Comment


    • #3
      Re: creating a batch file to edit a text file

      I think it was a fragment of the TEXT file that was posted
      Tom Jones
      MCT, MCSE (2000:Security & 2003), MCSA:Security & Messaging, MCDBA, MCDST, MCITP(EA, EMA, SA, EDA, ES, CS), MCTS, MCP, Sec+
      PhD, MSc, FIAP, MIITT
      IT Trainer / Consultant
      Ossian Ltd
      Scotland

      ** Remember to give credit where credit is due and leave reputation points where appropriate **

      Comment


      • #4
        Re: creating a batch file to edit a text file

        A kind of similar thread: http://forums.petri.com/showthread.php?t=35683

        What makes it difficault here is that the TEXT file contains signs that have a special meaning to a batch. And there may even be more of those kind of signs in this (xml, html like) file (?). Without knowing more about TXT file I cannot be sure the following sample batch will work for you.

        Some tricks you could use in a batch when dealing with those kind of files are:
        - wrap all stringvalues directly comming from the file between quotes, strip the quotes at the end.
        - For the stringvariables that you define you might need to add quotes around the variablename together with its value.
        - make use of subroutines
        - use escape signs where needed
        - use occasionally character replacement when nessesary
        - step to an advanced script language

        sample:
        Code:
        :: ------------------------------------------------------
        ::  http://forums.petri.com/showthread.php?t=36286
        :: ------------------------------------------------------
        
        @echo off & setlocal enabledelayedexpansion
        
        Set "inputFile=Sample.txt"
        Set "outputFile=%inputFile%-TMP.txt"
        
        >"%outputFile%" (
           For /f "delims=" %%* in (
             'find.exe /n /v ""^<"%inputFile%"'
             ) Do (
             Call:GoParse "''%%*"
             (echo\!WriteLine!)
           )   
        )
        
        ::# replace original file (! remove 'rem' !)
        rem Move /Y "%outputFile%" "%inputFile%"
        
        :end   ----------
        
        goto:eof
        :GoParse lines
        (Set /a i = %i% +1)&(Set "WriteLine=")
        (Set Line=%1)&(Set Line=!Line:''[%i%]=!)
        
        (Set EQsn==)
        >nul (@echo\%Line% |Find.exe /i "username=" |Find.exe /i "password="
           )&&(For /f "tokens=3,5 delims=/%EQsn% " %%a in (
             'cmd /c @echo\%line%') Do (
             (Set Line=!Line:%%~a=%%username%%!)
             (Set Line=!Line:%%~b=%%username%%!)
           )
        )
        (Set WriteLine=!Line:~1,-1!)
        goto:eof
        \Rems
        Last edited by Rems; 18th May 2009, 21:04.

        This posting is provided "AS IS" with no warranties, and confers no rights.

        __________________

        ** Remember to give credit where credit's due **
        and leave Reputation Points for meaningful posts

        Comment


        • #5
          Re: creating a batch file to edit a text file

          @OP, batch is never the suitable tool for file processing (or any other tasks more complex), especially i see that you input file seems like an xml file, (or html?). with that, you should probably improve your productivity by using tools suitable for that, eg Python,Perl, (or vbscript). Here's an alternative solution using Python, if you can afford to use it. (there are modules to help process XML/html as well, but i will go simple here..
          Code:
          import os,fileinput
          username = os.environ['USERNAME']
          for line in fileinput.FileInput("file1.txt",inplace=1):
              if "<user username" in line:
                  line=line.replace("USER",username).replace("pass00",username)
              print line.strip()
          save the above as myscript.py, and on command prompt
          Code:
          C:\test>more file1.txt
          line 1
          line 2
          
          <user username="USER" password="pass00"/>
          line 4
          ...
          
          C:\test>python test.py
          
          C:\test>more file1.txt
          line 1
          line 2
          
          <user username="Administrator" password="Administrator"/>
          line 4
          ...

          Comment

          Working...
          X