检查一个目录的绝对路径或文件名是有效的 [英] Check if an absolute path of a directory or a file name are valid

查看:286
本文介绍了检查一个目录的绝对路径或文件名是有效的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建一个蝙蝠剧本,我应该检查一个变量是否包含目录的有效绝对路径,如果另一个变量包含一个文件的有效名称为Windows 8及以上。

I'm creating a bat script and I should check whether a variable contains a valid absolute path of a directory and if another variable contains a valid name of a file for Windows 8 and above.

所以,我怎么会去这些检查?

So, how would I go these checks?

感谢

细则

推荐答案

这是非常棘手比大多数人意识到。有许多关于这个话题误传可在SO和其他地方。有迹象表明,似乎很多工作的解决方案,但在某些情况下会失败。

This is much trickier than most people realize. There is lots of misinformation about this topic available on SO and elsewhere. There are many "solutions" that appear to work, but then fail under certain circumstances.

问题可分为两部分:

1)绝对或相对

您不能简单地将路径转换成使用FOR变量%%〜FF或参数%〜F1和测试,看它是否匹配原始的字符串(不区分大小写),因为有无限多的完整路径方式的绝对路径可以写入。一个有效的绝对路径可以有任意数量的 \\ .. \\ \\。\\ 在其中。此外,绝对路径可以使用一个驱动器号,或者它可以是一个UNC路径。此外,绝对路径可以包含任意数量的之内吧。

You cannot simply convert the path into a full path using FOR variable "%%~fF" or parameter "%~f1" and test to see if it matches the original string (ignoring case) because there are an infinite number of ways an absolute path can be written. A valid absolute path may have any number of \..\ or \.\ within it. Also, an absolute path may use a drive letter or it may be a UNC path. Also, an absolute path may include any number of " within it.

我测试,以查看是否路径是首先取出所有的报价绝对或相对的,然后我用FINDSTR来测试它是否与下面的任一开始的:

I test to see if a path is absolute or relative by first removing all quotes, and then I use FINDSTR to test if it begins with either of the following:


  • 驱动器号,其次是结肠,其次是反斜杠

  • 两回斜线

2)文件或文件夹,或不存在

这是很容易使用 IF EXISTS路径来告诉路径是否有效。但更难以区分从文件夹中的一个文件。

It is easy to use IF EXISTS path to tell whether a path is valid or not. But it is more difficult to distinguish a file from a folder.

早在DOS时代,你可以检查路径\\ NUL 存在,如果确实如此,那么你知道这条道路是一个文件夹。不幸的是,很多人都是假的IM pression,这个工作在Windows下下 - 这是不可靠的Windows下的

Back in the days of DOS, you could check if path\NUL exists, and if it does, then you knew that path was a folder. Unfortunately, many people are under the false impression that this works under Windows - It is not reliable under Windows

另一个常见的​​尝试是测试如果路径\\ 存在,如果是这样,假设它必须是一个文件夹。这往往似乎工作,但它的不若路径涉及目录的符号链接或路口工作

Another frequent attempt is to test if path\ exists, and if so, assume that it must be a folder. This often seems to work, but it does not work if the path involves directory symbolic links or junctions

分类code我会用几乎一样的 http://stackoverflow.com/a/8669636/ 1012053 ,但我已经通过它为使用环境变量,而不是一个批次的参数。

The classification code I would use is nearly the same as for http://stackoverflow.com/a/8669636/1012053, except I have adopted it for use with an environment variable instead of a batch parameter.

我用的是FOR变量〜一个修改一下文件/文件夹的属性。如果我发现 D 那么它是一个文件夹。如果我发现没有 D属性那么它是一个文件。否则,如果我不能找到属性,然后它不存在。

I use the FOR variable ~a modifier to look at the file/folder attributes. If I find d then it is a folder. if I find attributes without d then it is a file. Else if I fail to find attributes then it does not exist.

这似乎准确地告诉一个文件夹是否存在另一种方法是测试如果路径\\ * 存在,但我有这个方法经验较少。

The other method that appears to accurately tell whether a folder exists is to test if path\* exists, but I have less experience with this method.

所以把他们放在一起,我得到

So putting it all together, I get

@echo off
setlocal
set var=test.bat

setlocal enableDelayedExpansion

:: Determine absolute or relative
echo(!var:^"=!|findstr /i "^[A-Z]:[\\] ^[\\][\\]" >nul && set "type=absolute" || set "type=relative"

:: Determine file or folder or not exists
for /f eol^=^ delims^= %%F in ("!var!") do (
  for /f "tokens=1,2 delims=d" %%A in ("-%%~aF") do if "%%B" neq "" (
    echo %%F = %type% folder
  ) else if "%%A" neq "-" (
    echo %%F = %type% file
  ) else (
    echo %%F does not exist
  )
)

这篇关于检查一个目录的绝对路径或文件名是有效的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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