powershell - 在 Powershell 中移动文件
问题描述
使用下面的代码作为基础(来自另一个问题)我如何在 2 个日期之间移动文件,例如:>2wks AND < 13 Months 到另一个位置。是否可以限制要移动的文件扩展名?
get-childitem -Path "E:\source" |
where-object {$_.LastWriteTime -lt (get-date).AddDays(-31)} |
move-item -destination "F:\target"
解决方案
Get-ChildItem
可以通过多种方式限制要获取的文件。首先是使用-Filter
参数:
# set up the start and end dates
$date1 = (Get-Date).AddDays(-14)
$date2 = (Get-Date).AddMonths(-13)
# using -Filter if you want to restrict to one particular extension.
Get-ChildItem -Path "E:\source" -Filter '*.ext' |
Where-Object {$_.LastWriteTime -lt $date1 -and $_.LastWriteTime -gt $date2} |
Move-Item -Destination "F:\target"
如果您需要限制多个扩展名,则无法使用该-Filter
参数执行此操作。相反,您需要使用-Include
可以采用通配符模式数组的参数。
请注意,为了使-Include
工作,您还必须使用-Recurse
开关,或者具有这样的Get-ChildItem
end路径\*
:
# using -Include if you want to restrict to more than one extensions.
Get-ChildItem -Path "E:\source\*" -Include '*.ext1', '*.ext2', '*.ext3' |
Where-Object {$_.LastWriteTime -lt $date1 -and $_.LastWriteTime -gt $date2} |
Move-Item -Destination "F:\target"
然后有一个-Exclude
参数,它-Include
也接受一个通配符参数的字符串数组,这些参数将过滤掉你不想要的扩展。
如果您需要过滤文件属性,例如 ReadOnly、Hidden 等,您可以使用该-Attributes
参数。
对于这些,请查看文档
推荐阅读
- python - 合并两个 DataFrame 的函数
- javascript - AngularJS 如何在类控制器中访问 axios 的结果?
- iphone - 如何在我的网站上播放 iPhone 5 到 X 版本的广播流?
- python - 在 pandas 中创建一个通用的 headers 字段
- android - Flutter 安装错误,“状态不佳:未来已完成”
- excel - 来自用户窗体的按钮执行抛出下标超出范围错误
- uwp - C++/CX:使用 Dispatcher 访问 UI 线程不起作用
- c - 使用 printf 在 C 中打印浮点数/双精度时指定精度
- javascript - 找不到使用 Browserify 创建的模块
- angular - Nginx反向代理不改变主机