返回列表 发帖

[ 新手练习题 3 ] 批处理判断目录存在与否

[ 新手练习题 3 ] 判断目录存在与否
判断 C:\ 盘是否存在 WINDOWS 目录。如果存在,则用资源管理器打开目录。
不存在则创建此目录并打开。

目的:掌握判断目录存在与否的方法,学会调用程序打开目录。

要求:方法不限。

评分:代码可读性 1 分;
   首个新方法 4 分,第二个 3 分,依次类推,最少 2 分;
   一人可多种方法,新方法追加 2 分,已经出现过的方法追加 1 分。
批处理之家论坛官方 QQ 群 :【当前人数/人数上限】【2009-07-08】
群①:43011867(181/200);群②:(暂缺数据);群③:66165582(120/200)。

判断 C:\ 盘是否存在 WINDOWS 目录。如果存在,则用资源管理器打开目录。
不存在则创建此目录并打开。
@echo off
set path="C:\windows"
if exist %path% explorer %path%
if not exist %path% md %path%
start explorer %path%COPY

TOP

@echo off
set specfolder=%1
if not exist "%specfolder%" (
md "%specfolder%"
)
start "%specfolder%" "%specfolder%"COPY

TOP

@echo off
if exist c:\windows explorer c:\windows else md c:\windows

TOP

@echo off
if exist D:\Windows (
   explorer D:\Windows
) else (
    md D:\Windows
)
pause

TOP

@echo off
set diretory=c:\windows
if exist %diretory% (explorer %diretory%) else (md %diretory%&explorer %diretory%)COPY

TOP

本帖最后由 wutarnow 于 2015-10-20 15:46 编辑
@echo off
md c:\windows 2>nul & start c:\windows\COPY

TOP

@echo off
set str=c:\windows  
if exist %str% (start explorer.exe) else (cd /d c:\&md windows)
pause

TOP

方法一
if exist c:\windows\ (
echo c:\windows存在
start c:\windows\ ) else (
echo c:\windows\不存在
echo 开始创建c:\windows\
md c:\windows\
start c:\windows\ )
pause>nulCOPY
方法二
cd /d d:\windows&&echo 目录存在||md d:\windows
explorer d:\windows
pause>nulCOPY

TOP

回复 80# zzw8822


两个建议

1. 在不确定 c:\windows 是否存在的情况下就直接 cd /d 没啥意义
2. 两个 if 命令可以合并成一个 if ... else ...
if exist c:\windows\ (
    explorer c:\windows
) else (
    md c:\windows
)COPY
Talk is cheap. Show me the code.
没事不要瞎扯淡,有能耐就把代码贴出来给我看。

TOP

@echo off
cd /d c:\windows
if exist c:\windows explorer c:\windows
if not exist c:\window  md c:\windowsCOPY

TOP

试试
@echo off
if not exist F:\windows\ (md F:\windows\&start F:\windows\) else
(explorer F:\windows\)[color=LemonChiffon][/color]COPY

TOP

顺便出一个问题,如何用 if exist 判断 “D:\Program Files” 是文件还是目录?。。。

当路径中不含有空格时,用 if exist 路径 if exist 路径\nul (echo “路径”是目录) else “路径”是文件
解释执行的脚本语言与编译语言还是有很大差别的。。。

TOP

本帖最后由 battab 于 2014-2-19 17:52 编辑

回复 1# wxcute
@echo off
if exist c:\windows (start c:\windows) else (md c:\windows&start c:\windows)
pauseCOPY
学习中

TOP

@echo off
if exist c:\windows (
if exist c:\windows\nul.ext (
explorer.exe c:\windows
goto :eof
))
if exist c:\windows ren c:\windows windows_bak
md c:\windows
explorer.exe c:\windowsCOPY

TOP

返回列表