I have a text file with thousands of lines, containing both directory paths and file paths. I would like to loop through each line of the text file and remove any lines containing a directory path, and keep all lines containing a file path. An example of two lines (one directory, and one path from the text file):
exampleDirectoryPath/tags/10.0.0.8/tools/
exampleFilePath/tags/10.0.0.8/tools/hello.txt
So far, to loop through the text file, I have:
foreach ($line in [System.IO.File]::ReadLines("file.txt")) {
if ($line -match ".*/.*$") {
$line
}
}
Goal output:
exampleFilePath/tags/10.0.0.8/tools/hello.txt
Note: I do not want to hardcode file extensions. There are thousands of files to traverse and I dont know what extensions are present, so I would like to return all of them.
So, the basic logic here is easy:
Get-Content "file.txt" | where { $_ is a file path... }
It kind of depends on how you want to determine, if it's a file path
If all of your directory paths end in "/", you could simply do:
where { -not $_.EndsWith("/") }
or:
where { [system.io.Path]::GetFileName($_) -eq "" }
If not, but all of your file paths definitely have an extension, you could do:
where { [system.io.Path]::GetExtension($_) -ne "" }
If all of the paths actually exist, you could also do this:
where { Test-Path $_ -Type Leaf }