China DOS Union

-- Unite DOS · Advance DOS · Grow DOS --

Union site: www.cn-dos.net Forum site: www.cn-dos.net/forum
DOS stands for freedom, openness and progress. Let us work hard, learn from the openness and GNU spirit of FreeDOS and Linux, and together build and grow a free GNU GPL world!

中国DOS联盟论坛
The time now is 2026-06-27 00:07
中国DOS联盟论坛 » DOS批处理 & 脚本技术(批处理室) » Replacement of conditional text files [Answered] View 3,079 Replies 45
Floor 31 Posted 2007-09-25 19:15 ·  中国 新疆 乌鲁木齐 电信
初级用户
Credits 97
Posts 41
Joined 2007-08-30 19:05
18-year member
UID 96211
Gender Male
Status Offline
Can we extract 127.128.225.18 from the cd.txt file to update the ab.txt file? The newly generated ab.txt file is:
[abcd]
doorCount = 1
goodpaIp = 127.128.225.18:8383
Max = 125
tackmoneys = 127.128.225.18:8086
doorCounttc = 12
doorCountwkl= 18
doorCountasd = 20

Please note: The number of digits of 127.128.225.18 is assumed to be variable, that is, even if it becomes an IP with other numbers of digits like 12.11.23.11 or others, the complete replacement can be achieved.
Floor 32 Posted 2007-09-25 21:23 ·  中国 江苏 常州 电信
银牌会员
★★★
Credits 2,404
Posts 946
Joined 2005-09-08 13:44
20-year member
UID 42345
Status Offline
@echo off
setlocal enabledelayedexpansion
ren ab.txt ab1.txt
for /f "tokens=1,2,3 delims==:" %%i in ('type cd.txt^|find /i "tackmoneys"') do set data=%%j
for /f "tokens=1,2,3 delims==: " %%i in ('type ab1.txt^|find /i "goodpaIp"') do set data1=%%j
for /f "tokens=1,2,3 delims==: " %%i in ('type ab1.txt^|find /i "tackmoneys"') do set data2=%%j
if !data! equ !data1! if !data! equ !data2! goto :eof
for /f "tokens=* delims==: " %%i in (ab1.txt) do (
set m=%%i
set m=!m:%data1%=%data%!
set m=!m:%data2%=%data%!
echo !m! >>ab2.txt
)
ren ab2.txt ab.txt
pause
Floor 33 Posted 2007-09-26 00:58 ·  中国 北京 广电网
初级用户
★★
Credits 128
Posts 59
Joined 2007-09-15 23:03
18-year member
UID 97496
Gender Male
Status Offline
I'll post a complicated one...

@echo off
cd.>new.txt
setlocal enabledelayedexpansion
set m=0
set a=0
set b=0
set cd_name=tackmoneys_0
set ab_name1=goodpaIp
set ab_name2=tackmoneys
for /f "delims=" %%i in (cd.txt) do (
call :cdcd %%i
)
for /f "delims=" %%y in (ab.txt) do (
call :abab %%y
)
for /f "delims=" %%y in (ab.txt) do (
set str=%%y
set str=!str:%ab_result1%=%cd_result%!
set str=!str:%ab_result2%=%cd_result%!
echo !str!>>new.txt
)

:abab
if "%1" == "" exit /b
if "%a%" == "1" (
set ab_result1=%1
for /f "tokens=1,2 delims=:" %%i in ("!ab_result1!") do set ab_result1=%%i
set a=0
exit /b
)
if "%b%" == "1" (
set ab_result2=%1
for /f "tokens=1,2 delims=:" %%i in ("!ab_result2!") do set ab_result2=%%i
set b=0
exit /b
)
if "%1" == "%ab_name1%" set a=1
if "%1" == "%ab_name2%" set b=1
shift
goto abab

:cdcd
if "%1" == "" exit /b
if "%m%" == "1" (
set cd_result=%1
set m=0
exit /b
)
if "%1" == "%cd_name%" set m=1
shift
goto cdcd

pause

So many problems with 123, haha

[ Last edited by yoyodos on 2007-9-26 at 01:00 AM ]
Recent Ratings for This Post ( 1 in total) Click for details
RaterScoreTime
123cainiao +2 2007-09-26 10:56
Floor 34 Posted 2007-09-26 10:54 ·  中国 新疆 乌鲁木齐 电信
初级用户
Credits 97
Posts 41
Joined 2007-08-30 19:05
18-year member
UID 96211
Gender Male
Status Offline
Originally posted by terse at 2007-9-25 09:23 PM:
@echo off
setlocal enabledelayedexpansion
ren ab.txt ab1.txt
for /f "tokens=1,2,3 delims==:" %%i in ('type cd.txt^|find /i "tackmoneys"') do set data=%%j
for /f "tokens ...

This program, after repeated testing, is found to have a problem:
The first time the program runs, it will generate an ab1.txt which has the same parameters as the files in ab.txt, and the replacement is successful.
Modify the value in cd.txt casually, and then run the program again (without deleting the previously generated ab1.txt)
It is found that there is a file duplication prompt, and then ab2.txt will be generated, and only ab2.txt is successfully replaced, while ab.txt and ab1.txt remain unchanged.
Modify the value in cd.txt casually again, and then run the program again (without deleting the previously generated ab1.txt and ab2.txt)

As a result, the program prompts for file duplication, and the new replacement value appears in the ab2.txt file in a cumulative form, and no other files are replaced.
Floor 35 Posted 2007-09-26 10:56 ·  中国 新疆 乌鲁木齐 电信
初级用户
Credits 97
Posts 41
Joined 2007-08-30 19:05
18-year member
UID 96211
Gender Male
Status Offline
Originally posted by yoyodos at 2007-9-26 12:58 AM:
I'll post a complicated one...

@echo off
cd.>new.txt
setlocal enabledelayedexpansion
set m=0
set a=0
set b=0
set cd_name=tackmoneys_0
set ab_name1=goodpaIp
set ab_name2=tackmone ...

Although this program is much more complicated, after repeated testing, it all performs normally!
Floor 36 Posted 2007-09-26 11:04 ·  中国 北京 广电网
初级用户
★★
Credits 128
Posts 59
Joined 2007-09-15 23:03
18-year member
UID 97496
Gender Male
Status Offline
What Terse wrote is correct. Both ab1.txt and ab2.txt are temporary files. Just delete them each time and it's fine. Hehe
Floor 37 Posted 2007-09-26 11:10 ·  中国 北京 广电网
初级用户
★★
Credits 128
Posts 59
Joined 2007-09-15 23:03
18-year member
UID 97496
Gender Male
Status Offline
terse is to find the ip through special characters, and mine is to find the ip through the position of the specified string, just different ideas. ^_^
Floor 38 Posted 2007-09-26 11:27 ·  中国 江苏 常州 溧阳市 电信
银牌会员
★★★
Credits 2,404
Posts 946
Joined 2005-09-08 13:44
20-year member
UID 42345
Status Offline
Originally posted by 123cainiao at 2007-9-26 10:54:

There is a problem with this program after repeated testing:
When the program runs for the first time, it will generate a ab1.txt and the parameters in ab.txt are consistent, and the replacement is successful.
Modify the value in cd.txt casually, and then ...

ab1.txt is just a backup of the original ab.txt, this step can be removed
Or try it this way
@echo off
setlocal enabledelayedexpansion
cd.>ab2.txt
for /f "tokens=1,2,3 delims==:" %%i in ('type cd.txt^|find /i "tackmoneys"') do set data=%%j
for /f "tokens=1,2,3 delims==: " %%i in ('type ab.txt^|find /i "goodpaIp"') do set data1=%%j
for /f "tokens=1,2,3 delims==: " %%i in ('type ab.txt^|find /i "tackmoneys"') do set data2=%%j
if !data! equ !data1! if !data! equ !data2! goto :eof
for /f "tokens=* delims==: " %%i in (ab.txt) do (
set m=%%i
set m=!m:%data1%=%data%!
set m=!m:%data2%=%data%!
echo !m! >>ab2.txt
)
del ab.txt
ren ab2.txt ab.txt
pause
Floor 39 Posted 2007-09-26 13:06 ·  中国 新疆 乌鲁木齐 电信
初级用户
Credits 97
Posts 41
Joined 2007-08-30 19:05
18-year member
UID 96211
Gender Male
Status Offline
Originally posted by terse at 2007-9-26 11:27 AM:

ab1.txt is just a backup of the original ab.txt, this step can be removed
Or try it this way
@echo off
setlocal enabledelayedexpansion
cd.>ab2.txt
for /f "tokens=1,2,3 delims==:" %%i in ( ...


The test is complete, the program is completely normal!
Thank you for the reply!
All the above reference answers, after careful study and integration, can completely solve other problems of similar text string replacement. Thank you again to all the netizens who posted!
Floor 40 Posted 2007-09-26 15:03 ·  中国 北京 广电网
初级用户
★★
Credits 128
Posts 59
Joined 2007-09-15 23:03
18-year member
UID 97496
Gender Male
Status Offline
123 continue to set questions. Haha
Floor 41 Posted 2007-09-26 16:32 ·  中国 江苏 无锡 联通
初级用户
★★
Credits 128
Posts 56
Joined 2007-09-26 15:37
18-year member
UID 98321
Gender Male
From 江苏 无锡
Status Offline
I'm a newbie, just came here. I want to ask you all, how to replace a certain string in a specified file according to the value entered by the user in the window.

For example, there is an a.sql file, and the b.bat file is in the same directory. There is a part of the content in the a.sql file:

EXECUTE @ReturnCode = msdb.dbo.sp_add_job @job_id = @JobID OUTPUT , @job_name = N'预定检查', @owner_login_name = N'localhost\Administrator', @description = N'没有可用的描述。', @category_name = N'[Uncategorized (Local)]', @enabled = 1, @notify_level_email = 0, @notify_level_page = 0, @notify_level_netsend = 0, @notify_level_eventlog = 2, @delete_level= 0
. . . . . . .

How to write the b.bat file to replace localhost with a string entered by the user;

For another example, there is an a.INI file in the INI folder, and the content is as follows:

[CONNECT_DB]
CONNECTSTRING="Provider=SQLOLEDB.1;Password=MTIzNDU2;Persist Security Info=True;User ID=SA;Initial Catalog=HXGL_DB;Data Source=localhost\SQLEXPRESS1"
LatestLogin=admin
. . . . . .

This INI folder is not in the same directory structure as the b.bat. How to replace localhost\SQLEXPRESS1 with the entered character?

I know that interacting with the user is SET /P char=Please enter the string:
Then how to operate according to the above idea with the string entered by the user? I'm applying in the company. Can anyone help me? Thank you all in advance!
Floor 42 Posted 2007-09-26 16:41 ·  中国 江苏 无锡 联通
初级用户
★★
Credits 128
Posts 56
Joined 2007-09-26 15:37
18-year member
UID 98321
Gender Male
From 江苏 无锡
Status Offline
No one answers me? 555555
I'm a novice, everyone take care of...
Waiting...
Floor 43 Posted 2007-09-26 16:54 ·  中国 香港 腾讯云
银牌会员
★★★
Credits 1,928
Posts 931
Joined 2007-01-06 11:46
19-year member
UID 75624
Gender Male
Status Offline
It's not that I don't answer. For such questions, please search the forum first. Only ask if there's really no answer after searching.
Floor 44 Posted 2007-09-26 16:56 ·  中国 江苏 常州 武进区 电信
银牌会员
★★★
Credits 2,404
Posts 946
Joined 2005-09-08 13:44
20-year member
UID 42345
Status Offline
@echo off
setlocal ENABLEDELAYEDEXPANSION
set str1=localhost\SQLEXPRESS1
SET /P STR2=Enter replacement character
for /f "delims=" %%i in (Path\a.ini) do (
set str=%%i
set str=!str:%str1%=%str2%!
echo !str!>>Path\b.ini
)
pause
Recent Ratings for This Post ( 1 in total) Click for details
RaterScoreTime
yyf0346 +1 2007-09-26 18:04
Floor 45 Posted 2007-09-26 18:03 ·  中国 江苏 无锡 联通
初级用户
★★
Credits 128
Posts 56
Joined 2007-09-26 15:37
18-year member
UID 98321
Gender Male
From 江苏 无锡
Status Offline
:D
Thanks a lot and many thanks! Since I just joined, there are many things I don't understand. Maybe the questions I ask are already in the forum. In the future, I will search the forum resources first before asking questions. Please take care of me...
Forum Jump: