|
digitalchina
新手上路
积分 16
发帖 7
注册 2007-7-2
状态 离线
|
『楼 主』:
分割文件 并依据文本内容产生新的文件名? 已完成.
写一段代码打算把文档TEST.LST 分割成 aaaaadb.bvbde.txt
和 %j.%b.txt
:D:D:D
%j %b 分别取自'File . . . . . . . . : , Library . . . . . . : 后的字符串.
以 * * * * * E N D O F L I S T I N G * * * * * 来分割文件.
我写了一小段想截取 %J 和%b 也不能成功, 不知道为什么.
rem @echo off
set fil=
set lib=
cd.>list.txt
setlocal enabledelayedexpansion
for /f "tokens=1* delims=:" %%i in (test.lst) do (
if /i "%%i"=="File . . . . . . . . :" set fil=%%j
if /i "%%i"==Library . . . . . . :" set lib=%%j
)
if "%fil%" neq "" echo !fil!>>list.txt
do ( if "%lib%" neq "" echo !lib!>>list.txt
echo %lib%.%fil%.txt
rem copy test.lst %lib%.%fil%.txt
)
pause
test.lst
---------------------------------------------------------------------------------------
999WWW VVVT50 930101 Programming Development TST - Member list 6/29/07 8:56:59 Page 1
File . . . . . . . . : aaaaadb
Library . . . . . . : bvbde
Member . . . . . . . : *ALL
Type . . . . . . . . : *ALL
Creation Last Changed Deleted
Member Type Date Date Time Records Records Text
---------- ---------- -------- -------- -------- ------- ------- ------------------------------------------------
A00001 CPP 01/12/02 01/18/03 08:00:50 0100005 0000000 MODIFY SEAR
A00002 C 01/13/02 01/16/03 08:00:50 0300000 0000000 modi file
AU0001 PAS 01/14/02 01/16/03 08:00:50 0000100 0000000 SEQUENTIAL READ library
ADDD01 VBS 01/15/02 01/16/03 08:00:50 0000210 0000000 SEARCH
SEAR02 BAT 01/16/02 01/16/03 08:00:50 0000003 0000000 SEARC
GETFILE FIL 01/17/02 01/16/03 08:00:51 0000007 0000000
* * * * * E N D O F L I S T I N G * * * * *
999WWW VVVT50 930101 Programming Development TST - Member list 6/29/07 8:56:59 Page 1
File . . . . . . . . : TTTDBGF
Library . . . . . . : LIBRARY
Member . . . . . . . : *ALL
Type . . . . . . . . : *ALL
Creation Last Changed Deleted
Member Type Date Date Time Records Records Text
---------- ---------- -------- -------- -------- ------- ------- ------------------------------------------------
BBB01 CPP 11/10/93 05/21/03 01:55:00 0000100 0000000 MODIFY SEAR
BBBD02 C 11/10/92 05/21/73 01:55:00 0000100 0000000 modi file
BBB PAS 11/10/95 05/11/83 01:55:00 0000100 0000000 SEQUENTIAL READ library
RRRR VBS 11/10/92 05/03/03 01:55:00 0000000 0000000 SEARCH
ASSSS BAT 11/10/95 03/15/93 01:55:00 0000004 0000000 SEARC
TTT01NF FIL 11/10/95 01/13/02 01:55:00 0000100 0000000 LIBRARY
999WWW VVVT50 930101 Programming Development TST - Member list 6/29/07 8:56:59 Page 2
File . . . . . . . . : TTTDBGF
Library . . . . . . : LIBRARY
Member . . . . . . . : *ALL
Type . . . . . . . . : *ALL
Creation Last Changed Deleted
Member Type Date Date Time Records Records Text
---------- ---------- -------- -------- -------- ------- ------- ------------------------------------------------
TGGG01 CPP 11/10/93 05/21/03 01:55:00 0000195 0000000 MOSAFWEFR
TUROB C 11/10/93 05/21/03 01:55:00 0000195 0000000 moASFEAWEF
BATCH PAS 11/10/93 05/21/03 01:55:00 0000115 0000000 SEQUENTIAL READ library
LIBRARY VBS 11/10/93 05/21/03 01:55:00 0000184 0000000 LIBRARY
FILE BAT 11/10/93 05/21/03 01:55:00 0000184 0000000 FILE
FILE01 FIL 11/10/93 05/21/03 01:55:00 0000117 0000000 FOLE
* * * * * E N D O F L I S T I N G * * * * *
[ Last edited by digitalchina on 2007-7-3 at 09:59 PM ]
|
|
2007-7-2 22:31 |
|
|
digitalchina
新手上路
积分 16
发帖 7
注册 2007-7-2
状态 离线
|
『第
2 楼』:
请指点.
修改了一下可以分离出变量了, 但是 为什么会把文件读完才完成FOR语句?
我打算是读到 LIB FIL 有值了就退出. 但是好像没做用啊.
rem @echo off
set fil=
set lib=
for /f "tokens=1,2 delims=:" %%i in (test.lst) do (
if /i "%%i"== "File . . . . . . . . " if "%fil%"=="" set fil=%%j
if /i "%%i"== " Library . . . . . . " do ( if "%lib%"=="" set lib=%%j
if "%fil%" neq "" if "%lib%" neq "" goto end )
)
)
:end
echo %lib%.%fil%.txt
pause
|
|
2007-7-3 01:02 |
|
|
digitalchina
新手上路
积分 16
发帖 7
注册 2007-7-2
状态 离线
|
『第
3 楼』:
不会去空格了
rem @echo off
set fil=
set lib=
for /f "tokens=1,2 delims=:" %%i in (test.lst) do (
if /i "%%i"== "File . . . . . . . . " set fil=%%j & set strfil=%%j & call set "fil=%%fil%%: =%%"
if /i "%%i"== " Library . . . . . . " ( set lib=%%j & set strlib=%%j & call set "lib=%%lib%%: =%%"
if "%strfil%" neq "" if "%strlib%" neq "" goto end )
)
)
:end
echo %lib%.%fil%.txt
pause
|
|
2007-7-3 02:30 |
|
|
digitalchina
新手上路
积分 16
发帖 7
注册 2007-7-2
状态 离线
|
『第
4 楼』:
搞定了:)
rem @echo off
set fil=
set lib=
for /f "tokens=1,2 delims=:" %%i in (test.lst) do (
if /i "%%i"== "File . . . . . . . . " set fil=%%j & set strfil=%%j & call set "fil=%%strfil: =%%"
if /i "%%i"== " Library . . . . . . " ( set lib=%%j & set strlib=%%j & call set "lib=%%strlib: =%%"
if "%strfil%" neq "" if "%strlib%" neq "" goto end )
)
)
:end
echo %lib%.%fil%.txt
pause
还差文件分割
[ Last edited by digitalchina on 2007-7-3 at 08:26 AM ]
|
|
2007-7-3 08:24 |
|
|
bjsh
银牌会员
积分 2000
发帖 621
注册 2007-1-1
状态 离线
|
『第
5 楼』:
提醒: 可以先输出到 临时文件夹;
遇到 * * * * * E N D O F L I S T I N G * * * * * 时;
把临时文件夹改名为: %lib%.%fil%.txt
顺便贴个awk的解决:
$ awk '/File/{name1=$NF};/Library/{name2=$NF};{if ( $0 ~ /E N D O F L I S T I N G/ ) {print $0>"temp.txt";close("temp.txt"); system("mv temp.txt "name1"."name2".txt");}else {print $0>"temp.txt"} }' 1.txt
|
|
2007-7-3 10:15 |
|
|
digitalchina
新手上路
积分 16
发帖 7
注册 2007-7-2
状态 离线
|
『第
6 楼』:
只差一步了。
getname.bat
------------------------------------------------------------------------------
rem @echo off
copy tmp.lst temp.lst >nul 2>nul
set fil=
set lib=
for /f "tokens=1,2 delims=:" %%i in (temp.lst) do (
if /i "%%i"== "File . . . . . . . . " set fil=%%j & set strfil=%%j & call set "fil=%%strfil: =%%"
if /i "%%i"== " Library . . . . . . " set lib=%%j & set strlib=%%j & call set "lib=%%strlib: =%%"
if "%strfil%" neq "" if "%strlib%" neq "" goto end
)
:end
ren temp.lst %lib%.%fil%.lst >nul 2>nul
del temp.lst >nul 2>nul
del tmp.lst >nul 2>nul
split.bat
------------------------------------------------------------------
rem @echo off
for /f "tokens=1,* delims= " %%a in ('type job.all') do (
set a=%%a
echo %%a %%b>>tmp.lst
if "%%b"=="* * * * E N D O F L I S T I N G * * * * *" call getname
)
现在已经能用SPLIT 正确分割,GETNAME 获得正确的名字, 但是用SPLIT CALL GETNAME 就得不到正确的名字了。
有谁遇到过?
|
|
2007-7-3 10:20 |
|
|
digitalchina
新手上路
积分 16
发帖 7
注册 2007-7-2
状态 离线
|
『第
7 楼』:
终于成功了.
getname.bat
@echo off
copy tmp.lst temp.lst >nul 2>nul
set blank=
set fil=%blank%
set lib=%blank%
for /f "tokens=1,2 delims=:" %%i in (temp.lst) do (
if /i "%%i"== "File . . . . . . . . " set fil=%%j & set strfil=%%j & call set "fil=%%
strfil: =%%"
if /i "%%i"== "Library . . . . . . " set lib=%%j & set strlib=%%j & call set "lib=%%
strlib: =%%"
if "%fil%" neq " " if "%lib%" neq " " goto end
)
:end
ren temp.lst %lib%.%fil%.lst >nul 2>nul
del temp.lst >nul 2>nul
del tmp.lst >nul 2>nul
|
|
2007-7-3 21:50 |
|
|
digitalchina
新手上路
积分 16
发帖 7
注册 2007-7-2
状态 离线
|
『第
8 楼』:
经过SPLIT后,TMP.TXT 已经变化了. 所以GETNAME 要换参数.
|
|
2007-7-3 21:51 |
|