Use batch processing to replace ZZ with a specified string in a text file

Source: Internet
Author: User







Use batch processing to replace specified strings in text files. «Previous topic | next topic»
Uglyfrog
Posted on: Reply
Message: 249
Points: 0
Registration: 2001-12-30
In fact, batch processing can also do a lot of work. The following batch processing files implement the function of replacing strings in a specific file type in a specified directory. Although you can write a statement in Unix or Linux, it is easier to use batch processing in Windows than to compile a program.


Reference

@ Echo off

Setlocal

Rem dpath: Specifies the Directory

Rem filetype: Specifies the file type.

Rem scontent: replace the previous string

Rem DContent: string after replacement

Rem tmpfile: Temporary File

Set dpath = C:/test

Set filetype = *. txt

Set scontent = aaa

Set DContent = bbb

Set tmpfile = % random %. tmp

For/R % dpath % I in (% filetype %) Do (

If exist % tmpfile % (DEL/f/Q % tmpfile %)

For/F "tokens = *" % J in (% I) Do (

Set TMP = % J

Echo! TMP: % scontent % = % DContent %! >>% Tmpfile %

)

Move/Y % tmpfile % I

)

Endlocal

Save the preceding content as replace. bat, and then run CMD/V: On/C replace. bat on the command line.
In this example, AAA in all TXT files in the C:/test directory (including sub-Directories) is replaced with BBB.

Zzzevazzz
Posted on: Reply
Message: 275
Points: 0
Registration: 2002-05-13
No ,. Have you tested it?
First, the file with spaces cannot run correctly.
Besides, "Echo! TMP: % scontent % = % DContent %! >>% Tmpfile % "what does it mean?
The replaced files are all like this:
! TMP: AAA = BBB!
! TMP: AAA = BBB!
! TMP: AAA = BBB!
---
Ph4nt0m Security Team
Http://ph4nt0m.org/
Uglyfrog
Posted on: Reply
Message: 249
Points: 0
Registration: 2001-12-30
Sorry, the bat I provided simply demonstrates that it has this function. No strict tests were conducted. If anyone is interested, they can continue to improve or explore new features.

As for the content replaced by zzzevazzz, the system I use is 2000 and I don't know what you are. If your system is also 2000, you can refer to the CMD and set commands help to view the content about the delayed environment variable. Do not forget to add the/V: On parameter of CMD while executing the BAT file. I have never tried other systems. I think all systems later than 2000 will have this feature.

In addition, if I have tried it myself, I will not send it, but I still want to thank you because you have pointed out the problem for me. Learn from you at the same time because of your rigorous attitude towards things.

Zzzevazzz
Posted on: Reply
Message: 275
Points: 0
Registration: 2002-05-13
I didn't notice "CMD/V: On/C". Sorry.
However, there is still a problem. The replaced text has many spaces at the end of the line, because
Echo! TMP: % scontent % = % DContent %! >>% Tmpfile %
Unnecessary spaces are added before ">" of this statement.

The file name with spaces is easy to solve. The following is what I modified:

@ Echo off
Setlocal

Rem dpath: Specifies the Directory
Rem filetype: Specifies the file type.
Rem scontent: replace the previous string
Rem DContent: string after replacement
Rem tmpfile: Temporary File

Set dpath = C:/test
Set filetype = *. txt
Set scontent = aaa
Set DContent = bbb
Set tmpfile = % random %. tmp

For/R % dpath % I in (% filetype %) Do (
If exist % tmpfile % (DEL/f/Q % tmpfile %)
For/F "usebackq tokens = *" % J in ("% I") Do (
Set TMP = % J
Echo! TMP: % scontent % = % DContent %!> % Tmpfile %
)
Move/Y % tmpfile % "% I"
)

Endlocal
---
Ph4nt0m Security Team
Http://ph4nt0m.org/

Uglyfrog
Posted on: Reply
Message: 249
Points: 0
Registration: 2001-12-30
Take a look at it later in the evening. The ECHO is always parsed
Echo! TMP: AAA = BBB! 1> 252.16.tmp, so a space will be added at the end of the row.
You can solve the problem by using the following methods:
Echo! TMP: % scontent % = % DContent %!> Nul 1> % tmpfile %

There is also a space problem in the file name. Your processing method is good and you fully utilize the usebackq feature. It can also be used as an alternative:
For/F "tokens = *" % J in ('Type "% I.

Allyesno
Posted on: Delete edit reply
Message: 1128
Points: 0
Registration: 2003-06-06-06
The bat function is still too weak.

If it's just letters and numbers, the program can also be executed.

If the text contains ^ & ":; <> these characters are difficult to process

No function dedicated to character and string operations, no multi-thread performance, no read/write memory.

Allyesno was edited
---
When there is no such thing★
Qq overflows ~~~~

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.