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-02 07:09
中国DOS联盟论坛 » DOS批处理 & 脚本技术(批处理室) » [Closed] IF, ELSE View 3,021 Replies 10
Original Poster Posted 2006-10-14 00:53 ·  中国 上海 宝山区 电信
初级用户
★★
Credits 153
Posts 68
Joined 2006-10-12 22:25
19-year member
UID 65507
From 上海
Status Offline
I'm a new arrival, please take care, hehe, just groping...

@echo off
fc 1.txt 2.txt>3.txt
setlocal enabledelayedexpansion
for /f %%a in (3.txt) do set file1=%%a !file1!
if %file%==""
(
goto end
)
else
(
copy test.xls e:\备份.xls
)
:end

[ Last edited by uemuem on 2006-10-14 at 01:17 ]
个人主页-找餐厅:http://www.canting.cc
Floor 2 Posted 2006-10-14 01:02 ·  中国 甘肃 平凉 电信
金牌会员
★★★★
Credits 4,103
Posts 1,744
Joined 2006-01-20 13:00
20-year member
UID 49241
Gender Male
From 甘肃.临泽
Status Offline
for /f "tokens=*" %%a in (3.txt) do copy %%a e:\备份.xls

Is this enough? Why make it so complicated?
Floor 3 Posted 2006-10-14 01:05 ·  中国 云南 昆明 电信
初级用户
Credits 32
Posts 14
Joined 2006-10-09 07:50
19-year member
UID 65045
Status Offline
Does batch processing have if else? I think this one looks like the language of PASAL
Floor 4 Posted 2006-10-14 01:14 ·  中国 四川 成都 移动
荣誉版主
★★★★
batch fan
Credits 5,226
Posts 1,737
Joined 2006-03-10 00:38
20-year member
UID 51697
From 成都
Status Offline
if %file%=="" is a syntax format error. The correct one should be if ""%file%""=="" Pay attention to adding quotes.
尺有所短,寸有所长,学好CMD没商量。
考虑问题复杂化,解决问题简洁化。
Floor 5 Posted 2006-10-14 01:22 ·  中国 上海 宝山区 电信
初级用户
★★
Credits 153
Posts 68
Joined 2006-10-12 22:25
19-year member
UID 65507
From 上海
Status Offline
个人主页-找餐厅:http://www.canting.cc
Floor 6 Posted 2006-10-14 01:47 ·  中国 浙江 衢州 电信
银牌会员
★★★
Credits 1,270
Posts 548
Joined 2004-05-31 00:00
22-year member
UID 25754
Gender Male
Status Offline
If the IF command is mastered correctly, it can be very useful.
Floor 7 Posted 2006-10-14 01:48 ·  中国 上海 宝山区 电信
初级用户
★★
Credits 153
Posts 68
Joined 2006-10-12 22:25
19-year member
UID 65507
From 上海
Status Offline
Actually, I still don't quite understand. Can you explain it in more detail?

I still get a syntax error like this:

@echo off
setlocal enabledelayedexpansion
for /f %%a in (1.txt) do set file1=%%a !file1!
for /f %%a in (2.txt) do set file2=%%a !file2!
if "%file1%"=="%file2%"(
goto end
)else(
copy test.xls e:\备份.xls
)
:end
个人主页-找餐厅:http://www.canting.cc
Floor 8 Posted 2006-10-14 06:14 ·  中国 四川 成都 移动
荣誉版主
★★★★
batch fan
Credits 5,226
Posts 1,737
Joined 2006-03-10 00:38
20-year member
UID 51697
From 成都
Status Offline
If there is still a syntax error, there are probably two places where the problem occurs:

1. The format of the else part is incorrect. The correct format should be if condition (command1) else (command2). In the top floor, the else is not on the same line as the if; in the 7th floor, the parentheses are connected with else; there is also a problem of writing parentheses and statements together in if "%file1%"=="%file2%"(;
2. The sentence if "%file1%"=="%file2%" may also have a problem. Because the content extracted by your %file1% and %file2% are both the content in the file. If there are sensitive characters under CMD in the file content, this sentence will have an error.
尺有所短,寸有所长,学好CMD没商量。
考虑问题复杂化,解决问题简洁化。
Floor 9 Posted 2006-10-16 22:07 ·  中国 上海 宝山区 电信
初级用户
★★
Credits 153
Posts 68
Joined 2006-10-12 22:25
19-year member
UID 65507
From 上海
Status Offline
个人主页-找餐厅:http://www.canting.cc
Floor 10 Posted 2006-10-16 22:29 ·  中国 四川 南充 电信
超级版主
★★★★
我爱DOS
Credits 5,310
Posts 2,044
Joined 2005-09-26 12:00
20-year member
UID 42843
Gender Male
From 四川南充
Status Offline
Originally posted by namejm at 2006-10-14 01:14:
  if %file%=="" is a syntax format error. The correct one should be if ""%file%"=="" . Please note to add quotes.

if %file%=="" is not a syntax format error. If the value of variable file is "", the condition is established.
The mistake is (not adding












Last edited by qwe1234567 on 2006-10-16 at 22:37 ]
Floor 11 Posted 2006-10-17 04:48 ·  中国 湖北 武汉 电信
版主
★★★★★
Credits 11,386
Posts 4,938
Joined 2006-07-23 17:10
20-year member
UID 59080
Status Offline

What the above moderator said is extremely true.

A space is an effective default separator between commands, between commands and various symbols, and between commands and parameters. If you omit the space, cmd.exe will think that the written-together content is a single statement or command, and then cmd.exe cannot interpret or execute it, thus an error will occur. Therefore, when writing batch scripts or commands in the command line, you must pay attention to the writing norms. Especially novice friends (of course, I am also one ~: ) ) should pay attention to this point.
Forum Jump: