3

I found a batch script that changes a string to another string.

Below batch script changes every 'bath' to 'hello' in test.txt

@echo off
setlocal enabledelayedexpansion
set INTEXTFILE=test.txt    set OUTTEXTFILE=test_out.txt
set SEARCHTEXT=bath
set REPLACETEXT=hello

set OUTPUTLINE=

for /f "tokens=1,* delims=¶" %%A in ( '"type %INTEXTFILE%"') do (
SET string=%%A
SET modified=!string:%SEARCHTEXT%=%REPLACETEXT%!

echo !modified! >> %OUTTEXTFILE%
)
del %INTEXTFILE%
rename %OUTTEXTFILE% %INTEXTFILE%

(copied from How to replace substrings in windows batch file)

But what I want to do is changing every '△▽' to 'newline and xy' in test.txt For example, if test.txt is

abc△▽def

then the result should be

abc
xydef

Before attacking this problem, I tried easier-looking problem : changing every '△▽' to 'xy'. But this easier-looking problem looks even difficult to me. I just simply changed bath to △▽, and hello to xy in the above script. Then the script didn't work. The reason I guess is - since △,▽ are symbols.

Could you teach me how to express symbol or line break, tab in the refered batch script ?

Community
  • 1
  • 1
imida k
  • 239
  • 2
  • 9
  • Sorry, but what kind of a text file does include `WHITE UP-POINTS TRIANGLE` and `WHITE DOWN-POINTS TRIANGLE`? What is the encoding of that text file? Which text editor is used to open that text file and can properly show that triangles? – kitap mitap Mar 28 '15 at 17:40
  • Just windows notepad. The encoding is UTF-8. – imida k Mar 28 '15 at 22:12
  • My final purpose is making windows batch script custom string replacer. I use windows notepad. The encoding is UTF-8, but I don't care that much because I can change the encoding at the final step... ANSI or UNICODE are accepted too. Thank you – imida k Mar 28 '15 at 22:35
  • There is a solution to replace triangles with "xy". First change the codepage: `CHCP 1254` Then convert file to ANSI `TYPE INTEXTFILE.txt > ASCII.txt` The `ASCII.txt` file includes weird characters (`¢-`, ALT+0162 and ALT+45) instead of triangles. It is easy to replace them with "xy" characters. But I wasn't able to find a solution for adding newlines and I don't know if the solution I mentioned can be integrated with a solution to addnewlines. The final file will be encoded in ANSI. – kitap mitap Mar 28 '15 at 23:19
  • Sorry. You should use -I think- `CHCP 1252` instead of `CHCP 1254`. – kitap mitap Mar 28 '15 at 23:53
  • it may be much easier in powershell because UTF-8 support in cmd.exe and other Windows tools is very bad – phuclv Apr 09 '18 at 01:44

1 Answers1

0

The WHITE UP-POINTS TRIANGLE is encoded in UTF-8 with 3 bytes with the hexadecimal code values E2 96 B3 and the WHITE DOWN-POINTS TRIANGLE with E2 96 BD.

How can you find and replace text in a file using the Windows command-line environment? contains several answers with suggestions on how to replace strings in batch mode using console applications or even just Windows standard commands.

My favorite tool for such replaces is Xchang32.exe from Clay's Utilities for Win32.

Important note: Clay Ruth, author of Xchang32.exe and owner of domain clayruth.com died years ago. For that reason the ZIP file with this tool is not available anymore in world wide web although it was permitted explicitly to distribute this package free of charge according to read me file in the ZIP file.

This console application replaces bytes by bytes and can be used therefore for any file type, for binary files as well as text files of any encoding.

This tool with the following parameters makes the replace as you want.

xchang32.exe Test.txt "^xE2^x96^xB3^xE2^x96^xBD" "^x0D^x0Axy"

All △▽ are replaced by a carriage return (0D) and a line-feed (0A) and the characters xy in file Test.txt. In case of file Test.txt is a UNIX file with using just a line-feed as line terminator, remove ^x0D from replace string.

Of course the same replacement could be also done with a UTF-8 aware text editor like UltraEdit which even has a Replace in Files command capable to replace UTF-8 encoded strings in all files of a folder or folder tree.

Mofi
  • 46,139
  • 17
  • 80
  • 143