0
votes

My script is working, but I'm getting this error:

The property 'Name' cannot be found on this object. Verify that the property exists.

I'm looping to copy folders. Any idea what is going on?

    $source50 = "c:\folder\"
    $destination50 = "c:\folder1\"

    for ($i = 1; $i -le 7; $i++) {
        $d = ((Get-Date).AddDays( + $i))
        $d2 = $d.ToString("yyyy-MM-dd")
        $v = Get-ChildItem $source50 -Recurse -Include "$d2"
 foreach ($file in $v) 
      {

               if ( Test-Path  $v.FullName)


                 {  
                    if (-not (Test-Path -Path $destination50$d4)){
                    New-Item -ItemType directory -Path $destination50$d4 

                    }


                     Write-Output "Copy ok " $v.FullName     
                     $bd= Copy-Item $v.FullName -Destination $destination50$d4 
                     break 


                  }

       }

The code is giving the error:

ok C:\folder\2017-12-27
ok C:\folder\2017-12-28
The property 'Name' cannot be found on this object. Verify that the property
exists.
At line:16 char:11
+       if ($v.Name -eq $d2) {
+           ~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [], PropertyNotFoundException
    + FullyQualifiedErrorId : PropertyNotFoundStrict

ok C:\folder\2017-12-30
The property 'Name' cannot be found on this object. Verify that the property
exists.
1
check $v.GetType(). I suspect that you are finding more than one file.EBGreen
what is $v returning? Is it returning only one? I do not think so. Secondly, if yes , then hit a gettype with that and see it.Ranadip Dutta
@EBGreen Yes, I'm hiring more than one folder. I do not understand the mistake The property 'Name' cannot be found on this object. Verify that the property exists. $v.GetType Name DirectoryInfo , (System.IO.FileSys...Jorge Miguel
If you are getting more than one file then $v is an array of file objects. The file objects in the array individually each have a .Name property but the array itself does not. To access the .Name property of each individual File Object that is in the array you would need to use a loop ti iterate through each File Object in the array.EBGreen
Run under the debugger in ISE and you can learn and debug much about your results and how to access their members.Kory Gill

1 Answers

0
votes

the $v.Name throw exception because its an array object and not "System.IO." that has the property Name.

try using this code:

$source50 = "c:\folder\"
$destination50 = "c:\folder1\"

for ($i = 1; $i -le 7; $i++)
{
    $d = ((Get-Date).AddDays( + $i))
    $d2 = $d.ToString("yyyy-MM-dd")
    $v = Get-ChildItem  $source50 -Recurse -include "$d2"
    foreach($file in $v)
    {
        if ($file.Name -eq $d2)
        {
            Copy-Item $file.FullName -Destination $destination50 -Force
            Write-Host "ok" $file.FullName  | Out-File -FilePath  c:\folder\logggg.txt
        }
    }
}