繁体   English   中英

使用批处理脚本提取文件中的路径列表

[英]Extract list of path in a file using batch script

这是我的文本文件:

==================================================
Folder            : D:\T\New folder
==================================================

==================================================
Folder            : D:\T\Z-Ai
==================================================

==================================================
Folder            : D:\T\Z-BiN
==================================================

我需要从这个文件中提取路径,所以我有这样的事情:

D:\T\New folder 
D:\T\Z-Ai 
D:\T\Z-BiN

看来我应该使用findstr TargetWord TargetFile.txt命令。 而且似乎我可以像这样使用正则表达式: findstr /r "^[az][az]$ ^[az][az][az]$"

但我不知道如何遍历找到的目标或获取输出列表。 任何帮助都非常感谢。

根据您的评论,您想使用结果来执行 xcopy 任务,看来您真的想要这样的东西。 注意我使用example.txt作为输入文件,并使用DESTINATION在其中添加目的地,包括您需要的相关xcopy开关:

@echo off
for /f "tokens=2*" %%i in ('type example.txt ^| findstr /i ":\\"') do xcopy "%%~j\*" DESTINATION

或者,我们可以直接在Folder上使用findstr

@echo off
for /f "tokens=2*" %%i in ('type example.txt ^| findstr /i "Folder"') do xcopy "%%~j\*" DESTINATION

你可以这样做:

@echo off
Title Extract list of path in a file using batch script
set "TxtList=MyList.txt"
Set  "OutPutData=Output.txt"
Call :Extract "%TxtList%" "%OutPutData%"
Start "" "%OutPutData%"
Exit
::*****************************************************
:Extract <InputData> <OutPutData>
(
echo Data = WScript.StdIn.ReadAll
echo Data = Extract(Data,"[\w]:(\\[0-9\sA-Za-z\-]*)+"^)
echo WScript.StdOut.WriteLine Data
echo '************************************************
echo Function Extract(Data,Pattern^)
echo    Dim oRE,oMatches,Match,Line
echo    set oRE = New RegExp
echo    oRE.IgnoreCase = True
echo    oRE.Global = True
echo    oRE.Pattern = Pattern
echo    set oMatches = oRE.Execute(Data^)
echo    If not isEmpty(oMatches^) then
echo        For Each Match in oMatches  
echo            Line = Line ^& Trim(Match.Value^) ^& vbcrlf
echo        Next
echo        Extract = Line 
echo    End if
echo End Function
echo '************************************************
)>"%tmp%\%~n0.vbs"
cscript /nologo "%tmp%\%~n0.vbs" < "%~1" > "%~2"
If Exist "%tmp%\%~n0.vbs" Del "%tmp%\%~n0.vbs"
exit /b
::****************************************************

对于 Windows。 你可以使用powershell。

 select-string -Path c:\tmp\file.txt -Pattern '[A-Z]:(\\[0-9\ A-Za-z\-]*)+' -AllMatches | % { $_.Matches } | % { $_.Value }

在我看来, For /F是您完成任务所需的全部。 尽管在某些情况下使用Type可能很有用,但不需要使用find.exefindstr.exe来执行此任务,因为您不需要匹配特定的 glob/pattern

@For /F "EOL==Tokens=2*UseBackQ" %%A In ("TargetFile.txt")Do @"%__AppDir__%xcopy.exe" "%%B" "Destination\" /Options

请注意,如果有一个或多个这些Folder s 不存在的机会,那么使用If Exist "%%B\\"可能是明智的。 重要的是,如果包含Folder路径的每一行都填充到其行尾,则此解决方案对您不起作用。

暂无
暂无

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

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