如何搜索本地驱动器的目录是否存在? [英] How can I search the local drives for the existence of a directory?

查看:116
本文介绍了如何搜索本地驱动器的目录是否存在?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图找出如何确定目录\\程序文件(x86)\\ Apache软件基金会\\的Apache2.2在任何一个Windows 2008计算机的本地驱动器的存在。

I'm trying to figure out how to determine if the directory "\Program Files (x86)\Apache Software Foundation\Apache2.2" exists on any of the local drives of a Windows 2008 machine.

我觉得这一定是这样的:

I thought it'd be something like this:

for /f "skip=2 tokens=2 delims=," %%A in (
 'wmic logicaldisk get name /format:csv'
) DO (
  echo %%A
  IF EXIST "%%A\Program Files (x86)\Apache Software Foundation\Apache2.2" (
    echo FOUND ON %%A
  )
)

但它只是回声的盘符。

But it simply echos the drive letter.

我是一个Unix家伙停留在Windows世界这个特定的项目,请帮忙!

I'm a Unix guy stuck in a Windows world for this particular project, please help!

我从一位同事一些帮助,下面是它是如何解决:

I got some help from a co-worker, here's how it was solved:

SET FOUNDIT=FALSE
SET ERRORLEVEL=

FOR /F "skip=2 tokens=2 usebackq delims=," %%i IN (
    `wmic logicaldisk where "drivetype=3" get name^,size /format:csv`
) DO (
    if /i "%foundit%" equ "true" exit /b
    CALL :APACHE %%i
    if %ERRORLEVEL% equ 0 set FOUNDIT=TRUE
)

IF %FOUNDIT% equ "TRUE" exit /B ELSE exit /B 1

:APACHE
    set DRIVE=%1
    REM echo DRIVE is %DRIVE%
    if "%DRIVE%" equ "" exit /B
    set APACHEPATH=%DRIVE%\Program Files (x86)\Apache Software Foundation\Apache2.2
    IF EXIST "%APACHEPATH%" CALL :FOUNDIT "%APACHEPATH%"

    exit /B

:FOUNDIT
    echo FOUND APACHE on %1
    for /f "tokens=*" %%x in (%1) do set APACHEINSTALLDIR=%%x
    set FOUNDIT=TRUE
    exit /B

感谢您的帮助!

推荐答案

WMIC有一个丑恶行为:它不写正确的换行,因此,如果您使用的最后一个令牌,你遇到问题

wmic has a ugly behaviour: it does not write proper linefeeds, so if you use the last token, you run into problems.

解决方法:不使用最后一个令牌(或迫使它添加另一个道理,你不要用,这里尺寸)。当你不使用最后一个令牌,它并不重要,它并没有正确结束

Workaround: don't use the last token (or force it to add another token, that you don't use, here size). As you don't use the last token, it doesn't matter that it doesn't end properly

for /F "skip=2 tokens=2 delims=," %A in ('wmic logicaldisk where "drivetype=3" get name^,size /format:csv') DO (

(你将不得不逃离逗号)。

(you will have to escape the comma).

我添加了,其中DRIVETYPE = 3,所以将只检查硬碟(无CD / DVD /闪存),但你可以离开了这一点。

I added the where "drivetype=3", so it will only check HardDisks (no CD/DVD/Flash), but you can leave this out.

这篇关于如何搜索本地驱动器的目录是否存在?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆