Replace a character in a .txt file in batch.

dimedean -  
 dimedean -
```html Hello, I would like to know how I could replace a single character in a txt file using a .bat or something... let me explain:
I have a text file containing:

00000000

And I would like to be able to change one of those "0" to a "1" like this:

00100000

The idea is to be able to control each character separately without having to modify all the others.

PS: I was thinking of creating 8 bat files each managing one of the 8 characters...

There you go, I hope someone can find my solution! Thank you in advance, have a great day everyone.

Configuration: Windows XP / Firefox 3.6.4 ```

5 answers

  1. dubcek Posted messages 18627 Registration date   Status Contributor Last intervention   5 660
     
    This .bat takes 2 parameters, the position of the character to change and the replacement value
    C:> type lpt.txt 00000000 C:> aa 3 1 C:> type lpt.txt 00100000 C:> aa 5 2 C:> type lpt.txt 00102000 C:> aa 3 0 C:> type lpt.txt 00002000 C:> type aa.bat @echo off set pos=%1 set /a p1=pos-1 set c=%2 setlocal enableDelayedExpansion for /f "tokens=1" %%a in (lpt.txt) do ( set x=%%a set y=!x:~0,%p1%!%c%!x:~%pos%,10! echo !y! > lpt.txt ) C:>
    1
    1. dimedean
       
      OK thanks for your patience guys impeccable!

      Have a good day everyone.
      0
  2. dubcek Posted messages 18627 Registration date   Status Contributor Last intervention   5 660
     
    hello
    we display 2 characters then the X followed by the rest starting from the 4th character, thus we replaced the 3 with X
    C:> set x=1234567890 C:> echo %x:~0,2%X%x:~3,10% 12X4567890 C:>
    0
  3. dimedean
     
    Thank you very much, Dubcek, for this quick and precise response!! I will try all of that and keep you updated. Thanks again.
    0
    1. dimedean
       
      Thank you Dubcek, I just tried what you suggested and I can't get it to work, I'm a novice in batch.

      What I would like to know is what it actually results in because I've tried several different syntaxes but it doesn't work.

      Thanks again.
      0
  4. dubcek Posted messages 18627 Registration date   Status Contributor Last intervention   5 660
     
    C:> set x=00000000
    C:> set y=%x:~0,2%1%x:~3,10%
    C:> echo %x% & echo %y%
    00000000
    00100000
    C:>
    0
  5. dimedean
     
    So I'll explain, my file "lpt.txt" is located at the root of my HDD, it contains:

    00000000

    And I would like to create batch files capable of changing each character separately as I mentioned before.

    My problem mainly lies in the syntax of the batch file. If you could clearly show me what is in the .bat file. (Not that your explanation is unclear, but as I said I'm a novice in bat).

    There you go, thank you.

    I hope I was a little clearer.
    0
    1. chuka Posted messages 980 Status Member 379
       
      Hi,
      you can use sed.exe (http://gnuwin32.sourceforge.net/packages/sed.htm and in your batch it looks like this:
      sed monfichier.txt -e "s/00000/00001/g" > monfichier1.txt
      del monfichier.txt
      rename monfichier1.txt monfichier.txt
      @+
      0