I have a PowerShell script that stores the full paths of the files in a specific directory along with some other information. I have exported the CSV file. Now, the paths are actually being combined together to comprise of the full path. Let me give an example below:
$root= C:\Users\home\
$web = facebook website\domain
$app = facebook android\latest
These paths are then joined together with either Join-Path
or $fbweb = $root + $web
to make up the full path: C:\Users\home\facebook website\domain
Now the above mentioned path will have other files, subfolders etc in it but that's the gist of how the paths are structured. I have exported them in the CSV file but I'm having trouble with the following. I need the CSV file to have paths in such a way that the part leading up to the $web
is trimmed out.
For instance if the CSV file is like this:
Path
C:\Users\home\facebook website\domain\version\version-feb-2020.txt
C:\Users\home\facebook website\domain\interface\somefile.html
C:\Users\home\facebook android\latest\messenger\messenger app files\code.js
C:\Users\home\facebook android\latest\application\somecode.js
I want it to turn out like this:
Path
facebook website\domain
\version\version-feb-2020.txt
\interface\somefile.html
facebook android\latest
\messenger\messenger app files\code.js
application\somecode.js
I have tried using the following to trim it out:
$number = [regex]::matches($fbweb,"\\").count
Select-Object Hash,
@{
Name = "FilePath";
Expression = { [string]::Join("\", ($_.Path -split "\\" | Select-Object -skip ($number)))}
}
Update: I have tried this:
$replace = Join-Path -Path $root -ChildPath $web
echo $replace
$RefHash = Import-csv "C:\Users\Admin\Desktop\fb.csv"
$RefHash | ForEach-Object {
echo $_.Path
($_.Path).Replace($replace, "\")
} | Export-csv "C:\Users\Admin\Desktop\replaced.csv"
But this just results in the exported csv showing the following:
#TYPE System.String
"Length"
"numbers"
"numbers"
"numbers"
version\version-feb-2020.txt
not havedomain\
in front and why isapplication\somecode.js
missinglatest\
? – Theodomain\interface\somefile.html
in your desired output havedomain\
, while this was already in$web
and why does this linelatest\messenger\messenger app files\code.js
containlatest\
, where this is already part of$app
?? – Theo-NoTypeInformation
afterExport-csv "C:\Users\Admin\Desktop\replaced.csv"
– Theo