
Please don't ask duplicate questions.
https://learn.microsoft.com/en-us/answers/questions/648868/relative-path-powershell.html
This browser is no longer supported.
Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.
Hello,
I current have a script here I am trying to get some info into a CSV file and it does not get the relative path from others folders and keeps to pass the same relative path to all folders and subfolders.
Could someon help me on how to fix this please?
This is my current script:
$starttime = (Get-Date)
$root = "C:\backup\""
$report = "testtttt.csv"
$hasher = [System.Security.Cryptography.SHA256]::Create()
$AllFiles = @()
"`n"#line space
Write-Host "Generating the Hash from $root"
foreach ($file in get-childitem $root -recurse -File | Select-Object FullName, path, Directory, Name, PSIsContainer, Length, CreationTime, LastAccessTime, LastWriteTime)
{
$obj = new-object psObject
$items = Get-ChildItem -Path $root -Recurse -File
foreach($item in $items) {
$relativePath = $item.FullName.Substring($root.Length)
Write-Host "Debug $relativePath" -ForegroundColor Red
#$obj | Add-Member -MemberType noteproperty -Name Path -Value $relativePath #-force
}
if(!$file.PsIsContainer)
{
$inputStream = New-Object IO.StreamReader $file.fullname
$hashBytes = $hasher.ComputeHash($inputStream.BaseStream)
$inputStream.Close()
$builder = New-Object System.Text.StringBuilder
$hashBytes | Foreach-Object { [void] $builder.Append($_.ToString("X2")) }
$obj | Add-Member -membertype noteproperty -name FilePathandName -Value $file.FullName
$obj | Add-Member -membertype noteproperty -name Name -Value $file.Name
$obj | Add-Member -MemberType noteproperty -Name RelativePath -Value $relativePath #-force
$obj | Add-Member -MemberType noteproperty -Name Hash -Value $builder.ToString()
$obj | Add-Member -membertype noteproperty -name CreationTime -Value $file.CreationTime
$obj | Add-Member -MemberType noteproperty -Name LastAccessTime -Value $file.LastAccessTime
$obj | Add-Member -MemberType noteproperty -Name LastWriteTime -Value $file.LastWriteTime
$AllFiles += $obj
}
}
$AllFiles |Export-Csv $report –NoTypeInformation
"n" Write-Host "$report File has been created " "
n"
Write-Host "The script took:"
$endTime = Get-Date
New-TimeSpan -Start $startTime -End $endTime
And this is what I am getting in the CSV File
As you can see in green it sets the same relative path to all folders and subfolders/files
Please don't ask duplicate questions.
https://learn.microsoft.com/en-us/answers/questions/648868/relative-path-powershell.html
Look no further than this bit of code:
#Getting the Relative Path
$items = Get-ChildItem -Path $root -Recurse -File
foreach ($item in $items) {
$relativePath = $item.FullName.Substring($root.Length)
Write-Host "Debug $relativePath" -ForegroundColor Red
#$obj | Add-Member -MemberType noteproperty -Name Path -Value $relativePath #-force
}
$obj will get the relative path of the LAST file processed by the foreach loop EVERY TIME.
If you're going to post code please use the "Code Sample" editor. It's the 5th icon from the left on the Format Bar -- it has the graphic "101 010" icon.