|
xycoordinate
中级用户
积分 493
发帖 228
注册 2007-2-16 来自 安徽
状态 离线
|
『第
16 楼』:
探讨2
str.bat
Quote: | @echo off
setlocal enabledelayedexpansion
for /f "tokens=1,2,3 delims==, " %%i in (%1) do (
set str1=%%i
set str2=%%j
set str3=%%k
set str1
set str2
set str3
) |
|
E:\>str.bat 111.txt
显示:
str1=ncrlog\15600\20070111.txt:COM
str2=MOBILE:13705553216
环境变量 str3 没有定义
str1=ncrlog\15600\20070111.txt:AMOUNT
str2=100.00
环境变量 str3 没有定义
str1=ncrlog\15600\20070111.txt:
str2=MTR
str3=62232754000719668
str1=ncrlog\15600\20070111.txt:
str2=15:04
str3=MTR
"for /f "tokens=1,2,3 delims==, " %%i in (%1) do"
for命令扫描111.txt的1行,赋值一次%%i, %%j, %%k,
结果正好与111new.txt显示顺序对应.
111new.txt
Quote: | DATE TYPE CUSTOM_NO CARD AMOUNT
3 ncrlog\15600\20070111.txt:COM MOBILE:13705553216
4 ncrlog\15600\20070111.txt:AMOUNT 100.00
2 ncrlog\15600\20070111.txt: MTR 62232754000719668
20070111 COM MOBILE 13705553216 62232754000719668 100.00
1 ncrlog\15600\20070111.txt: 15:04 MTR |
|
[ Last edited by xycoordinate on 2007-5-16 at 10:14 PM ]
|
|
2007-3-3 10:20 |
|
|
xycoordinate
中级用户
积分 493
发帖 228
注册 2007-2-16 来自 安徽
状态 离线
|
『第
17 楼』:
探讨3
test.bat
Quote: | @echo off
setlocal enabledelayedexpansion
echo DATE TYPE CUSTOM_NO CARD AMOUNT
for /f "tokens=1,2,3 delims==, " %%i in (%1) do (
rem START
rem A++++++++++++++++++++
if !flag! equ 2 (
echo !name! !TYPE! !CUSTOM_NO! !CARD! !AMOUNT!
set flag=
echo 1 %%i %%j %%k
)
rem A++++++++++++++++++++
rem B++++++++++++++++++++
if !flag! equ 1 (
set CARD=%%k
set flag=2
echo 2 %%i %%j %%k
)
set str=%%i
set var=%%j
rem B++++++++++++++++++++
rem C++++++++++++++++++++
rem MOBILE
if "!var:~0,6!"=="MOBILE" (
set TYPE=!str:~-3,3! MOBILE
set CUSTOM_NO=!var:~-11!
echo 3 %%i %%j %%k
)
rem C++++++++++++++++++++
rem D++++++++++++++++++++
if "!var:~-3,1!"=="." (
set name=%%~ni
set AMOUNT=%%j
set flag=1
echo 4 %%i %%j %%k
)
rem D++++++++++++++++++++
rem END
) |
|
111.txt
Quote: | ncrlog\15600\20070111.txt:COM MOBILE:13705553216
ncrlog\15600\20070111.txt:AMOUNT=100.00
ncrlog\15600\20070111.txt: MTR 62232754000719668
ncrlog\15600\20070111.txt: 15:04 MTR ACCEPTED |
|
E:\>testmiddle.bat 111.txt
Quote: | DATE TYPE CUSTOM_NO CARD AMOUNT
3 ncrlog\15600\20070111.txt:COM MOBILE:13705553216
4 ncrlog\15600\20070111.txt:AMOUNT 100.00
2 ncrlog\15600\20070111.txt: MTR 62232754000719668
20070111 COM MOBILE 13705553216 62232754000719668 100.00
1 ncrlog\15600\20070111.txt: 15:04 MTR |
|
探讨:
for命令
扫描111.txt中的第1行时,test.bat中C部分符合条件,执行C,到END再返回START;
扫描111.txt中的第2行时,test.bat中D部分符合条件,执行D,到END再返回START;
扫描111.txt中的第3行时,test.bat中B部分符合条件,执行B,到END再返回START;
扫描111.txt中的第4行时,test.bat中A部分符合条件,执行A,到END再返回START.
for命令是一行一行地扫描文本文件,for命令体(START到END之间)中的语句是按顺序执行.
[ Last edited by xycoordinate on 2007-5-16 at 06:23 PM ]
|
|
2007-3-4 00:07 |
|
|
xycoordinate
中级用户
积分 493
发帖 228
注册 2007-2-16 来自 安徽
状态 离线
|
『第
18 楼』:
修改一点
增加对ACCEPTED的判断!
test2.bat
Quote: | @echo off
setlocal enabledelayedexpansion
echo DATE TYPE CUSTOM_NO CARD AMOUNT
for /f "tokens=1,2,* delims==, " %%i in (%1) do (
set str1=%%i
set str2=%%j
set str3=%%k
if !flag! equ 2 (
if "!str3:~-8!"=="ACCEPTED" (
echo !name! !TYPE! !CUSTOM_NO! !CARD! !AMOUNT!
)
set flag=
)
if !flag! equ 1 (
set CARD=!str3!
set flag=2
)
rem MOBILE
if "!str2:~0,6!"=="MOBILE" (
set TYPE=!str1:~-3,3! MOBILE
set CUSTOM_NO=!str2:~-11!
if "!TYPE:~0,3!"=="UNI" set CUSTOM_NO=!str3!
)
rem TELEPHONE
if "!str1:~-17,9!"=="TELEPHONE" (
set TYPE=TELEPHONE
set CUSTOM_NO=!str1:~-7!
)
rem WATER
if "!str1:~-14,5!"=="WATER" (
set TYPE=WATER
set CUSTOM_NO=!str1:~-8!
)
rem ELEC
if "!str1:~-5,4!"=="ELEC" (
set TYPE=ELEC
set CUSTOM_NO=!str2:~-8!
)
rem GAS
if "!str1:~-4,3!"=="GAS" (
set TYPE=GAS
set CUSTOM_NO=!str2:~-10!
)
if "!str2:~-3,1!"=="." (
set name=%%~ni
set AMOUNT=!str2!
set flag=1
)
rem GAS AMOUNT format: "**.*"
if "!str2:~-2,1!"=="." (
set name=%%~ni
set AMOUNT=!str2!
set flag=1
)
) |
|
123.txt
Quote: | ncrlog\15600\20070111.txt:COM MOBILE:13715553216
ncrlog\15600\20070111.txt:AMOUNT=100.00
ncrlog\15600\20070111.txt: MTR 62232754001719668
ncrlog\15600\20070111.txt: 15:04 MTR
ncrlog\15600\20070109.txt:TELEPHONE:2202834
ncrlog\15600\20070109.txt:AMOUNT=73.00
ncrlog\15600\20070109.txt: MTR 6228790915000112760
ncrlog\15615\20070104.txt:UNI MOBILE: 13305555353
ncrlog\15615\20070104.txt:AMOUNT=100.00
ncrlog\15615\20070104.txt: MTR 6228790915000010566
ncrlog\15615\20070104.txt: 09:26 MTR ACCEPTED
ncrlog\15615\20070112.txt:WATER:10213819
ncrlog\15615\20070112.txt:AMOUNT=9.60
ncrlog\15615\20070112.txt: MTR 6228791915000010211 |
|
test2.bat 123.txt
Quote: | DATE TYPE CUSTOM_NO CARD AMOUNT
20070104 UNI MOBILE 13305555353 6228790915000010566 100.00 |
|
如果用test.bat执行
test.bat
Quote: | @echo off
setlocal enabledelayedexpansion
echo DATE TYPE CUSTOM_NO CARD AMOUNT
for /f "tokens=1,2,3 delims==, " %%i in (%1) do (
if !flag! equ 2 (
echo !name! !TYPE! !CUSTOM_NO! !CARD! !AMOUNT!
set flag=
)
if !flag! equ 1 (
set CARD=%%k
set flag=2
)
set str=%%i
set var=%%j
rem MOBILE
if "!var:~0,6!"=="MOBILE" (
set TYPE=!str:~-3,3! MOBILE
set CUSTOM_NO=!var:~-11!
if "!TYPE:~0,3!"=="UNI" set CUSTOM_NO=%%k
)
rem TELEPHONE
if "!str:~-17,9!"=="TELEPHONE" (
set TYPE=TELEPHONE
set CUSTOM_NO=!str:~-7!
)
rem WATER
if "!str:~-14,5!"=="WATER" (
set TYPE=WATER
set CUSTOM_NO=!str:~-8!
)
rem ELEC
if "!str:~-5,4!"=="ELEC" (
set TYPE=ELEC
set CUSTOM_NO=!var:~-8!
)
rem GAS
if "!str:~-4,3!"=="GAS" (
set TYPE=GAS
set CUSTOM_NO=!var:~-10!
)
if "!var:~-2,1!"=="." (
set name=%%~ni
set AMOUNT=%%j
set flag=1
)
if "!var:~-3,1!"=="." (
set name=%%~ni
set AMOUNT=%%j
set flag=1
)
) |
|
test.bat 123.txt
结果:
Quote: | DATE TYPE CUSTOM_NO CARD AMOUNT
20070111 COM MOBILE 13715553216 62232754001719668 100.00
20070109 TELEPHONE 2202834 6228790915000112760 73.00
20070104 UNI MOBILE 13305555353 6228790915000010566 100.00 |
|
如果,不做ACCEPTED条件的判断,没有ACCEPTED的将不显示!!!
特别地交易2(TELEPHONE)与交易3(WATER)的结果就不同!
而且如果将2,3位置颠倒,显示的结果就会不同!!!
[ Last edited by xycoordinate on 2007-3-4 at 08:25 AM ]
|
|
2007-3-4 07:56 |
|
|
minmin888
初级用户
积分 127
发帖 62
注册 2007-4-19
状态 离线
|
『第
19 楼』:
这样写不是更好理解吗!!!
@echo off
setlocal enabledelayedexpansion
echo DATE CARD AMOUNT
for /f "tokens=1,2* delims== " %%i in (test.txt) do (
set var=%%j
if "!var:~-3,1!"=="." (
set name=%%~ni
set AMOUNT=%%j
set flag=1
)
if !flag! equ 1 (
set CARD=%%k
set CARD=!CARD:~0,19!
set flag=2
)
if !flag! equ 2 (
echo !name! !CARD! !AMOUNT!
set flag=
)
)
pause
|
|
2007-5-7 21:34 |
|
|
xycoordinate
中级用户
积分 493
发帖 228
注册 2007-2-16 来自 安徽
状态 离线
|
『第
20 楼』:
19楼,讲得对"上面的写法理解是不大好",不过只是简单地改变上下顺序是不行的!需要修改内容!
而且是"namejm"斑竹帮忙想出来,我就只好尊重他的写法了!
test.txt
ncrlog\15600\20070111.txt:COM MOBILE:13705553216
ncrlog\15600\20070111.txt:AMOUNT=100.00
ncrlog\15600\20070111.txt: MTR 62232754000719668
ncrlog\15600\20070111.txt: 15:04 MTR ACCEPTED
执行你的bat,得到的是:
DATE CARD AMOUNT
20070111 100.00
for扫描test.txt,第1行不满足,pass!
只有test.txt第2行中,
ncrlog\15600\20070111.txt:AMOUNT=100.00
满足for命令中条件,
if "!var:~-3,1!"=="." (
set name=%%~ni
set AMOUNT=%%j
set flag=1
)
for命令顺序执行下面的行,显示出来,
但是,for继续扫描第3,4行,不满足条件,所以,不能取到!CARD!的值!
修改19楼的bat文件
test.bat
Quote: | @echo off
setlocal enabledelayedexpansion
echo DATE CUSTOM_NO CARD AMOUNT
for /f "tokens=1,2* delims== " %%i in (%1) do (
set sub=%%i
set var=%%j
if "!sub:~-3,3!"=="COM" (
set TYPE=COM
set CUSTOM_NO=!var:~7!
)
if "!var:~-3,1!"=="." (
set name=%%~ni
set AMOUNT=%%j
)
if "%%j"=="MTR" (
set CARD=%%k
set CARD=!CARD:~0,19!
set flag=1
)
if "%%k"=="MTR ACCEPTED" (
set flag=2
)
if !flag! equ 2 (
echo !name! !CUSTOM_NO! !CARD! !AMOUNT!
set flag=
)
)
pause |
|
执行
test.bat test.txt
得到:
DATE CUSTOM_NO CARD AMOUNT
20070111 13705553216 62232754000719668 100.00
请按任意键继续. . .
[ Last edited by xycoordinate on 2007-5-16 at 10:40 PM ]
|
|
2007-5-16 20:16 |
|
|