Replace a character in a .txt file in batch.
dimedean
-
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 ```
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
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:>
hello
we display 2 characters then the X followed by the rest starting from the 4th character, thus we replaced the 3 with X
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:>
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.
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.
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
@+
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
@+
Have a good day everyone.