![]() |
China DOS Union-- Unite DOS · Advance DOS · Grow DOS --Union site: www.cn-dos.net Forum site: www.cn-dos.net/forum |
| Guest | Log in | Register | Members | Search | China DOS Union |
|
中国DOS联盟论坛 The time now is 2026-11-11 08:04 |
47,815 topics / 349,930 posts / today 0 new / 48,291 members |
| DOS批处理 & 脚本技术(批处理室) » Replacement of conditional text files [Answered] |
| Printable Version 4,177 / 45 |
| Floor31 123cainiao | Posted 2007-09-25 19:15 |
| 初级用户 Posts 41 Credits 97 | |
|
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. |
|
| Floor32 terse | Posted 2007-09-25 21:23 |
| 银牌会员 Posts 946 Credits 2,404 | |
|
@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 |
|
| Floor33 yoyodos | Posted 2007-09-26 00:58 |
| 初级用户 Posts 59 Credits 128 | |
|
I'll post a complicated one...
So many problems with 123, haha [ Last edited by yoyodos on 2007-9-26 at 01:00 AM ] |
|
| Floor34 123cainiao | Posted 2007-09-26 10:54 |
| 初级用户 Posts 41 Credits 97 | |
Originally posted by terse at 2007-9-25 09:23 PM: 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. |
|
| Floor35 123cainiao | Posted 2007-09-26 10:56 |
| 初级用户 Posts 41 Credits 97 | |
Originally posted by yoyodos at 2007-9-26 12:58 AM: |
|
| Floor36 yoyodos | Posted 2007-09-26 11:04 |
| 初级用户 Posts 59 Credits 128 | |
|
What Terse wrote is correct. Both ab1.txt and ab2.txt are temporary files. Just delete them each time and it's fine. Hehe
|
|
| Floor37 yoyodos | Posted 2007-09-26 11:10 |
| 初级用户 Posts 59 Credits 128 | |
|
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. ^_^
|
|
| Floor38 terse | Posted 2007-09-26 11:27 |
| 银牌会员 Posts 946 Credits 2,404 | |
Originally posted by 123cainiao at 2007-9-26 10:54: 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 |
|
| Floor39 123cainiao | Posted 2007-09-26 13:06 |
| 初级用户 Posts 41 Credits 97 | |
Originally posted by terse at 2007-9-26 11:27 AM: 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! |
|
| Floor40 yoyodos | Posted 2007-09-26 15:03 |
| 初级用户 Posts 59 Credits 128 | |
|
123 continue to set questions. Haha
|
|
| Floor41 yyf0346 | Posted 2007-09-26 16:32 |
| 初级用户 Posts 56 Credits 128 From 江苏 无锡 | |
|
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! |
|
| Floor42 yyf0346 | Posted 2007-09-26 16:41 |
| 初级用户 Posts 56 Credits 128 From 江苏 无锡 | |
|
No one answers me? 555555
I'm a novice, everyone take care of... Waiting... |
|
| Floor43 wudixin96 | Posted 2007-09-26 16:54 |
| 银牌会员 Posts 931 Credits 1,928 | |
|
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.
|
|
| Floor44 terse | Posted 2007-09-26 16:56 |
| 银牌会员 Posts 946 Credits 2,404 | |
|
@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 |
|
| Floor45 yyf0346 | Posted 2007-09-26 18:03 |
| 初级用户 Posts 56 Credits 128 From 江苏 无锡 | |
|
: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... |
|
| Prev 1 2 3 4 Next |
|
[ Contact the Union admin team -
中国DOS联盟 -
Standard version ] Sponsored by ifanr Inc | © 2001–2023 |