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-07-26 10:47
中国DOS联盟论坛 » DOS批处理 & 脚本技术(批处理室) » Seeking help with the issue of if exit View 1,802 Replies 7
Original Poster Posted 2006-10-12 11:37 ·  中国 江西 吉安 遂川县 电信
中级用户
★★
Credits 253
Posts 112
Joined 2006-05-31 11:12
20-year member
UID 56308
Gender Male
Status Offline
Format: if exist file name  command to be executed
If the specified file exists, the condition is true, and the command is run; otherwise, the next sentence is run

If there are two or more commands to be executed, how should they be written?

Another question:
Find a segment of statements to have the following effect.
If 01.txt and 02.txt exist simultaneously under D:\
Then open the contents of 01.txt and 02.txt
Otherwise, delete 01.txt (or 02.txt) when only 01.txt or 02.txt exists

Looking forward to an answer~~~
Floor 2 Posted 2006-10-12 11:53 ·  中国 湖北 荆门 电信
荣誉版主
★★★
Credits 2,013
Posts 718
Joined 2006-02-18 07:07
20-year member
UID 50550
Status Offline
1.

if exist 1.txt if exist 2.txt (start "" 1.txt && start "" 2.txt)

2.
@echo off
dir /a-d 1.txt || goto next
dir /a-d 2.txt || goto next
start "" 1.txt & start "" 2.txt
goto :eof

:next
del 1.txt 2.txt 2>nul
goto :eof

The code has not been tested...
Floor 3 Posted 2006-10-12 11:59 ·  中国 江西 吉安 遂川县 电信
中级用户
★★
Credits 253
Posts 112
Joined 2006-05-31 11:12
20-year member
UID 56308
Gender Male
Status Offline
Thanks. . .
Let's try first.
Floor 4 Posted 2006-10-12 12:22 ·  中国 广东 佛山 广东睿江科技有限公司
荣誉版主
★★★★
batch fan
Credits 5,226
Posts 1,737
Joined 2006-03-10 00:38
20-year member
UID 51697
From 成都
Status Offline
Merge the code for 2F, which should meet the LZ's requirement:

@echo off
if exist 1.txt if exist 2.txt (start "" 1.txt && start "" 2.txt) else (del 1.txt 2.txt 2>nul)
尺有所短,寸有所长,学好CMD没商量。
考虑问题复杂化,解决问题简洁化。
Floor 5 Posted 2006-10-12 12:44 ·  中国 浙江 杭州 电信
初级用户
Credits 34
Posts 12
Joined 2006-09-25 04:45
19-year member
UID 63588
Gender Male
Status Offline
What is the usage of || here? Is it a logical operation?
And what does the '''' in front of 1.txt mean?
Floor 6 Posted 2006-10-12 12:53 ·  中国 广东 佛山 广东睿江科技有限公司
荣誉版主
★★★★
batch fan
Credits 5,226
Posts 1,737
Joined 2006-03-10 00:38
20-year member
UID 51697
From 成都
Status Offline
1、

& command1 & command2 Used to separate multiple commands in a command line. Cmd.exe runs the first command and then the second command.
&& command1 && command2 Used to run the command after the && only if the command before the && is successful. Cmd.exe runs the first command and then runs the second command only if the first command runs successfully.
|| command1 || command2 Used to run the command after the || only if the command before the || fails. Cmd.exe runs the first command and then runs the second command only if the first command fails to run successfully (receives an error code greater than zero).
( ) (command1 & command2) Used to group or nest multiple commands.
; or , command1 parameter1;parameter2 Used to separate command parameters.
Note
The "and" symbol (&), pipe symbol (|), and parentheses () are special characters. When passing them as parameters, you must prefix them with the escape character (^) or quotes.
If a command successfully completes an operation, the command returns a zero (0) exit code or no exit code. For detailed information about exit codes, refer to the Microsoft Windows Resource Kit.

  2、

START




"title" The title displayed in the window title bar.
  ……

When the filename is a path with spaces, you must enclose the path in quotes. At this time, in order to prevent start from treating the path as the title, insert a pair of quotes between start and the path to distinguish the title from the path.
尺有所短,寸有所长,学好CMD没商量。
考虑问题复杂化,解决问题简洁化。
Floor 7 Posted 2006-10-12 12:58 ·  中国 浙江 杭州 电信
初级用户
Credits 34
Posts 12
Joined 2006-09-25 04:45
19-year member
UID 63588
Gender Male
Status Offline
Um, learning. Thanks
Floor 8 Posted 2006-10-12 20:48 ·  中国 江苏 苏州 电信
银牌会员
★★★
Credits 1,181
Posts 533
Joined 2006-08-14 12:54
19-year member
UID 60484
Status Offline

if exist filename.txt (
echo 1
echo 2
echo 3
) else (
echo 4
echo 5
echo 6
)


This format might be more convenient to use.
Forum Jump: