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-31 23:33
中国DOS联盟论坛 » DOS批处理 & 脚本技术(批处理室) » How to determine whether a parameter is a file or a directory View 2,812 Replies 5
Original Poster Posted 2006-06-03 20:17 ·  中国 辽宁 大连 电信
初级用户
Credits 110
Posts 27
Joined 2006-06-03 17:25
20-year member
UID 56491
Status Offline
In batch processing, when operating on a file passed in as an argument, if the file doesn't exist but the directory does, an error will occur during operation. So I want to make a judgment. My operation is to use if exist path\nul, but when there are long file names, it can't be judged. How should I do it? How to judge whether a parameter passed to the batch is a file name or a directory name?
Floor 2 Posted 2006-06-03 20:25 ·  中国 山西 临汾 中移铁通
元老会员
★★★★
Batchinger
Credits 4,432
Posts 1,512
Joined 2002-10-18 00:00
23-year member
UID 19
Gender Male
Status Offline
关于wentimao的回复:

提问时请注明你的系统环境(DOS/98/XP)和脚本环境(COMMAND/CMD),如果将这些参数写在你的用户签名中,就无需每次都转发这些数据。

在CMD中判断文件或目录是否存在,一般不再使用NUl了。

判断是否存在目录:if exist "%~f1\" ...
判断是否存在文件:if exist "%~f1" if not exist "%~f1\"...

The reply to wentimao:

Please indicate your system environment (DOS/98/XP) and script environment (COMMAND/CMD) when asking a question. If you write these parameters in your user signature, you don't need to repost these data every time.

In CMD, to judge the existence of a file or directory, NUl is generally not used anymore.

To judge whether a directory exists: if exist "%~f1\" ...
To judge whether a file exists: if exist "%~f1" if not exist "%~f1\"...
※ Batchinger 致 Bat Fans:请访问 批处理编程的异类 ,欢迎交流与共享批处理编程心得!
Floor 3 Posted 2006-06-06 19:23 ·  中国 山西 运城 中移铁通
元老会员
★★★★
Batchinger
Credits 4,432
Posts 1,512
Joined 2002-10-18 00:00
23-year member
UID 19
Gender Male
Status Offline
Re wentimao:

Regarding the existence judgment issue of the same-named directory and file you mentioned in other topics, the reply is as follows:

1. In CMD and COMMAND, it is very rare to have the situation where the directory and file have the same name and path. This is not allowed by the file system rules of DOS/Windows; if it really occurs, it should be a problem of the system, and it should be solved by the system itself, not by our code to be compatible with this problem.

2. If you really want to judge "whether the file with the same name exists when the directory exists", you can use (but it is not recommended) the following code:

(for /f "usebackq" in ("%~f1") do ...)2>nul
※ Batchinger 致 Bat Fans:请访问 批处理编程的异类 ,欢迎交流与共享批处理编程心得!
Floor 4 Posted 2010-12-04 15:58 ·  中国 广东 东莞 电信
新手上路
Credits 14
Posts 13
Joined 2010-12-01 15:41
15-year member
UID 178657
Gender Male
Status Offline
I want to ask: How to know whether your script environment is COMMAND or CMD?
Floor 5 Posted 2010-12-04 16:28 ·  中国 吉林 延边朝鲜族自治州 延吉市 电信
银牌会员
★★★
正在学习中的菜鸟...
Credits 1,039
Posts 897
Joined 2009-03-01 15:34
17-year member
UID 140302
Gender Male
From 在地狱中仰望天堂
Status Offline
Under what system does it run? Is it run directly in cmd or in a batch file?
Floor 6 Posted 2010-12-04 17:06 ·  中国 广东 东莞 电信
新手上路
Credits 14
Posts 13
Joined 2010-12-01 15:41
15-year member
UID 178657
Gender Male
Status Offline
Originally posted by Hanyeguxing at 2010-12-4 16:28:
Under what system does it run?
Is it run directly in cmd or in a batch script?

Thank you very much for your reply,

I thought these posts were several years old. Hehe

Finally saw someone online Handshake...
Forum Jump: