olivierg
olivierg

Reputation: 792

powershell wrapping long file paths

i have a script running fine in a powershell window but not when ran as a script, where lines are "wrapped", screwing the variables contents.

basically a part of my code looks like this :

Get-AIPFileStatus -Path $Path | Where-Object {$_.IsRMSProtected -eq 'True'}  | Out-String -Stream -width 4096 | Select-String "^(FileName|FilePath) +: +(.*)$" 

and instead of getting an output such as this one :

FileName        : Z:\long_path_here\long_path_here\long_path_here\long_path_here\file_name01.xlsx
FileName        : Z:\long_path_here\long_path_here\long_path_here\long_path_here\file_name02.xlsx
FilePath        : Z:\long_path_here\long_path_here\long_path_here\long_path_here\file_name03.xlsx
FileName        : Z:\long_path_here\long_path_here\long_path_here\long_path_here\file_name04.xlsx
FilePath        : Z:\long_path_here\long_path_here\long_path_here\long_path_here\file_nameN.xlsx

the script instead generates :

FileName        : Z:\long_path_here\long_path_here\long_path_here\long_path_here\
file_name01.xlsx
FileName        : Z:\long_path_here\long_path_here\long_path_here\long_path_here\
file_name02.xlsx
FilePath        : Z:\long_path_here\long_path_here\long_path_here\long_path_here\
file_name03.xlsx
FileName        : Z:\long_path_here\long_path_here\long_path_here\long_path_here\
file_name04.xlsx
FilePath        : Z:\long_path_here\long_path_here\long_path_here\long_path_here\
file_nameN.xlsx

which is messing up my loop afterwards (as i need the full file name), e.g. my code :

Get-AIPFileStatus -Path $Path | Where-Object {$_.IsRMSProtected -eq 'True'} | Out-String -Stream -width 4096 | Select-String "^(FileName|FilePath) +: +(.*)$" | ForEach-Object{$_.Matches.Groups[2].Value} |
Foreach-Object {
   $file = $_
   Write-Host -NoNewline "File '$file' has encryption enabled, trying to remove it.."
   Set-AIPFileLabel -LabelId $UID -Path $file -PreserveFileDetails -JustificationMessage "blablabla" 
   if( $? ) {
      Write-Host "SUCCESS"
   } else {
      Write-Host "FAILED"
   }
}

the variable "$file" above is only getting a small piece of the file path. in short, i just want to get file paths from the output of Get-AIPFileStatus and apply something on them, but because of this wrapping this is messing up the script as the final result is that the file path "taken" from the script is for example :

Z:\long_path_here\long_path_here\long_path_here\long_path_her

instead of :

Z:\long_path_here\long_path_here\long_path_here\long_path_here\file_name01.xlsx

how can i fix that ? thanks again regards,

PS : this is my first ever powershell script, be nice :)

Upvotes: 0

Views: 189

Answers (3)

olivierg
olivierg

Reputation: 792

Thanks for your answers. The solutions you offered seemed quite complex while i was simply trying to understand why the "basic" command Get-AIPFileStatus did wrap the paths and it was apparently related to the loop i was doing using ForEach-Object.

I have replaced it with ForEach instead and it is working now :

 Get-AIPFileStatus -Path $Path | Where-Object {$_.IsRMSProtected -eq 'True'} | Out-String -Stream -width 4096 | Select-String "^(FileName|FilePath) +: +(.*)$" | ForEach {
   $encryptedfile = $_.Matches.Groups[2].Value
   # using $encryptedfile...
}

i don't really know why but it works.

Upvotes: 0

iRon
iRon

Reputation: 23788

You might PinVoke the Create pagepathcompactpathex (shlwapi) function:

$Shlwapi = Add-Type -MemberDefinition '
    [DllImport("Shlwapi.dll", CharSet=CharSet.Auto)]public static extern bool PathCompactPathEx([Out] System.Text.StringBuilder pszOut, string szPath, int cchMax, int dwFlags);
' -Name 'ShlwapiFunctions' -namespace ShlwapiFunctions -PassThru

$Path = 'Z:\long_path_here\long_path_here\long_path_here\long_path_here\file_name01.xlsx'
$Length = 33 # Size of the simplifyed path (+1)
$StringBuilder = [System.Text.StringBuilder]::new($Length) 
$Null = $Shlwapi::PathCompactPathEx($StringBuilder, $Path, $Length, 0)
$StringBuilder.ToString()

Z:\long_path...\file_name01.xlsx

Upvotes: 1

Avshalom
Avshalom

Reputation: 8889

This is one option:

$txt = @"
FileName        : Z:\long_path_here\long_path_here\long_path_here\long_path_here\
file_name01.xlsx
FileName        : Z:\long_path_here\long_path_here\long_path_here\long_path_here\
file_name02.xlsx
FilePath        : Z:\long_path_here\long_path_here\long_path_here\long_path_here\
file_name03.xlsx
FileName        : Z:\long_path_here\long_path_here\long_path_here\long_path_here\
file_name04.xlsx
FilePath        : Z:\long_path_here\long_path_here\long_path_here\long_path_here\
file_nameN.xlsx
"@

$Splitted = ($txt -split "\n")

for ($i=0; $i -lt $Splitted.Count; $i+=2)
{
$Splitted[$i..($i-1)]  -join ""
}

Will Results:

FileName        : Z:\long_path_here\long_path_here\long_path_here\long_path_here\file_nameN.xlsx
FileName        : Z:\long_path_here\long_path_here\long_path_here\long_path_here\file_name01.xlsx
FilePath        : Z:\long_path_here\long_path_here\long_path_here\long_path_here\file_name02.xlsx
FileName        : Z:\long_path_here\long_path_here\long_path_here\long_path_here\file_name03.xlsx
FilePath        : Z:\long_path_here\long_path_here\long_path_here\long_path_here\file_name04.xlsx

Upvotes: 1

Related Questions