简体   繁体   English

如何制作一个批处理脚本,以递归方式在c中找到文件.bat?

[英]How to make a batch script that finds a file .bat in c recursively?

Im new to batch scripting and im trying to learn how I can make a script that can go recursively through the folders and if its founded give me the path, if not echo a simple message. 我是批处理脚本的新手,我试图学习如何制作可以递归浏览文件夹的脚本,如果建立了该脚本,则为我提供了路径,即使没有回显一条简单的消息。 i was starting by here: 我从这里开始:

@echo off
for /f %%f in ('dir bucle.bat /s/b') do @echo %f  %~tf
pause
exit 

but Im kinda stuck. 但我有点卡住了

You are using a for command to process the output of a dir command. 您正在使用for命令来处理dir命令的输出。 Just to found the file, you don't need all this. 只需找到文件,就不需要所有这些。 Simplify 简化

dir c:\bucle.bat /s /b 

This searchs the file, when found shows it with full path and if not found echoes a error message. 这将搜索文件,找到后显示完整路径,如果未找到则回显错误消息。

try: 尝试:

@echo off
for /f %%f in ('dir bucle.bat /s/b') do @echo %%f  %%~tf
pause
exit 

to echo only the path: 只回显路径:

@echo off
for /f %%f in ('dir bucle.bat /s/b') do @echo %%f
pause
exit 

you can also use where command: http://ss64.com/nt/where.htm 您还可以使用where命令: http : //ss64.com/nt/where.htm
and the exit command at the end isn't necessary... 并且不需要最后的exit命令...
PS: all your code is the same as this: dir bucle.bat /s /b PS:您的所有代码与此相同: dir bucle.bat /s /b

denotes missing elements in the original code: 表示原始代码中缺少的元素:

@echo off
for /f "delims=" %%f in ('dir bucle.bat /s/b') do @echo %%f  %%~tf
rem    ↑↑↑↑↑↑↑↑↑                                        ↑↑   ↑↑
pause
  • "delims=" - to properly treat paths with spaces, "delims=" -正确处理带空格的路径,
  • %%f - loop variable. %%f循环变量。

You could specify starting directory for recursive searching: 您可以为递归搜索指定起始目录:

for /f "delims=" %%f in ('dir C:\myfolder\bucle.bat /s/b') do @echo %%f  %%~tf

or even 甚至

for /f "delims=" %%f in ('dir C:\bucle.bat /s/b') do @echo %%f  %%~tf

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM