批处理之家's Archiver

wxcute 发表于 2009-5-30 21:08

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

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

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

要求:方法不限。

评分:代码可读性 1 分;
   首个新方法 4 分,第二个 3 分,依次类推,最少 2 分;
   一人可多种方法,新方法追加 2 分,已经出现过的方法追加 1 分。

yslyxqysl 发表于 2009-5-30 21:20

[color=black][color=white][code]Dir C:\ /a:d /b 2>nul|Findstr /i "^Windows$">nul||md C:\Windows&Explorer C:\Windows[/code][/color]或者[color=white][code](If Not Exist C:\Windows\nul md C:\Windows)&Explorer C:\Windows[/code][/color]最直接的[/color][color=white][code]md C:\Windows 2>nul&Explorer C:\Windows[/code][/color]

[[i] 本帖最后由 yslyxqysl 于 2009-5-30 21:27 编辑 [/i]]

stuqx 发表于 2009-6-3 10:13

[code]@echo off
cd /d c:\windows
if %errorlevel% equ 1 (md c:\windows)
explorer c:\windows[/code]这样应该可以了吧

flyzr 发表于 2009-6-5 13:24

[code]@echo off

if exist c:\windows (
explorer c:\windows
) else (
md c:\windows)

pause >nul [/code]

[[i] 本帖最后由 flyzr 于 2009-6-5 13:27 编辑 [/i]]

keen 发表于 2009-6-5 17:11

[code]@echo off
explorer.exe c:\windows||md c:\windows[/code]

[[i] 本帖最后由 keen 于 2009-6-5 17:19 编辑 [/i]]

wangshuping42 发表于 2009-6-8 23:28

[code]

@echo off
if exist c:\windows (goto open) else (
md c:\windows
  )
:open
start c:\windows
exit

[/code]

rwxyxy 发表于 2009-6-10 13:35

[code]@echo off
cd c:\windows 2>nul
if errorlevel 1 ( del c:\windows 2>nul & md c:\windows )
explorer c:\windows
pause>nul[/code]这个如何?

quake_wu 发表于 2009-6-12 10:30

[code]@echo off
dir /ad /b c:\ |findstr "windows" &&explorer c:\windows ||md c:\windows[/code]请问一下为什么我这段代码直接在命令行下运行没有异常
放到bat里执行就会出现一点异常,异常就是如果存在windows 它还是会去执行 md c:\windows

[[i] 本帖最后由 quake_wu 于 2009-6-12 10:32 编辑 [/i]]

q625805390 发表于 2009-7-27 22:06

@echo off
if exist c:windows (start c:windows) else (md c:windows)
pause>nul

lee 发表于 2009-8-3 00:36

[code]@echo off
for %%i in ("c:\windows") do (
if exist %%i (explorer %%i) else (
md %%i&&explorer %%i
))
pause[/code]


谢谢LZ指导!!

[[i] 本帖最后由 lee 于 2009-8-4 14:11 编辑 [/i]]

nopr 发表于 2009-8-3 10:05

[code]@echo off
set str=c:\windows
if exist %str% (c:\windows\explorer.exe %str%) else md %str%&c:\windows\explorer.exe %str%
pause[/code]

[[i] 本帖最后由 nopr 于 2009-8-3 10:25 编辑 [/i]]

nopr 发表于 2009-8-6 16:55

简单点的

@echo off
cd \&md windows 2>nul&start windows
goto :eof

hs4814 发表于 2009-8-9 21:01

[code]@echo off
dir c:\windows>nul
if  errorlevel 1 ( echo 此目录不存在将建立&md c:\windows\ &explorer c:\windows)   else  (echo 存在此目录&explorer c:\windows)  
pause>nul[/code]
请楼主检查

[[i] 本帖最后由 hs4814 于 2009-8-9 21:31 编辑 [/i]]

quake_wu 发表于 2009-8-11 15:17

[quote]原帖由 [i]quake_wu[/i] 于 2009-6-12 10:30 发表 [url=http://bbs.bathome.net/redirect.php?goto=findpost&pid=31361&ptid=4715][img]http://bbs.bathome.net/images/common/back.gif[/img][/url]
@echo off
dir /ad /b c:\ |findstr "windows" &&explorer c:\windows ||md c:\windows请问一下为什么我这段代码直接在命令行下运行没有异常
放到bat里执行就会出现一点异常,异常就是如果存在windows 它还是会去执 ... [/quote]

感谢wxcute版主的指导
我重新调整了code
[code]
dir /ad /b c:\ |findstr /IX "windows" &&start explorer c:\windows ||md c:\windows[/code]

wayaoqiang 发表于 2009-8-12 17:29

[code]
@echo off
set pat=c:\WINDOWS
if exist %pat% (start %pat%) else (md %pat%&&start %pat%)
[/code]

[[i] 本帖最后由 wayaoqiang 于 2009-8-17 21:01 编辑 [/i]]

atsivsucks 发表于 2009-8-19 08:51

[code]
@echo off

if not exist c:\windows (
        md c:\windows
)

explorer c:\windows

pause
[/code]

[[i] 本帖最后由 atsivsucks 于 2009-8-19 08:56 编辑 [/i]]

msconfig 发表于 2009-8-21 16:02

@echo off
if exist C:\WINDOWS (goto a)
else (
md C:\WINDOWS)
:a
explorer C:\WINDOWS
pause>nul
有个问题不太明白。上面的代码中,如果存在C:\WINDOWS结果正确。若是不存在呢。新建一个后我没有写代码用explorer打开。却也执行打开了。想不明白。

[[i] 本帖最后由 msconfig 于 2009-8-21 16:15 编辑 [/i]]

lianfayong 发表于 2009-8-26 18:15

@echo off
if exist \windows\nul explorer \windows
md windows & start \windows
pause

cs19860814 发表于 2009-8-27 08:17

@echo off dir c:\ WINDOWS>nul 2>nul if errorlevel neq 0 ( md C:\WINDOWS ) esle ( explorer C:\WINDOWS ) pause>nul

DXSX 发表于 2009-9-17 08:58

这里面涉及到一个问题:
1、如果WINDOWS 是以一个文件的形式存在呢?
2、判断文件夹是否存在应该用  IF EXIST C:\WINDOWS\NUL

dominater 发表于 2009-10-13 00:06

[code]@echo off
if exist c:\Windows (
echo 目录已存在,准备打开...) else (
choice /c YN /m 目录不存在,现在创建?是选Y,否选N
if errorlevel 2 goto end
if errorlevel 1 goto Y
:Y
md c:\Windows
)
start c:\Windows
:end
pause[/code]

samble 发表于 2009-10-15 21:29

方法一代码:
[code]md c:\windows 2>NUL & explorer c:\windows[/code]

方法二:
用 IF 语句判断[code]if exist c:\windows\nul (explorer c:\windows) else (
md c:\windows & explorer c:\windows)[/code]
之所以用 explorer 而不用 start ,是因为我发现用 explorer 打开目录速度快点。

[[i] 本帖最后由 samble 于 2009-10-15 21:48 编辑 [/i]]

pumahxh 发表于 2009-11-10 18:59

还是新手,看起来我的方法很呆板。[code]@echo off
if exist c:\windows\nul (explorer c:\windows
   ) else (md c:\windows & explorer c:\windows)
pause>nul[/code]
刚开始没有考虑windows可能为文件,后来看到大家提示,学习了。
楼上的方法一,很经典!

[[i] 本帖最后由 pumahxh 于 2009-11-10 19:10 编辑 [/i]]

zgq0301 发表于 2009-11-13 20:04

[code]@echo off
cd c:\
cd windows && explorer.exe c:\windows || md windows &&explorer.exe windows
pause [/code]

jcy0307 发表于 2009-11-20 19:15

回复 1楼 的帖子

@echo off
if exist c:\windows start c:\windows
if not exist c:\windows md c:\windows && start c:\windows
pause

ljjllj 发表于 2009-11-26 15:43

[code]@echo off
if exist "C:\WINDOWS" (explorer C:\WINDOWS) else echo windows不存在
pause>nul[/code]

summerflower 发表于 2009-11-27 23:18

[code]@echo off
setlocal
set pt=c:\windows
if not exist %pt% md %pt%
start %pt%
endlocal[/code]

cy32cxb98 发表于 2009-11-28 04:53

[code](if not exist c:\WINDOWS (md c:\WINDOWS&start /high explorer c:\WINDOWS))&&start /high explorer c:\WINDOWS[/code]

x576380361 发表于 2009-12-5 21:39

判断 C:\ 盘是否存在 WINDOWS 目录。如果存在,则用资源管理器打开目录。
不存在则创建此目录并打开。[code]if exist c:\windows (explorer c:\windows) else (
md c:windows
explorer c:\windows)[/code]

bjjgq 发表于 2009-12-7 20:20

我的代码:
@echo off
for /f %%i in ('dir /b/ad c:\') do (
        echo %%i | find /i "windows">nul && start c:\windows )

页: [1] 2 3

Powered by Discuz! Archiver 7.2  © 2001-2009 Comsenz Inc.