使用单个字符串查找多个文件路径 [英] find multiple files paths with single string

查看:60
本文介绍了使用单个字符串查找多个文件路径的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图编写一个批处理脚本,该脚本查找名称与输入字符串相同的文件的所有路径.现在它只能找到找到的第一个文件,我想不出一种方法来列出多个文件位置.我不是很有经验,我需要一些帮助.

I tried to write a batch script that find all the paths of files that have the same name as the input string. right now it can find only the first file found, and i cant think of a way to make it list multiple files locations. I am not very experienced and I need some help.

这是脚本代码的一部分:

this is part of the script code:

:start
    cls
    echo Enter file name with extension:
    set /p filename=
    echo Searching...

for %%a in (C D E F G H U W) do (
    for /f "tokens=*" %%b in ('dir /s /b "%%a:\%filename%"') do (
        set file=%%~nxb
        set datapath=%%~dpb\
        ::the path of the file without the filename included "C:\folder\folder\"
        set fullpath=%%b
        ::the path of the file with the filename included "C:\folder\folder\file"
        goto break
  )
)
:notfound
    cls
    echo Enter file name with extension:
    echo %filename%
    echo File Not Found!
    ping localhost -n 4 >nul
    goto start

:break
    if "%datapath:~-1%"=="\" set datapath=%datapath:~,-1%
    cls
    echo 3 %filename% found
    echo %fullpath1%
    echo %fullpath2%
    echo %fullpath3%
    --- || ---

我希望脚本搜索计算机并列出所有遇到的具有相同名称的文件,并且希望能够将这些文件的路径放入不同的变量中.
例如,如果输入readme.txt,那么我想要具有该特定名称(readme.txt)的所有文件的所有路径的列表,并且想要为每个路径设置变量,以便以后可以使用它.

I want the script to search the computer and list every encountered files with the same name and I want to be able to put those files' paths into different variables.
For example, if readme.txt is the input, then I want the list of all the paths of all the files with that specific name (readme.txt) and I want to set variable for each path so I can use it after that.

input:  
readme.txt

output:  
3 files found  
C:\folder\folder\readme.txt  
C:\folder\folder\folder\readme.txt  
D:\folder\readme.txt

推荐答案

@echo off
set filename=readme.txt
for %%a in (C D E F G H U W) do (
  for /f "tokens=*" %%b in ('dir /s /b "%%a:\%filename%"') do (
    echo you can do something here with  %%~nxb in %%~dpb
    echo    full name: %%b
  )
)

我认为不需要将文件名设置为变量,因为您可以在循环中处理它们.但是,如果您确实出于某些原因(出于某种原因)需要变量:

I see no need to set the filenames to variables, as you can process them inside your loop. But if you really need them (for some reason) in variables:

@echo off
setlocal enabledelayedexpansion
set filename=readme.txt
set count=0
for %%a in (C D E F G H U W) do (
  for /f "tokens=*" %%b in ('dir /s /b "%%a:\%filename%" 2^>nul') do (
    set /a count+=1
    set _file[!count!]=%%b
  )
)
set _file

这篇关于使用单个字符串查找多个文件路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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