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-08-29 07:10
中国DOS联盟论坛 » DOS批处理 & 脚本技术(批处理室) » Split a larger text with a certain regular pattern into multiple smaller text files? View 1,292 Replies 5
Original Poster Posted 2007-03-29 08:25 ·  中国 四川 遂宁 电信
中级用户
★★
Credits 278
Posts 103
Joined 2006-10-21 21:08
19-year member
UID 67562
Gender Male
Status Offline
I have a relatively large text as follows:
//@Function name: abs
//Function prototype: int abs(int x);
//Function purpose: find the absolute value of integer x
//Function return: calculation result
//Parameter description:
//Header file: <math.h>,<stdlib.h>
//Usage example:
#include <stdio.h>
#include <math.h>
int main()
{
int number=-1234;
printf("number: %d absolute value: %d",number,abs(number));
return 0;
}


//@Function name: fabs
//Function prototype: double fabs(double x);
//Function purpose: find the absolute value of x.
//Function return: calculation result
//Parameter description:
//Header file: <math.h>
//Usage example:
#include <stdio.h>
#include <math.h>
int main()
{
float number=-1234.0;
printf("number: %f absolute value: %f",number,fabs(number));
return 0;
}




//@Function name: sin
//Function prototype: double sin(double x);
//Function purpose: calculate the value of sinx. Sine function
//Function return: calculation result
//Parameter description: the unit is radians
//Header file: <math.h>
//Usage example:
#include <stdio.h>
#include <math.h>
int main()
{
double result,x=0.5;
result=sin(x);
printf("The sin() of %lf is %lf",x,result);
return 0;
}
.........

I want to use the text before each "//@Function name" as the boundary, and split each C-language function description into a separate text file named after the function name.
Since there are quite a lot of them, I want to do it with a batch file, but I don't know how to start. Hope an expert can give me some pointers?

[ Last edited by zzhh612 on 2007-3-29 at 09:49 AM ]
Floor 2 Posted 2007-03-29 12:01 ·  中国 辽宁 本溪 联通
银牌会员
★★★
Credits 1,212
Posts 464
Joined 2006-12-13 21:11
19-year member
UID 73417
Gender Male
Status Offline
Floor 3 Posted 2007-03-29 14:54 ·  中国 四川 遂宁 电信
中级用户
★★
Credits 278
Posts 103
Joined 2006-10-21 21:08
19-year member
UID 67562
Gender Male
Status Offline
The VBScript from the poster above is pretty good. I tried it, and it works. But I wonder whether this kind of thing can be done with a DOS batch file. DOS is more fundamental, and I want to learn through actually using it. Thanks.

[ Last edited by zzhh612 on 2007-3-29 at 03:03 PM ]
Floor 4 Posted 2007-03-29 23:52 ·  中国 河北 廊坊 三河市 移动
金牌会员
★★★★
Credits 2,725
Posts 1,160
Joined 2006-09-23 12:00
19-year member
UID 63486
From 河北廊坊
Status Offline
@echo off&setlocal enabledelayedexpansion
for /f "tokens=1*" %%i in (test.txt) do (
set var=%%i
set str=%%j
if "!var:~3,4!"=="函数名称" (set lxm=%%j&>>!lxm!.txt echo !var! !str!
) else (
>>!lxm!.txt echo !var! !str!
)
)

The format is changed slightly, mainly the spaces.
三人行,必有吾师焉。 学然后知不足,教然后知困,然后能自强也。
Floor 5 Posted 2007-03-30 03:01 ·  中国 河北 廊坊 三河市 移动
金牌会员
★★★★
Credits 2,725
Posts 1,160
Joined 2006-09-23 12:00
19-year member
UID 63486
From 河北廊坊
Status Offline
Posting the code written by brother 9527 on his behalf; it matches the code format the OP wants very well.
@echo off&setlocal enabledelayedexpansion
for /f "tokens=1* delims=@" %%a in (test.txt) do (
if "%%a" equ "//" (
call:P "%%b"
) else (
>>!a! echo %%a%%b
)
)
goto:eof

:P
for /f "tokens=1* delims=: " %%x in (%1) do set a=%%y.txt&>>!a! echo %%a%%b
goto:eof
三人行,必有吾师焉。 学然后知不足,教然后知困,然后能自强也。
Floor 6 Posted 2007-03-30 10:01 ·  中国 四川 遂宁 电信
中级用户
★★
Credits 278
Posts 103
Joined 2006-10-21 21:08
19-year member
UID 67562
Gender Male
Status Offline
The code from jmz573515 and the code posted on behalf of ccwan achieved the same effect.
Really different tunes played with equal skill. Looks like I still have a long way to go in learning: "The road ahead is long and has no ending; yet high and low I will search with my will unbending...
Forum Jump: