无法在 Powershell 中复制文件
我正在编写将文件复制到特殊文件夹的小脚本。问题出在复制命令中。它声称我使用错误的语法来复制
$files = dir -r -path "Z:\graphics\" -i *.*
foreach ($file in $files)
{
copy -path $file Z:\SatData\graphics\LastDays\
}
此外,我想创建计算 1 天前创建的文件大小的脚本。我尝试下一步:
$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 {}
}
该脚本根本找不到昨天创建的任何文件,而我现在看到了任何输出。仅当设置不是 -eq 而是 -lt 时才有效 但昨天创建的文件存在于文件夹中
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 folder
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
使用管道:
计算文件大小(以 MB 为单位):
Use the pipeline:
To calculate files sizes (in MB):
在复制语句中,您需要引用实际路径,因为 dir 返回一个 fileinfo 对象。例如:copy -path $file.FullName Z:\SatData\graphics\LastDays\
你的问题的第二部分
$now
是天数而不是日期。尝试类似$yesterday = (get-date).adddays(-1).date
并将比较更改为if ($file.CreationTime.date -eq $yesterday)
您需要使用日期属性,因为它将时间设置为 00:00:00 进行比较。如果你不这样做,你永远不会匹配。
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 toif ($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.
对于第一部分,使用 $file.FullName 而不是仅使用 $file。
For the first part, use $file.FullName instead of just $file.