powershell - 如何修改此 PS 脚本以将文件移动到正确的文件夹中?
问题描述
我目前的 PS 脚本是:
$loc = "C:\Users\USER\Desktop\New folder1"
$files = Get-ChildItem -Path $loc
for ($i=0; $i -lt $files.Count; $i++)
{
$outfile = $files[$i].FullName
$filename = Split-Path -Path $outfile -Leaf -Resolve
$Year = $filename -replace "Status_\d{0}(\d{4})[\d_]*.txt",'$1'
$Month = $filename -replace "Status_\d{4}(\d{2})[\d_]*.txt",'$1'
$folderYYYY = Join-Path -Path $loc -ChildPath $Year
$folderMM = Join-Path -Path $folderYYYY -ChildPath $Month
$ExistsYYYY = Test-Path $folderYYYY
$ExistsMM = Test-Path $folderMM
If (!$ExistsYYYY)
{
New-Item -Type directory -Path $folderYYYY
}
If (!$ExistsMM)
{
New-Item -Type directory -Path $folderMM
}
Move-Item $outfile $folderMM
}
该脚本应该将当前驻留在 $loc 目录中的文件移动到 YYYY-MM 子文件夹中。如果 YYYY 或 MM 子文件夹不存在,则会创建它。如果子文件夹路径已存在,则将文件移动到此子文件夹中。
文件名始终采用以下格式:
Status_20180215_074559.txt
所以对于上面的文件,YYYY 会映射到 2018,MM 会映射到 02
通过测试脚本 - 如果 YYYY 子文件夹不存在,则脚本运行良好。如果 YYYY 子文件夹已经存在,那么我最终会得到脚本创建的以下路径:
C:\Users\USER\Desktop\New folder1\2018
C:\Users\USER\Desktop\New folder1\2018\02
C:\Users\USER\Desktop\New folder1\2018\2018
文件被上面的脚本移动到下面的路径中:
C:\Users\USER\Desktop\New folder1\2018\02
我还收到以下错误:
Move-Item : Access to the path 'C:\Users\USER\Desktop\New folder1\2018' is denied.
At line:14 char:10
+ Move-Item <<<< $outfile $folder
+ CategoryInfo : WriteError: (C:\Users\USER...ew folder1\2018:DirectoryInfo) [Move-Item], IOException
+ FullyQualifiedErrorId : MoveDirectoryItemIOError,Microsoft.PowerShell.Commands.MoveItemCommand
任何停止创建以下路径的方法:
C:\Users\USER\Desktop\New folder1\2018\2018
另外,如何解决抛出的错误?
非常感谢
编辑:尝试了@Theo 脚本,但我遇到了以下问题:
然后我删除了所有评论和空行,认为这可能会在某处引起一些问题,但随后出现以下错误:
因此,在其他人的帮助下,我的脚本的问题是 $files 行 - 它应该只查找文件:
$files = Get-ChildItem -Path "$loc\*.txt"
此外,@Theo 提供的修改后的 PS 脚本也很有效,因为我有 PS 版本 2
解决方案
您的代码非常混乱且过于复杂。无需单独检查文件目标的每个部分,因为New-Item
cmdlet 可以一次性创建完整路径。
Get-Childitem
此外,通过管道传递using的结果ForEach-Object
并在该循环中使用$_
自动变量要简单得多。
像这样的东西:
$loc = "C:\Users\USER\Desktop\New folder1"
Get-ChildItem -Path $loc -File -Filter 'Status_*' | # get a list of files with names starting with 'Status_'
Where-Object { $_.BaseName -match '_\d{8}_\d{6}$' } | # filter files that have the '_date_time' format
ForEach-Object { # loop through
# join the year and month as partial destination path
$targetpath = '{0}\{1}' -f $_.Name.Substring(7,4), $_.Name.Substring(11,2)
# ceate the full destination path for the file
$destination = Join-Path -Path $loc -ChildPath $targetpath
# check if the path exists and if not, create it
if (!(Test-Path -Path $destination -PathType Container)) {
$null = New-Item -Path $destination -ItemType Directory
}
# now move the file to the destination path
$_ | Move-Item -Destination $destination
}
编辑
看来您使用的是 PowerShell 2.0 版,然后该-File
开关不存在。对于该版本,您需要像这样调整代码:
$loc = "C:\Users\USER\Desktop\New folder1"
Get-ChildItem -Path $loc -Filter 'Status_*' |
Where-Object { !$_.PSIsContainer -and $_.BaseName -match '_\d{8}_\d{6}$' } |
ForEach-Object {
# join the year and month as partial destination path
$targetpath = '{0}\{1}' -f $_.Name.Substring(7,4), $_.Name.Substring(11,2)
# ceate the full destination path for the file
$destination = Join-Path -Path $loc -ChildPath $targetpath
# check if the path exists and if not, create it
if (!(Test-Path -Path $destination -PathType Container)) {
$null = New-Item -Path $destination -ItemType Directory
}
# now move the file to the destination path
$_ | Move-Item -Destination $destination
}
推荐阅读
- excel - Excel VBA 错误处理程序找不到文件...如何跳过它?
- c# - 当我多次调用同一个函数时出现 StackOverflowException
- python - 处理 args 字典的最佳实践:解压缩成单个变量或传递字典?
- java - 使用 ProcessBuilder/RunTime 从 java 调用 easy_install 应用程序
- haskell - 如何在 Haskell 自动微分库“ad”中添加自定义渐变?
- architecture - 我们应该在 DDD 中使用“按功能打包”结构吗?
- android - 接受邀请后 Play 管理中心错误 403
- android - 如何在保留父 Firebase 的同时删除子节点
- python - 优化python数据框
- php - 结帐后是否可以使用 Woocommerce 会话?