none
Path with a spaces in powershell - Need help RRS feed

  • Question

  • Hi All,

    I am trying the replace the path with spaces single tick, double tick and three ticks nothing adds tick mark to the variable with spaces.

    Do you all know what I am doing wrong here.

    Get-ChildItem -Path $Loc -Include $Extensions1 -Recurse -ErrorAction SilentlyContinue | ForEach-Object {
    
       $fname =$_.FullName
    
       Write-Host $fname
    
       $fname -replace ' ', '``` '

    Please let  me know.

    Full script

             

    param([string]$Param1="");
    $sys = Get-Content $Param1
    $output = ForEach ($System in $sys) {
    $LocStr= @("\\$System\c$\")
    $Extensions1 = @('*.jar','*.war','*.ear')
    
    
     ForEach ($Loc in $LocStr){
    
    
    Get-ChildItem -Path $Loc -Include $Extensions1 -Recurse -ErrorAction SilentlyContinue | ForEach-Object {
    
       $fname =$_.FullName
    
       Write-Host $fname
    
       $fname -replace ' ', '``` '
    Write-Host $fname


    • Edited by Chennai600101 Tuesday, March 12, 2019 4:27 PM
    • Moved by Bill_Stewart Wednesday, September 4, 2019 6:31 PM Abandoned
    Tuesday, March 12, 2019 3:01 PM

All replies

  • Please only post correctly formatted code using the code posting tool provided.  Please edit your post and fix. As posted it is unreadable.


    \_(ツ)_/

    Tuesday, March 12, 2019 3:42 PM
  • This appears to be what you are trying to do.  What will be accomplished by adding backticks in front of spaces.  Spaces do not require escaping.

    param (
        [string]$FileName,
        [string[]]$Extensions = @('*.jar','*.war','*.ear')
    )
    
    
    Get-Content $FileName |
        ForEach-Object{
            Get-ChildItem -Path "\\$_\c$\" -Include $extensions -Recurse -ErrorAction SilentlyContinue | #" 
                ForEach-Object {
                    Write-Host $_.FullName
                    $fname = $_.FullName -replace ' ', '``` '
                    Write-Host $fname
                }
        }


    \_(ツ)_/





    • Edited by jrv Tuesday, March 12, 2019 4:04 PM
    Tuesday, March 12, 2019 4:02 PM
  • sorry i updated it correctly with the code block.  I am trying to use the unzip command on the path returned by Get-ChildItem which is $_.FullName

    It works great if the path does not contain spaces.

    Example :  unzip works with the path below

    \\SMD1OAEATPW0001\c$\jira-cli-5.7.0-distribution\jira-cli-5.7.0\lib\json-simple-1.1.jar

    unzip does not work with the path below because of the space in the "Program Files".  I am trying to avoid it.

    \\SMD1OAEATPW0001\c$\Program Files\Java\jre1.8.0_181\lib\javaws.jar


    $jver=unzip -q -c $fname META-INF/MANIFEST.MF |findstr Implementation-Version


    Tuesday, March 12, 2019 4:32 PM
  • unzip -q -c "$fname" META-INF/MANIFEST.MF |findstr I"mplementation-Version"

    or -

    $cmd = 'unzip -q -c "{0}" META-INF/MANIFEST.MF | findstr Implementation-Version' -f $fname
    Invoke-Expression $cmd


    \_(ツ)_/

    Tuesday, March 12, 2019 4:40 PM
  • Thanks

    I updated the code . I get the error below. unzip fails saying cannot find or open . The path is missing a backslash. I am not sure why

    \\SMD1OAEATPW0001\c$\Program Files\Java\jre1.8.0_181\lib\ext\access-bridge-64.jar

    unzip:  cannot find or open \SMD1OAEATPW0001\c$\Program Files\Java\jre1.8.0_181\lib\ext\access-bridge-64.jar, \SMD1OAEATPW0001\c$\Program Files\Java\jre1.8.0_181\lib\ext\access-bridge-64.jar.zip or \SMD1OAEATPW0001\c$\Program Files\Java\jre
    1.8.0_181\lib\ext\access-bridge-64.jar.ZIP.


     $cmd = 'unzip -q -c "{0}" META-INF/MANIFEST.MF | findstr Implementation-Version' -f $fname
    Invoke-Expression $cmd |Out-String -OutVariable jver
    Write-Host $jver


    Tuesday, March 12, 2019 5:01 PM
  • What is in $fname?

    What does $cmd have after the assignment?


    \_(ツ)_/

    Tuesday, March 12, 2019 5:59 PM
  • Hi ,

    Thanks attached the below.   Looks like the path correctly quoted .  Is it due to I am unable to open the jar file due to permissions?

    Is there a way I can force open it?

    \\SMD1OAEATPW0001\c$\Program Files\Java\jre1.8.0_181\lib\resources.jar
    unzip -q -c "\\SMD1OAEATPW0001\c$\Program Files\Java\jre1.8.0_181\lib\resources.jar" META-INF/MANIFEST.MF | findstr Impl
    ementation-Version
    unzip:  cannot find or open \SMD1OAEATPW0001\c$\Program Files\Java\jre1.8.0_181\lib\resources.jar, \SMD1OAEATPW0001\c$\P
    rogram Files\Java\jre1.8.0_181\lib\resources.jar.zip or \SMD1OAEATPW0001\c$\Program Files\Java\jre1.8.0_181\lib\resource
    s.jar.ZIP.

    Tuesday, March 12, 2019 6:24 PM
  • I suspect that unzip does not take UNC paths.  You need to use a drive spec.


    \_(ツ)_/

    Tuesday, March 12, 2019 6:32 PM
  • Thanks I will try that
    Tuesday, March 12, 2019 6:49 PM