Board logo

标题: 重定向的没有得出想要的结果 [打印本页]

作者: 740011611     时间: 2010-12-24 00:15    标题: 重定向的没有得出想要的结果

下面这段代码得不到想要的结果,但是把>>号前面加上一个空格就可以了,不知道为什么?
echo {>>test.txt
echo if ($2==2) $2=4 >>test.txt
echo else if ($2==1) $2=2 >>test.txt
echo else if ($2==3) $2=5>>test.txt
echo else if ($2==4) $2=3 >>test.txt
echo else $2=1>>test.txt
echo if ($13==1) $13=5>>test.txt
echo else if ($13==2) $13=4>>test.txt
echo else if ($13==3) $13=3>>test.txt
echo else if ($13==4) $13=2>>test.txt
echo else $13=1>>test.txt
echo if ($16==1) $16=5>>test.txt
echo else if ($16==2) $16=4>>test.txt
echo else if ($16==3) $16=3>>test.txt
echo else if ($16==4) $16=2>>test.txt
echo else $16=1>>test.txt
echo if ($19==1) $19=5>>test.txt
echo else if ($19==2) $19=4>>test.txt
echo else if ($19==3) $19=3>>test.txt
echo else if ($19==4) $19=2>>test.txt
echo else $19=1>>test.txt
echo if ($22==1) $22=5>>test.txt
echo else if ($22==2) $22=4>>test.txt
echo else if ($22==3) $22=3>>test.txt
echo else if ($22==4) $22=2>>test.txt
echo else $22=1>>test.txt
echo }>>test.txt

作者: Hanyeguxing     时间: 2010-12-24 01:38
方法1:
@echo off&setlocal ENABLEDELAYEDEXPANSION
(for /f "usebackq delims=" %%a in ("%~f0") do (
    set "a=%%a"
    if defined b echo %%a
    if "!a:~0,3!"==":标签" set b==
))>test.txt
exit
:标签
{
if ($2==2) $2=4
else if ($2==1) $2=2
else if ($2==3) $2=5
else if ($2==4) $2=3
else $2=1
if ($13==1) $13=5
else if ($13==2) $13=4
else if ($13==3) $13=3
else if ($13==4) $13=2
else $13=1
if ($16==1) $16=5
else if ($16==2) $16=4
else if ($16==3) $16=3
else if ($16==4) $16=2
else $16=1
if ($19==1) $19=5
else if ($19==2) $19=4
else if ($19==3) $19=3
else if ($19==4) $19=2
else $19=1
if ($22==1) $22=5
else if ($22==2) $22=4
else if ($22==3) $22=3
else if ($22==4) $22=2
else $22=1
}

作者: Hanyeguxing     时间: 2010-12-24 01:41
方法2:
@echo off&setlocal ENABLEDELAYEDEXPANSION
(
echo {
echo if ($2==2) $2=4
echo else if ($2==1) $2=2
echo else if ($2==3) $2=5
echo else if ($2==4) $2=3
echo else $2=1
echo if ($13==1) $13=5
echo else if ($13==2) $13=4
echo else if ($13==3) $13=3
echo else if ($13==4) $13=2
echo else $13=1
echo if ($16==1) $16=5
echo else if ($16==2) $16=4
echo else if ($16==3) $16=3
echo else if ($16==4) $16=2
echo else $16=1
echo if ($19==1) $19=5
echo else if ($19==2) $19=4
echo else if ($19==3) $19=3
echo else if ($19==4) $19=2
echo else $19=1
echo if ($22==1) $22=5
echo else if ($22==2) $22=4
echo else if ($22==3) $22=3
echo else if ($22==4) $22=2
echo else $22=1
echo }
)>test.txt

作者: Hanyeguxing     时间: 2010-12-24 01:43
方法3:
@echo off
(for /f "delims=" %%a in ('more +3^<"%~f0"') do echo %%a)>test.txt
exit
{
if ($2==2) $2=4
else if ($2==1) $2=2
else if ($2==3) $2=5
else if ($2==4) $2=3
else $2=1
if ($13==1) $13=5
else if ($13==2) $13=4
else if ($13==3) $13=3
else if ($13==4) $13=2
else $13=1
if ($16==1) $16=5
else if ($16==2) $16=4
else if ($16==3) $16=3
else if ($16==4) $16=2
else $16=1
if ($19==1) $19=5
else if ($19==2) $19=4
else if ($19==3) $19=3
else if ($19==4) $19=2
else $19=1
if ($22==1) $22=5
else if ($22==2) $22=4
else if ($22==3) $22=3
else if ($22==4) $22=2
else $22=1
}