中国DOS联盟论坛

中国DOS联盟

-- 联合DOS 推动DOS 发展DOS --
联盟域名:www.cn-dos.net 论坛域名:www.cn-dos.net/forum
游客 | 登录 | 注册 | 会员 | 搜索 | 中国DOS联盟
中国DOS联盟论坛
现在时间是 2026-08-08 17:19
47,811 主题排行 / 349,895 发帖 / 今日 0 篇 / 48,253 会员排行
DOS批处理 & 脚本技术(批处理室) » set的用法
可打印版本  1,100 / 8
第1楼 xycoordinate 发表于 2008-05-22 21:51
中级用户 发帖 228 积分 493 来自 安徽
set的用法
F:\bdttdb>set str2= 12345

F:\bdttdb>echo %str2%
12345

F:\bdttdb>set str3=-%str2%

F:\bdttdb>echo %str3%
- 12345

如何将str3设置成-12345????
第2楼 HAT 发表于 2008-05-22 21:53
版主 发帖 5,017 积分 9,023
C:\test>set str3=%str3: =%

C:\test>echo %str3%
-12345
第3楼 HAT 发表于 2008-05-22 21:55
版主 发帖 5,017 积分 9,023
set /?

Environment variable substitution has been enhanced as follows:

%PATH:str1=str2%

would expand the PATH environment variable, substituting each occurrence
of "str1" in the expanded result with "str2". "str2" can be the empty
string to effectively delete all occurrences of "str1" from the expanded
output. "str1" can begin with an asterisk, in which case it will match
everything from the beginning of the expanded output to the first
第4楼 bat-zw 发表于 2008-05-22 21:55
金牌会员 发帖 1,276 积分 3,105
set /a set3-=str2
第5楼 zqz0012005 发表于 2008-05-22 22:09
中级用户 发帖 135 积分 297
set /a运算会自动将数字型字符串转成数字,没想到字符串前面含空格也能转换
看到楼上的长见识了
第6楼 slore 发表于 2008-05-23 00:51
铂金会员 发帖 2,478 积分 5,212
set /a str3=~str2+1
第7楼 xycoordinate 发表于 2008-05-23 10:19
中级用户 发帖 228 积分 493 来自 安徽
首先,感谢大家!特别是"HAT"
但是"zw19750516"的set /a str=-%str%,我试验了,在我这个例子中好象不行,报"运算符不存在"???
原来,我也在BAT中使用过,不知道这次为什么???

jgh.txt
15600
15601
15602
15604
15605
15606
15607
15608
15609
15610
15611
15612
15613
15615
15616
15617

text.txt (建议用UE打开!!!)
┃ ┃ ┃ ┃ ┃
┃ 1┃ 10206┃ ┃ ┃
┃小计┃ 1┃ 借 ┃ 7,200.00┃
┃ 1┃ 15600┃ ┃ ┃
┃ 2┃ 15600┃ ┃ ┃
┃小计┃ 2┃ 贷 ┃ 50,000.00┃
┃ 1┃ 15601┃ ┃ ┃
┃ 2┃ 15601┃ ┃ ┃
┃ 3┃ 15601┃ ┃ ┃
┃ 4┃ 15601┃ ┃ ┃
┃ 5┃ 15601┃ ┃ ┃
┃ 6┃ 15601┃ ┃ ┃
┃小计┃ 6┃ 借 ┃ 26,850.00┃
┃ 1┃ 15604┃ ┃ ┃
┃ 2┃ 15604┃ ┃ ┃
┃ 3┃ 15604┃ ┃ ┃
┃ 4┃ 15604┃ ┃ ┃
┃ 5┃ 15604┃ ┃ ┃
┃小计┃ 5┃ 借 ┃ 500.00┃
┃ 1┃ 15605┃ ┃ ┃
┃ 2┃ 15605┃ ┃ ┃
┃ 3┃ 15605┃ ┃ ┃
┃ 4┃ 15605┃ ┃ ┃
┃ 5┃ 15605┃ ┃ ┃
┃小计┃ 5┃ 借 ┃ 47,377.33┃
┃ 1┃ 15606┃ ┃ ┃
┃ 2┃ 15606┃ ┃ ┃
┃ 3┃ 15606┃ ┃ ┃
┃小计┃ 3┃ 借 ┃ 3,320.00┃
┃ 1┃ 15607┃ ┃ ┃
┃ 2┃ 15607┃ ┃ ┃
┃ 3┃ 15607┃ ┃ ┃
┃ 4┃ 15607┃ ┃ ┃
┃小计┃ 4┃ 借 ┃ 6,100.00┃
┃ 1┃ 15608┃ ┃ ┃
┃ 2┃ 15608┃ ┃ ┃
┃ 3┃ 15608┃ ┃ ┃
┃ 4┃ 15608┃ ┃ ┃
┃ 5┃ 15608┃ ┃ ┃
┃ 6┃ 15608┃ ┃ ┃
┃ 7┃ 15608┃ ┃ ┃
┃ 8┃ 15608┃ ┃ ┃
┃ 9┃ 15608┃ ┃ ┃
┃ 10┃ 15608┃ ┃ ┃
┃ 11┃ 15608┃ ┃ ┃
┃ 12┃ 15608┃ ┃ ┃
┃ ┃ ┃ ┃ ┃
┃ ┃ ┃ ┃ ┃
┃ 13┃ 15608┃ ┃ ┃
┃ 14┃ 15608┃ ┃ ┃
┃ 15┃ 15608┃ ┃ ┃
┃ 16┃ 15608┃ ┃ ┃
┃ 17┃ 15608┃ ┃ ┃
┃ 18┃ 15608┃ ┃ ┃
┃ 19┃ 15608┃ ┃ ┃
┃ 20┃ 15608┃ ┃ ┃
┃ 21┃ 15608┃ ┃ ┃
┃ 22┃ 15608┃ ┃ ┃
┃小计┃ 22┃ 贷 ┃ 237,719.00┃
┃ 1┃ 15609┃ ┃ ┃
┃ 2┃ 15609┃ ┃ ┃
┃ 3┃ 15609┃ ┃ ┃
┃ 4┃ 15609┃ ┃ ┃
┃ 5┃ 15609┃ ┃ ┃
┃ 6┃ 15609┃ ┃ ┃
┃ 7┃ 15609┃ ┃ ┃
┃ 8┃ 15609┃ ┃ ┃
┃小计┃ 8┃ 贷 ┃ 9,860.00┃
┃ 1┃ 15610┃ ┃ ┃
┃ 2┃ 15610┃ ┃ ┃
┃ 3┃ 15610┃ ┃ ┃
┃ 4┃ 15610┃ ┃ ┃
┃小计┃ 4┃ 借 ┃ 79,300.00┃
┃ 1┃ 15611┃ ┃ ┃
┃ 2┃ 15611┃ ┃ ┃
┃ 3┃ 15611┃ ┃ ┃
┃小计┃ 3┃ 贷 ┃ 107,562.10┃
┃ 1┃ 15612┃ ┃ ┃
┃ 2┃ 15612┃ ┃ ┃
┃ 3┃ 15612┃ ┃ ┃
┃ 4┃ 15612┃ ┃ ┃
┃ 5┃ 15612┃ ┃ ┃
┃小计┃ 5┃ 贷 ┃ 197,710.00┃
┃ 1┃ 15613┃ ┃ ┃
┃ 2┃ 15613┃ ┃ ┃
┃ 3┃ 15613┃ ┃ ┃
┃小计┃ 3┃ 贷 ┃ 32,500.00┃
┃ 1┃ 15615┃ ┃ ┃
┃ 2┃ 15615┃ ┃ ┃
┃ 3┃ 15615┃ ┃ ┃
┃ 4┃ 15615┃ ┃ ┃
┃ 5┃ 15615┃ ┃ ┃
┃ ┃ ┃ ┃ ┃
┃ ┃ ┃ ┃ ┃
┃ 6┃ 15615┃ ┃ ┃
┃ 7┃ 15615┃ ┃ ┃
┃ 8┃ 15615┃ ┃ ┃
┃ 9┃ 15615┃ ┃ ┃
┃ 10┃ 15615┃ ┃ ┃
┃ 11┃ 15615┃ ┃ ┃
┃ 12┃ 15615┃ ┃ ┃
┃小计┃ 12┃ 贷 ┃ 109,263.00┃
┃ 1┃ 15616┃ ┃ ┃
┃ 2┃ 15616┃ ┃ ┃
┃小计┃ 2┃ 贷 ┃ ┃
┃ 1┃ 15617┃ ┃ ┃
┃ 2┃ 15617┃ ┃ ┃
┃ 3┃ 15617┃ ┃ ┃
┃ 4┃ 15617┃ ┃ ┃
┃ 5┃ 15617┃ ┃ ┃
┃ 6┃ 15617┃ ┃ ┃
┃ 7┃ 15617┃ ┃ ┃
┃ 8┃ 15617┃ ┃ ┃
┃ 9┃ 15617┃ ┃ ┃
┃小计┃ 9┃ 贷 ┃ 4,620.00┃
┃ 1┃ 18605┃ ┃ ┃
┃小计┃ 1┃ 借 ┃ 2,000.00┃
┃合计┃ 95┃ 贷 ┃ 576,586.77┃


test.bat
@echo off
setlocal enabledelayedexpansion
for /f %%x in (jgh.txt) do (
set jgh=%%x
for /f "delims=┃ tokens=1,2,3,4" %%i in (text.txt) do (
set str1=%%i
set str2=%%j
set str3=%%k
set str4=%%l

set str2=!str2: =!
set str3=!str3: =!
set str4=!str4: =!


if !flag! EQU 1 (
if !str1! EQU 小计 (
if "!str3!" EQU "贷" set str4=-!str4!
if "!str4!" EQU "-" set str4=0
@echo !jgh! !str2! !str3! !str4!
)
set flag=
)
if !str2! EQU !jgh! set flag=1
)
)
endlocal

E:\>test.bat
15600 2 贷 -50,000.00
15601 6 借 26,850.00
15604 5 借 500.00
15605 5 借 47,377.33
15606 3 借 3,320.00
15607 4 借 6,100.00
15608 22 贷 -237,719.00
15609 8 贷 -9,860.00
15610 4 借 79,300.00
15611 3 贷 -107,562.10
15612 5 贷 -197,710.00
15613 3 贷 -32,500.00
15615 12 贷 -109,263.00
15616 2 贷 0
15617 9 贷 -4,620.00

[ Last edited by xycoordinate on 2008-5-23 at 10:59 AM ]
第8楼 bat-zw 发表于 2008-05-23 10:25
金牌会员 发帖 1,276 积分 3,105
?????????????????!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
我好像写的是set /a str3-=str2

[ Last edited by zw19750516 on 2008-5-23 at 10:32 AM ]
第9楼 xycoordinate 发表于 2008-05-23 10:59
中级用户 发帖 228 积分 493 来自 安徽
问题找到了!!!
再次感谢大家的无私帮助!!!
关键是%str4%中的数字有","和".",所以set /a时才报"运算符不存在"!!!


test2.bat
@echo off
setlocal enabledelayedexpansion

set hj1=0
set hj2=0

for /f %%x in (jgh.txt) do (
for /f "delims=┃ tokens=1,2,3,4" %%i in (text.txt) do (
set str1=%%i
set str2=%%j
set str3=%%k
set str4=%%l

set str2=!str2: =!
set str3=!str3: =!
set str4=!str4: =!
set str4=!str4:,=!
set str4=!str4:~,-3!


if !flag! EQU 1 (
if !str1! EQU 小计 (
if "!str3!" EQU "贷" set str4=-!str4!
if "!str4!" EQU "-" set str4=0
@echo %%x !str2! !str3! !str4!

set /a hj1+=!str1!
set /a hj2+=!str4!

)
set flag=
)
if !str2! EQU !jgh! set flag=1
)
)

@echo 合计 !hj1! -- !hj2!


E:\>test2.bat
15600 2 贷 -50000
15601 6 借 26850
15604 5 借 500
15605 5 借 47377
15606 3 借 3320
15607 4 借 6100
15608 22 贷 -237719
15609 8 贷 -9860
15610 4 借 79300
15611 3 贷 -107562
15612 5 贷 -197710
15613 3 贷 -32500
15615 12 贷 -109263
15616 2 贷 0
15617 9 贷 -4620
合计 93 -- -585787

[ Last edited by xycoordinate on 2008-5-23 at 12:40 PM ]
[ 联系联盟系统管理团队 - 中国DOS联盟 - 标准版 ]
Sponsored by ifanr Inc | © 2001–2023