Can't copy the file in Powershell
I am writing small script that copy files to special folder. The problem is in Copy command. It's claim me that I use wrong syntaxis to copy
$files = dir -r -path "Z:\graphics\" -i *.*
foreach ($file in $files)
{
copy -path $file Z:\SatData\graphics\LastDays\
}
Also I want to create script that calculate size of files created 1day ago. I try to do next:
$today = Get-Date
$now = $today.Day
$now
$lastdays = $today.AddDays(-1)
$lastdays
$files = dir -r -path "Z:\graphics\" -i *.*
foreach ($file in $files)
{
if ($file.CreationTime -eq $lastdays) # if file was create yesterday calculate it
{
$sum
$sum = $sum + $file.length
$sum/1MB
$file.CreationTime
}
else {}
}
The script simply do not find any files created yesterday, and I do now see any output. It's work only if set not -eq, but -lt but yesterday created files are present in folde开发者_开发问答r
Use the pipeline:
dir -r -path Z:\graphics | copy-item -dest Z:\SatData\graphics\LastDays
To calculate files sizes (in MB):
$files = dir -r -path Z:\graphics |
where {-not $_.PSIsContainer -and $_.CreationTime.Date -eq (Get-Date).AddDays(-1).Date} |
measure -sum
$files.sum/1mb
In your copy statement you need to reference the actual path because dir
is returning a fileinfo object. For example: copy -path $file.FullName Z:\SatData\graphics\LastDays\
The second part of your question $now
is a day number not a date. Try something like $yesterday = (get-date).adddays(-1).date
and change your comparison to if ($file.CreationTime.date -eq $yesterday)
You need to use the date property because it sets the time to 00:00:00 for the comparison. If you don't you'll never match.
For the first part, use $file.FullName instead of just $file.
精彩评论