我对PowerShell还是陌生的,并且陷入了某种困境。
如何使用PowerShell根据文件名移动文件?
移至0001_ddmmyy_username[active1]_L_kkkkk.pdf
C:\ Users \ abc \ London
移至0001_ddmmyy_jacky[active1]_R_kkkkk.pdf
C:\ Users \ abc \ Russia
移至0001_ddmmyy_jim[active1]_P_kkkkk.pdf
C:\ Users \ abc \ Poland
我使用以下代码将文件移动到名为London的文件夹中。如果文件名带有文本[kkkk]的方括号,则失败。
gci 'C:\Users\abc' -Recurse -Include "*_L*" | %{ Move-Item $_.FullName 'C:\Users\abc\London'}
我该如何自动化?
Windows 10 64位。PowerShell的5。
如何将使用基于文件名前缀的文件的PowerShell gci
,foreach
,if
,-like
,move-item
和-literalpath
移动0001_ddmmyy_username[active1]_L_kkkkk.pdf
到C:\Users\abc\London
移动0001_ddmmyy_jacky[active1]_R_kkkkk.pdf
到C:\Users\abc\Russia
移动0001_ddmmyy_jim[active1]_P_kkkkk.pdf
到C:\Users\abc\Poland
改变$env:userprofile\desktop
。-whatif
满意时将其删除。
$items = gci -recurse -file *_L_*, *_R_*, *_P_*
foreach($item in $items) {
# Change $env:userprofile\desktop
if($item -like "*_L_*") {
Move-Item -LiteralPath $item "$env:userprofile\desktop\London" -whatif
} elseif($item -like "*_R_*") {
Move-Item -LiteralPath $item "$env:userprofile\desktop\Russia" -whatif
} elseif ($item -like '*_P_*') {
Move-Item -LiteralPath $item "$env:userprofile\desktop\Poland" -whatif
}
}
评论:
使用某些cmdlet,当您使用时会得到奇怪的结果,-Path
并且完整文件名的任何部分都包含[]。解决方法是使用-LiteralPath
参数。见Get-Help Move-Item -Parameter *path*
– Lee_Dailey
如何使用PowerShell gci,foreach,if,-like,move-item和-literalpath基于文件基本名移动文件
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句