Computing.Net > Forums > Programming > Batch Letter Change

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

Batch Letter Change

Reply to Message Icon

Name: CWoodward
Date: July 3, 2006 at 02:08:38 Pacific
OS: Windows XP Home Edition
CPU/Ram: Intel Celleron
Product: Dell
Comment:

Is there a batch file that will sort through a text file, and replace every letter "C" with the letter "D"?



Sponsored Link
Ads by Google

Response Number 1
Name: ghostdog
Date: July 3, 2006 at 02:44:30 Pacific
Reply:

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


0

Response Number 2
Name: Shr0Om
Date: July 3, 2006 at 03:19:54 Pacific
Reply:

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
::---


0

Response Number 3
Name: Mechanix2Go
Date: July 3, 2006 at 04:26:09 Pacific
Reply:

shr0Om,

Very cool.

Thanks


If at first you don't succeed, you're about average.

M2


0

Response Number 4
Name: Shr0Om
Date: July 3, 2006 at 04:36:51 Pacific
Reply:

Mechanix2Go:

Its one of IVO's tricks that i picked up;)


0

Response Number 5
Name: Mechanix2Go
Date: July 3, 2006 at 05:35:26 Pacific
Reply:

IVO is the man.


If at first you don't succeed, you're about average.

M2


0

Related Posts

See More



Response Number 6
Name: uli_glueck
Date: July 3, 2006 at 07:00:14 Pacific
Reply:

Yup, he is.

Btw:
In NT4 a subprocedure is necessary

Uli


0

Response Number 7
Name: CWoodward
Date: July 3, 2006 at 16:08:30 Pacific
Reply:

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.


0

Response Number 8
Name: dtech10
Date: July 3, 2006 at 16:38:50 Pacific
Reply:

No, Just add another Set Command.

Set TxtLine=!TxtLine:a=b!
Set TxtLine=!TxtLine:c=d!


0

Response Number 9
Name: CWoodward
Date: July 3, 2006 at 16:50:53 Pacific
Reply:

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?


0

Response Number 10
Name: dtech10
Date: July 4, 2006 at 13:17:47 Pacific
Reply:

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 Remarks

The 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.


0

Response Number 11
Name: ghostdog
Date: July 4, 2006 at 17:18:58 Pacific
Reply:

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


0

Response Number 12
Name: CWoodward
Date: July 4, 2006 at 20:13:47 Pacific
Reply:

So is there no way to make the SET command case-sensitive? Or maybe somehow use ASCII codes?


0

Response Number 13
Name: CWoodward
Date: July 4, 2006 at 20:41:24 Pacific
Reply:

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.


0

Response Number 14
Name: Mechanix2Go
Date: July 5, 2006 at 00:18:07 Pacific
Reply:

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



0

Response Number 15
Name: Shr0Om
Date: July 5, 2006 at 02:15:36 Pacific
Reply:

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).


0

Response Number 16
Name: FishMonger
Date: July 5, 2006 at 11:02:32 Pacific
Reply:

perl -ni.bak -e "tr/abcABC/bcdBCD/" file.txt


0

Response Number 17
Name: CWoodward
Date: July 5, 2006 at 23:22:56 Pacific
Reply:

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?


0

Response Number 18
Name: ghostdog
Date: July 6, 2006 at 02:39:06 Pacific
Reply:

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. :)


0

Response Number 19
Name: FishMonger
Date: July 6, 2006 at 09:08:08 Pacific
Reply:

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.


0

Sponsored Link
Ads by Google
Reply to Message Icon






Post Locked

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


Go to Programming Forum Home


Sponsored links

Ads by Google


Results for: Batch Letter Change

Using batch to change date format www.computing.net/answers/programming/using-batch-to-change-date-format/16172.html

Batch to change desktop wallpaper? www.computing.net/answers/programming/batch-to-change-desktop-wallpaper/15105.html

batch to change password www.computing.net/answers/programming/batch-to-change-password/15008.html