none
Copy-Item : Cannot find path RRS feed

  • Question

  • Hello

    I am creating CSV file using Add-Content. File is creating successfully. Now I want to copy this csv file from source location (c:\users\$Env:USERNAME\AppData\Local\Logs) to another folder c:\users\$Env:USERNAME\Downloads.

    So I used following code

    $Source="c:\users\$Env:USERNAME\AppData\Local\ComplianceLogs\$filename"

    $target="c:\users\$Env:USERNAME\Downloads"

    Copy-Item -Path $Source-Destination $target

    I am getting following error

    Copy-Item : Cannot find path

    However If I hardcoded the filename, I am not getting error, it seems after creating CSV file using add-content, I should clear buffer or close the CSV file.

    Monday, February 13, 2017 11:52 AM

All replies

  • Something I use to troubleshoot my own code a lot is just write-host ing my variables before I do work with them.  It might be something as simple as your $filename variable actually being a full path instead of just the base file name.  After you declare your $source and $target variables, output them to the screen just to make sure they're actually populated with what you think they should be.   Since hardcoding the filename works, that's my best guess for where the problem lies.
    • Proposed as answer by jrvModerator Monday, February 13, 2017 3:14 PM
    Monday, February 13, 2017 12:14 PM
  • $filename is not what you think it is.

    Copy-Item $filename $target

    You don't need to convert strings.


    \_(ツ)_/

    Monday, February 13, 2017 3:13 PM
    Moderator
  • Synck_ACK_87/jrv

    I checked output of $source and $target, it is coming properly.

    I also tried Copy-Item $Source $target, but getting same error.

    Please advise.

    Monday, February 13, 2017 4:52 PM
  • Correct me if im wrong but it doesn't appear you have assigned anything to $filename. If you assign $Filename  variable to the command creating the file in the first place, you should only need the $filename and $target.. e.g.

    Copy-Item $Filename $Target.. You don't need to add the parameters either.

    Bit like what Jrv said basically..

    If you haven't assigned anything to $Filename then you will have to put the full FilePath in and assign that filepath to the $Source variable and use that with the $Target variable..E.g.

    Copy-Item $Source $Target

    ---

    If this helped either mark it as helpful or answer please.. Powershell is awesome

     
    Monday, February 13, 2017 5:35 PM
  • Synck_ACK_87/jrv

    I checked output of $source and $target, it is coming properly.

    I also tried Copy-Item $Source $target, but getting same error.

    Please advise.

    I noticed a weird error on my computer when messing with your source and target variables. Assuming your Filename variable is declared elsewhere, can you just hard code the username in both source and target, but leave the $filename variable part in there? I just want to see if the "$ENV:Username" thing is throwing it for a loop for whatever reason.  If that works, change your variable declaration to

    $Source="c:\users\"+$Env:USERNAME+"\AppData\Local\ComplianceLogs\$filename"

    $target="c:\users\"+$Env:USERNAME+"\Downloads"

    and give it a shot.

    Monday, February 13, 2017 5:58 PM
  • Hi SYN_ACK_87

    I tried as you suggested but getting same error. Here is my code

    $Source="c:\users\"+$Env:USERNAME+"\AppData\Local\ComplianceLogs\$filename"
    $target="c:\users\"+$Env:USERNAME+"\Downloads"

    Copy-Item $Source $target

    or

    Copy-Item -Path $Source -Destination $target

    Monday, February 13, 2017 6:50 PM
  • Hi SYN_ACK_87

    I tried as you suggested but getting same error. Here is my code

    $Source="c:\users\"+$Env:USERNAME+"\AppData\Local\ComplianceLogs\$filename"
    $target="c:\users\"+$Env:USERNAME+"\Downloads"

    Copy-Item $Source $target

    or

    Copy-Item -Path $Source -Destination $target

    Can you type $filename into your console just to verify what it's coming back with?
    Monday, February 13, 2017 7:05 PM
  • Hi SYN_ACK_87

    I tried as you suggested but getting same error. Here is my code

    $Source="c:\users\"+$Env:USERNAME+"\AppData\Local\ComplianceLogs\$filename"
    $target="c:\users\"+$Env:USERNAME+"\Downloads"

    Copy-Item $Source $target

    or

    Copy-Item -Path $Source -Destination $target


    $filename is not what you think it is.

    Copy-Item $filename $target

    You don't need to convert strings.


    \_(ツ)_/

    • Proposed as answer by jrvModerator Monday, February 13, 2017 8:16 PM
    Monday, February 13, 2017 8:16 PM
    Moderator
  • Hi jrv

    I tried with your suggested command also, but no luck.

    Avian

    Monday, February 13, 2017 10:32 PM
  • Hi jrv

    I tried with your suggested command also, but no luck.

    Avian

    How did you create $filename?

    How did you create the file represented by $filename?

    Check "c:\users\$Env:USERNAME\AppData\Local\Logs" to be certain the file is actually there?


    \_(ツ)_/


    Monday, February 13, 2017 10:38 PM
    Moderator
  • You should use this construct as APPDATA can be in a different place:

    "$env:AppData\Local\Logs"

    Here is what I get on one of my systems:

    D:\scripts> "c:\users\$Env:USERNAME\AppData\Local\Logs"
    c:\users\testuser1\AppData\Local\Logs
    D:\scripts> $env:APPDATA
    C:\Users\test1\AppData\Roaming
    D:\scripts> "$env:AppDatal\Logs"
    C:\Users\test1\AppData\Roaming\Logs
    D:\scripts>

    Notice the difference.  THere can be many other variations depending on your configuration.


    \_(ツ)_/


    Monday, February 13, 2017 10:44 PM
    Moderator
  • This is the correct way to reference the local appdata folder as it will always reflect the tru location:

    [environment]::GetFolderPath('LocalApplicationData')

    D:\scripts> [environment]::GetFolderPath('LocalApplicationData')
    C:\Users\test1\AppData\Local
    D:\scripts>


    \_(ツ)_/

    Monday, February 13, 2017 10:49 PM
    Moderator
  • Hi,

    Since Powershell now as gone open source, we should probably start to use Join-Path and Split-Path more often. Here is a working sample that checks the source parent path, the source full path and the destination parentpath. Using ErrorAction Stop will terminate the script with the corresponding error.

    $filename = "somefile.txt"
    
    $source = Join-Path -Path $env:USERPROFILE -ChildPath Appdata | 
        Join-Path -ChildPath Local |
        Join-Path -ChildPath ComplianceLogs | 
        Join-Path -ChildPath $filename
    
    $target = Join-Path -Path $env:USERPROFILE -ChildPath Downloads |
        Join-Path -ChildPath $filename
    
    $sourceParent = $Source | Split-Path -Parent
    $targetParent = $target | Split-Path -Parent
    
    if (-not (Test-Path -Path $sourceParent))
    {
        Write-Error -Message "Sourcepath does not exists [$sourceParent]" -ErrorAction Stop
    }
    
    if (-not (Test-Path -Path $Source))
    {
        Write-Error -Message "Sourcefile does not exists [$Source]" -ErrorAction Stop
    }
    
    
    if (-not (Test-Path -Path $targetParent))
    {
        Write-Error -Message "TargetPath does not exists [$sourceParent]" -ErrorAction Stop    
    }
    
    Copy-Item -Path $Source -Destination $target
    

     Cheers

    Tore

    Monday, February 13, 2017 11:46 PM
  • Assuming that the computer is setup in some weird way that has the AppData folder in multiple locations, the original post mentions that just hard coding in the location of the file makes it work.   So just to double check, if you type $Source, copy and paste what it outputs and enter the command that way, everything goes through? 
    Tuesday, February 14, 2017 1:55 AM
  • Assuming that the computer is setup in some weird way that has the AppData folder in multiple locations, the original post mentions that just hard coding in the location of the file makes it work.   So just to double check, if you type $Source, copy and paste what it outputs and enter the command that way, everything goes through? 

    What you see at a prompt may not be what the code sees.

    AppData can be relocated to a network share.  In fact in most of my implementations the "roaming part is.

    I would run this a line at a time at a CLI prompt to inspect what is happening.


    \_(ツ)_/

    Tuesday, February 14, 2017 2:29 AM
    Moderator
  • Assuming that the computer is setup in some weird way that has the AppData folder in multiple locations, the original post mentions that just hard coding in the location of the file makes it work.   So just to double check, if you type $Source, copy and paste what it outputs and enter the command that way, everything goes through? 

    What you see at a prompt may not be what the code sees.

    AppData can be relocated to a network share.  In fact in most of my implementations the "roaming part is.

    I would run this a line at a time at a CLI prompt to inspect what is happening.


    \_(ツ)_/


    That's what I'm curious to see. If entering it manually works, and he said that when he outputted the variable to the screen, it was saying the right path, then the only thing left that I can see if to output the variable, copy it, and paste it manually into the command and see if it runs.  For all we know, maybe there's a weird character making its way in there, trailing white space, who knows.  Without seeing the actual output of everything for myself, all I can really say is that I did a test run with that code and a blank CSV file, and it worked straight away.
    Tuesday, February 14, 2017 12:16 PM