Get-ChidItem Piped to Copy-Item

This topic contains 8 replies, has 4 voices, and was last updated by  Rajkumar Balasakuri 1 week, 3 days ago.

  • Author
    Posts
  • #78943

    Rajkumar Balasakuri
    Participant

    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

    Paul DeArment Jr
    Participant

    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

    Rajkumar Balasakuri
    Participant

    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

    Richard Siddaway
    Moderator

    Can you show the EXACT output of this command

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

    Thanks

    • #78998

      Rajkumar Balasakuri
      Participant

      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

    Rajkumar Balasakuri
    Participant

    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

      Rajkumar Balasakuri
      Participant

      Hi All,

      Any advise and help on this please

      Thanks
      Rajkumar

  • #79478

    Erik Sundin
    Participant

    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

      Rajkumar Balasakuri
      Participant

      Eric,

      It worked thanks for the help. highly appreciated.

      Rajkumar

You must be logged in to reply to this topic.