BAT脚本搜索与输入名称匹配的文件夹的目录 [英] BAT script to search directory for folders that match an input name

查看:1181
本文介绍了BAT脚本搜索与输入名称匹配的文件夹的目录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

不知道什么是最好的(或更好的)方式来编写一个蝙蝠脚本来进行姓名输入并搜索一个目录来查看是否存在。



?p需要在运行比较之前先将目录列表输出到文件中?



如果它有所作为,目录在一个存储库,所以为了我的目的,我将使用'svn列表',但我认为一个很好的通用解决方案每个人都会很好。

解决方案

如果您正在测试目录中的特定名称,那么您通常会执行以下操作:

 如果存在名称(echo found it)else(echo not found)
/ pre>

或如果名称不完整

 如果存在* name *(echo found it)else(echo not found)

如果您正在测试名称,在目录树中,然后我将使用

  dir / s / ad * name *> nul&& (回音发现)|| (echo not found)

如果您发出生成输出行的命令,并且您想要测试如果任何一行中都有一个名称,那么Windows管道通常工作正常,只要输出的大小不大,下半部分可以跟上半年。

  yourCommand |找到名称> nul&&& (回音发现)|| (回音未找到)

但如果下半年比上半年慢,管道效率低下,并且必须缓冲大量数据。在这种情况下,最好使用临时文件而不是管道。我将一个随机数并入临时文件名,以防止使用相同临时目录的多个进程的可能冲突。

  set tempFile =%temp%\myTempFileBaseName%random%.txt
yourCommand>%tempFile%
<%tempFile%findname> nul&& (回音发现)|| (回音未找到)
del%tempFile%

我通常使用管道,除非我知道有一个性能问题。


not sure what's the best (or better) way to write a bat script to take name input and search a directory to see whether it exists.

do I need to output the directory list to a file first before running a comparison?

and if it makes a difference, the directory is on a repository so for my purposes I'll be using 'svn list', but I thought a nice general solution for everyone would be nice.

解决方案

If you are testing for a specific name within a directory, then you would generally do something like:

if exist name (echo found it) else (echo not found)

Or if the name is incomplete

if exist *name* (echo found it) else (echo not found)

If you are testing for a name anywhere within a directory tree, then I would use

dir /s /a-d *name* >nul && (echo found it) || (echo not found)

If you are issuing a command that generates lines of output and you want to test if a name exists within any one line, then Windows pipes generally work fine, as long as the size of the output is not huge and the 2nd half can keep up with the 1st half.

yourCommand | find "name" >nul && (echo found it) || (echo not found)

But pipes become inefficient if the 2nd half is slow compared to the 1st half, and a lot of data must be buffered. In that case it is definitely better to use a temp file instead of a pipe. I incorporate a random number into the temp file name to guard against possible collision of multiple processes using the same temp directory.

set tempFile="%temp%\myTempFileBaseName%random%.txt"
yourCommand >%tempFile%
<%tempFile% find "name" >nul && (echo found it) || (echo not found)
del %tempFile%

I generally use pipes unless I know I have a performance issue.

这篇关于BAT脚本搜索与输入名称匹配的文件夹的目录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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