Inside Man
Inside Man

Reputation: 4297

Extract list of path in a file using batch script

Here is my text file:

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

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

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

I need to extract the paths from this file, so I have something like this:

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

It seems I should use findstr TargetWord TargetFile.txt command. and Also it seems I can use regex like this: findstr /r "^[a-z][a-z]$ ^[a-z][a-z][a-z]$"

But I do not know how to loop through found targets or get the list of output. any help is really appreciated.

Upvotes: 1

Views: 555

Answers (4)

Compo
Compo

Reputation: 38623

In my opinion, For /F is all you need for the task. Although using Type may be useful in some situations, there's no need to use find.exe or findstr.exe for this task as you don't need to match a particular glob/pattern:

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

Please note that it may be wise, if there's a chance that one or more of theses Folders do not exist, that you prepend using If Exist "%%B\". Importantly, if each of the lines containing the Folder paths, is space padded up to the end of its line, this solution will not work for you.

Upvotes: 0

Hackoo
Hackoo

Reputation: 18827

You can do like this :

@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
::****************************************************

Upvotes: 1

user7818749
user7818749

Reputation:

Based on your comment, you want to use the result to perform an xcopy task, it seems you really want something like this. Note I used example.txt as input file, and DESTINATION where you should add your destination, including the relevant xcopy switches you require:

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

Alternatively we can use the findstr directly on Folder

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

Upvotes: 1

Ke Zhu
Ke Zhu

Reputation: 225

For Windows. you can use powershell.

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

Upvotes: 0

Related Questions