【问题标题】:How to move files based on filename using PowerShell?如何使用 PowerShell 根据文件名移动文件?
【发布时间】:2020-05-13 00:51:17
【问题描述】:

我是 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'}

如何实现自动化?

【问题讨论】:

  • 对于某些 cmdlet,当您使用 -Path 并且完整文件名的任何部分包含 [] 时,您会得到奇怪的结果。它也发生在其他一些字符上,但这些是我记得的唯一字符。解决方法是使用-LiteralPath 参数而不是-Path 参数。 您正在使用 -Path 参数,因为您没有指定任何内容 - cmdlet 默认为 -Path 查看 Get-Help Move-Item -Parameter *path* 的输出。

标签: powershell


【解决方案1】:
  1. 有多种方法:

获取位置

https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.management/get-location?view=powershell-7

gci (Get-Location).Path

或者干脆.\

gci .\

另外,如果没有指定,gci 将默认为当前路径。所以下面会得到当前文件夹:

gci
  1. Move-Item cmdlet 中使用-LiteralPath
Move-Item -LiteralPath $sourcePath $destinationPath
  1. 我会做类似下面的事情:
# Note I'm using -File so it only returns files
$items = gci .\ -Recurse -File -Include *_L*, *_R*, *_P*

foreach($item in $items) {
  # Could utilize something cleaner/better but this will do
  if($item.Name -like "*_L*") {
    Move-Item -LiteralPath $item 'C:\Users\abc\London'
  } elseif($item.Name -like "*_R*") {
    Move-Item -LiteralPath $item 'C:\Users\abc\Russia'
  } elseif ($item.Name -like "*_P*") {
    Move-Item -LiteralPath $item 'C:\Users\abc\Poland'
  } else {
    # Unexpected output
    Write-Host "The path did not match what was expected"
    Write-Host $item.Name
  }
}

【讨论】:

  • 你好,我的朋友,这段代码给我的输出与我的 _L 代码完全相同。当文件名在 _L 或 _R 或 _P 之前有一些带有 [xxxxxx] 的文本时,它不会移动它。您能否就创建文件夹 London 并将所有 _L 移入其中提出建议。
  • 嘿,根据@somebadhat 的回复,使用 -LiteralPath 将解决括号问题。
  • 他的解决方案工作得非常好,只是看到了一个小的改进。 1.检查并创建.\中的文件夹,如果文件存在则跳过或覆盖。
【解决方案2】:

Windows 10 64 位。 PowerShell 5。

如何使用 PowerShell gciforeachif-likemove-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 根据文件基名移动文件

【讨论】:

  • 朋友你好,它运行良好,一个小补充是有 1.如果文件夹不存在,则创建文件夹,然后根据当前目录移动它 2.检查是否存在同名文件和如果是,请不要移动或更换它非常感谢您的帮助
  • 注意到并抱歉,因为我是论坛的新手。
  • @somebadhat 我创建了一个新线程。你能帮忙吗?
  • @cur 从你的新帖子中对我发表评论。
猜你喜欢
  • 2019-12-28
  • 2015-10-09
  • 2019-10-17
  • 2021-08-23
  • 2013-11-20
  • 1970-01-01
  • 2016-02-07
  • 2014-06-06
相关资源
最近更新 更多