Tom's Guide | Tom's Hardware | Tom's Games
![]() |
![]() |
![]() |
Is there a batch file that will sort through a text file, and replace every letter "C" with the letter "D"?

this is not a batch solution, tasks like this can be done easily in scripting languages such as perl/python...eg in python:
# open the file for writing
o = open("outputfile.txt,"a")
for lines in file("yourinputfile.txt"):
#replace "C" with "D" and write to file
o.write(lines.replace("C","D") + "\n")
o.close() #close the file

This batch will replace lowercase c with lowercase d.
::---
@echo off
SetLocal EnableDelayedExpansion
For /F "tokens=* delims=" %%A in (Textfile.txt) Do (
Set TxtLine=%%A
Set TxtLine=!TxtLine:c=d!
echo !TxtLine!>>tmp.txt)
del Textfile.txt & ren tmp.txt Textfile.txt
::---

shr00m, awesome, thanks. Is there a way to change 2 letters at once? I.E. a=b and c=d, or would I have to start another FOR statement.

Ok, one more question. When I run the program, it changes all "C"'s into lowercase "d"'s. Is there a way to turn all lowercase "c"'s into lowercase "d"'s and all upper case "C"'s into uppercase "D"'s?

Hi
As far as I know the set command is'nt case sensitive.I have an old dos program call Change.com from a PC Magazine floppy.
It allows you to change text within a text file and is case sensitive.
in your case the syntax would be
change TextFile.txt "D" "C"
change TextFile.txt "d" "c"
If you can't find it on the net let me know.
---------------
CHANGE.COM
Michael J. Mefford
Purpose
Performs a rapid search-and-replace operation for text strings and/or
ASCII decimal codes throughout a file of maximum 40,GOG-byte length.
Format
CHANGE filespec findstring replacestring RemarksThe filespec parameter may include a drive letter and a path in addition
to the designated filename.
Findstring and replacestring may consist of text characters enclosed within (double) quote marks or ASCII decimal codes whose numbers are separated by commas. Note that the format requires that each parameter be separated by a single space. Text strings in quotes and ASCII values in numerals may be combined in either string if separated by commas.Example
To change all references to Miss Jones to Mrs. Smith in the file NOGOSSIP.ART on the current directory, you would enter
CHANGE NOGOSSIP.ART "Miss Jones" "Mrs. Smith" Example
To strip out all carriage return-line feeds (i.e. replace them with a null string) in the file MCI.B16 in the \COMM subdirectory, enter
CHANGE \COMM\MCI.B16 13,10 "" Notes
1. In the second example you might want to use a space between the quote marks rather than a null string to keep the words from running together. Observe that by putting the number of the month in hexadecimal (B=November) you can fit both month and day within the three character DOS filename extension.

you might be better off doing scripting using perl/python/vbscript/etc...they are more "powerful" in text manipulation, and easier to use too...

Also, the text file i use this program on loses its exclamation marks. It seems that the when line:
$nbsp Set TxtLine=!TxtLine:c=d!
interprets the (!) in the file, it sees it as the end of the variable. Any solution around this? Forgive me for being stubborn, but I like better to learn how to program a difficult program than to take the easy way out and download a solution.

Hi CWoodward,
AFAIK the "case-insensitivity" of a var name is hard wired.
Note that the var NAME is not case sensitive.
set abc=look
is equivalent to:
set ABC=look
As far as the behavior of !, I have no idea.
=====================================
If at first you don't succeed, you're about average.M2

CWoodward: Afaik its not possible to separate upper from lowercase with that script, and i dont think there are any creative workarounds.
Couldnt find a solution with those exlamation marks either. If you gonna do text manipulation, you should rather go for another scripting language (Perl/Python as mentioned earlier).

Well, if it can't be done in batch, I do have a C++ compiler (Bloodshed Dev C++) that I am trying to learn to code with. Is there a C++ solution?

wow..you are going from easy to more "troublesome" way to solve your problem. For learning purposes , i am sure there's no problem with that. :)

If you really want the experience of doing it the hard way, you could write the program in binary, like they did in the old days, but I'm not sure what compiler you'd need.

![]() |
![]() |
![]() |

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.
| Ads by Google |