![]() |
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-08-26 02:32 |
47,812 topics / 349,910 posts / today 0 new / 48,264 members |
| DOS批处理 & 脚本技术(批处理室) » [Help] How to get the content at the specified position of the specified line of a file |
| Printable Version 4,430 / 20 |
| Floor1 krell | Posted 2006-10-03 13:58 |
| 初级用户 Posts 6 Credits 116 | |
|
How to use batch processing to obtain the content at a specified position in a specified line of a file and write it to another file.
For example, the 12th character from the end in the 5th line of a file, and the 1st to 3rd characters from the end in the 6th line (there may be 1-3 characters after an = symbol in this line content). After getting these two values, write them to the specified lines of another file. (It's okay to simply output to a file for simplicity, with not many lines.) I really don't know any programming, and I think batch processing should be the simplest. However, after reading many tutorials, I didn't find the implementation method for getting the specified content in a file. Please help me, thank you! |
|
| Floor2 namejm | Posted 2006-10-03 21:24 |
| 荣誉版主 Posts 1,737 Credits 5,226 From 成都 | |
|
The initial code is roughly written. The poster can try to see if it works. Also, if you can post part of the content of the file, it would be better to find the pattern, because I don't know whether the content you mentioned to extract is the characters from the 1st to the 3rd from the end, or 1 or 2 or 3 from the end.
[ Last edited by namejm on 2006-10-3 at 21:51 ] |
|
| Floor3 pengfei | Posted 2006-10-03 21:28 |
| 银牌会员 Posts 485 Credits 1,218 From 湖南.娄底 | |
|
Brother namejm's code is concise. It would be even better if the situation where spaces exist in text lines and the entire line cannot be extracted is taken into account.
|
|
| Floor4 namejm | Posted 2006-10-03 21:33 |
| 荣誉版主 Posts 1,737 Credits 5,226 From 成都 | |
Originally posted by pengfei at 2006-10-3 21:28: Hehe, actually it can already extract lines that may have spaces. In the for statement, to extract lines with spaces, in addition to using "tokens=*", "delims=" is also a good choice. We discussed this in the group the other day, and pengfei might not have noticed. |
|
| Floor5 pengfei | Posted 2006-10-03 21:42 |
| 银牌会员 Posts 485 Credits 1,218 From 湖南.娄底 | |
|
Hehe~ Indeed, it seems there are three methods for extracting entire lines:
tokens=* delims=* delims= But I still noticed that your code is missing a /F parameter in the for. ^_^ |
|
| Floor6 namejm | Posted 2006-10-03 21:50 |
| 荣誉版主 Posts 1,737 Credits 5,226 From 成都 | |
|
Thank you for your reminder. It's indeed missing a /F parameter. I used to test the code before sending it out, but this time I thought it was simple and didn't test it. Unexpectedly, such a basic mistake occurred, and it has been corrected ^_^
So far, I have only found two methods to extract the entire line, and the delims=* you mentioned is difficult to extract because its meaning is just to extract the content separated by *. When there is no * in the content, it will also extract lines with spaces. However, if there is a * in the content, the situation is very different. You can test it and find out. |
|
| Floor7 pengfei | Posted 2006-10-04 05:06 |
| 银牌会员 Posts 485 Credits 1,218 From 湖南.娄底 | |
Originally posted by namejm at 2006-10-3 21:50: It seems this is a habitual mistake of mine. "tokens=*" assigns all the content of the text line to the first replaceable variable. "delims=" does not use any delimiters, which cancels the default space as a delimiter. It makes all the content of the text line assigned to a replaceable variable. "delims=*" but this uses a special delimiter to replace spaces. Obviously, it doesn't work. As the brother said, once this character appears, it cannot be extracted normally. |
|
| Floor8 krell | Posted 2006-10-04 12:59 |
| 初级用户 Posts 6 Credits 116 | |
|
Thanks to the two enthusiastic experts. However, I tried it and it seems not working.
The content of the file from which the value needs to be obtained is as follows: [Program ] Name = NEW ERY Source=FAC [FAC] ImageFileName=90B5A901.ACR ImageFileNumber=34 [Burn] ImageFileNumber=0 Description= [Action] DoRecovery=No The values needed are 90B5A901.ACR and 34 (this 34 may be a 1-digit data, may also be a 2-digit data, may also be a 3-digit data). After getting these two data, write them into a new file. Image_Name=Here write 90B5A901.ACR, Image_Count=Here write 34. The content of the new file is as follows: [Program ] Name = NEW ERY Source=FD_INFO [FD_INFO] Image_Name= Image_Count= Image_Size=51200 Image_PerDisc=13 Image_Language=1033 It is imagined that this operation should be very simple, not trying to get something for nothing, but really too inexperienced. Hope the expert can give more advice, thanks! |
|
| Floor9 pengfei | Posted 2006-10-05 00:23 |
| 银牌会员 Posts 485 Credits 1,218 From 湖南.娄底 | |
|
Please ask the owner to test the effect:
|
|
| Floor10 namejm | Posted 2006-10-05 00:36 |
| 荣誉版主 Posts 1,737 Credits 5,226 From 成都 | |
|
The line "set /a num+=1" can be omitted. In the for loop, using "skip=4" directly skips the first 4 lines of content, and then you can start reading from the fifth line. Alternatively, using "more +4 1.ini" can also achieve the same effect.
|
|
| Floor11 pengfei | Posted 2006-10-05 01:45 |
| 银牌会员 Posts 485 Credits 1,218 From 湖南.娄底 | |
|
I use the counting method to extract data because the sentence "ImageFileNumber" appears twice.
|
|
| Floor12 krell | Posted 2006-10-05 04:12 |
| 初级用户 Posts 6 Credits 116 | |
|
Hehe, still not working.
The generated new file did not get the value, it's still like this! The expert can give it a try. Image_Name= Image_Count= |
|
| Floor13 pengfei | Posted 2006-10-05 05:02 |
| 银牌会员 Posts 485 Credits 1,218 From 湖南.娄底 | |
|
The file that needs to get the value is defaulted to 1.ini in the current directory
|
|
| Floor14 krell | Posted 2006-10-05 05:45 |
| 初级用户 Posts 6 Credits 116 | |
|
Even the greenest hand knows the file name and path. But the test really didn't get the desired result. Try the files in the attachment by the expert upstairs!
Attachments 1.rar (598 bytes) |
|
| Floor15 pengfei | Posted 2006-10-05 08:08 |
| 银牌会员 Posts 485 Credits 1,218 From 湖南.娄底 | |
|
Haha~ It seems another new territory has been discovered. The 1.ini file of the original poster is in Unicode encoding.
This kind of encoded text lines cannot be extracted with for. I changed 1.ini to ANSI code. Please test... Attachments test.rar (546 bytes) |
|
| 1 2 Next |
|
[ Contact the Union admin team -
中国DOS联盟 -
Standard version ] Sponsored by ifanr Inc | © 2001–2023 |