Get-ChidItem Piped to Copy-Item

Welcome Forums General PowerShell Q&A Get-ChidItem Piped to Copy-Item

This topic contains 8 replies, has 4 voices, and was last updated by

 
Participant
1 year, 1 month ago.

  • Author
    Posts
  • #78943

    Participant
    Points: 0
    Rank: Member

    Below is the Sample Code
    ———————————
    1. Am trying to Copy files from one Folder to another folder using below code but it does not copy

    Method 1 (It does not copy anything, it only creates a file with $FolderName)
    ———————————————————————————————–
    $Date = Get-Date -UFormat "%Y%m%d"
    $FolderName = "D:\SONPD\" + $Date.ToString()
    Get-ChildItem -Path "D:\Production_Backup\" -Exclude SONPDA | Copy-Item -Destination $FolderName
    ————————————————————————————————-

    Method 2 (throws below Error)
    ———————————————————————————————–
    $Date = Get-Date -UFormat "%Y%m%d"
    $FolderName = "D:\SONPD\" + $Date.ToString()
    Get-ChildItem -Path "D:\Production_Backup\" -Exclude SONPDA | Select -ExpandProperty FullName | Copy-Item -Path $_.FullName -Destination $FolderName

    ———————————————————————————————–

    Error

    Copy-Item : Cannot bind argument to parameter 'Path' because it is null.
    At line:3 char:113
    + ... opy-Item -Path $_.FullName -Destination $FolderName
    + ~~~~~~~~~~~
    + CategoryInfo : InvalidData: (:) [Copy-Item], ParameterBindingValidationException
    + FullyQualifiedErrorId : ParameterArgumentValidationErrorNullNotAllowed,Microsoft.PowerShell.Commands.CopyItemCommand

    Can some please help me on this to figure whats going wrong.

  • #78950

    Participant
    Points: 0
    Rank: Member

    Number 1 should work. Without knowing the exact structure of your folder/filesystem there though its hard to tell what is going on. Using the same three lines on a similar folder setup on my machine works just fine (as long as you were looking to only go one level deep with the copy)

    Number 2 throws an error because once you do select -expandProperty it no longer has a property name of fullname and you need to wrap it in a foreach-object so that line becomes:

    Get-ChildItem -Path "D:\Production_Backup\" -Exclude SONPDA | Select -ExpandProperty FullName | foreach-object{Copy-Item -Path $_ -Destination $FolderName}
    

    If you just do

    Get-ChildItem -Path "D:\Production_Backup\" -Exclude SONPDA 

    Do you see the expected output?

  • #78953

    Participant
    Points: 0
    Rank: Member

    Paul,
    Thanks for the quick reply.
    MY Folder Structure is as below
    D:\Production_Backup
    SONPDA (Folder)
    1.dat (File)
    2.dat (File)
    3.log (File)
    4.bcf (File)

    As per your statement

    1. Get-ChildItem -Path "D:\Production_Backup\" -Exclude SONPDA | Select -ExpandProperty FullName | foreach-object{Copy-Item -Path $_ -Destination $FolderName}

    This is creating just a file Name with $FolderName

    2. Get-ChildItem -Path "D:\Production_Backup\" -Exclude SONPDA (according to my structure) am getting the below correct out put

    1.dat (File)
    2.dat (File)
    3.log (File)
    4.bcf (File)

  • #78956

    Participant
    Points: 0
    Rank: Member

    Can you show the EXACT output of this command

    Get-ChildItem -Path "D:\Production_Backup\"

    Thanks

    • #78998

      Participant
      Points: 0
      Rank: Member

      Hi Richard,

      Please find the below out put which list all files and folder SONPDA

      Mode LastWriteTime Length Name
      —- ————- —— —-
      d—- 9/6/2017 11:59 PM SONPDA
      -a— 9/6/2017 10:00 PM 17053184 HDR9_SDB_SiteBackup.dat
      -a— 9/6/2017 10:00 PM 843 SONPD.bcf
      -a— 9/6/2017 10:00 PM 2372 SONPDBackup.log
      -a— 9/6/2017 10:00 PM 673468928 SONPD_CatalogBackup.dat
      -a— 9/6/2017 10:00 PM 429038080 SONPD_Model_Backup.dat

  • #79000

    Participant
    Points: 0
    Rank: Member

    Hi All,

    I have tried the below Code also it does not work

    D:\Rooster\Scripts> $Date = Get-Date -UFormat "%Y%m%d"
    $FolderName = "D:\SONPD\" + $Date.ToString()
    $Files = Get-ChildItem -Path 'D:\Production_Backup\*' -Exclude SONPDA | Select -Property Name,@{label="Path";Expression ={$_.FullName}} | Select -Property Path

    foreach ($sFile in $Files)
    {
    Copy-Item -LiteralPath $sFile.Path -Destination $FolderName -Recurse (with or without -Recurse its not working)
    }

    • #79475

      Participant
      Points: 0
      Rank: Member

      Hi All,

      Any advise and help on this please

      Thanks
      Rajkumar

  • #79478

    Participant
    Points: 0
    Rank: Member

    Method 1 in your first post works if the folder in the destination path exists, if it doesn't exist copy-item creates a file with the same name. With your script you need to create a folder with the current date as name before you try to copy files to it.

    • #79517

      Participant
      Points: 0
      Rank: Member

      Eric,

      It worked thanks for the help. highly appreciated.

      Rajkumar

The topic ‘Get-ChidItem Piped to Copy-Item’ is closed to new replies.