使用计划任务根据文件名将文件移动到特定的 YY-MM 文件夹中

Use a scheduled task to move files into specific YY-MM folders based on file name

我正在尝试使用每晚运行的计划任务将数千个文件移动到 YYYY-MM 文件夹中。这些文件当前存储在这里:

C:\Users\USER\Desktop\New folder

我希望它们这样存储:

C:\Users\USER\Desktop\New folder\YYYY\MM\

例如:

C:\Users\USER\Desktop\New folder19

脚本将创建 YYYY\MM 文件夹(如果它们尚不存在)。文件名格式如下:

Status_20200116_001058.txt

所以对于上面的文件,YYYY是2020,MM是01

我在Stack上找到了一个PS的脚本,修改成下面的,但是我之前没用过PS,有点迷茫:

$Files_Folder = "C:\Users\USER\Desktop\New folder"
get-childitem | % {
    $file = $_.FullName
    $month = $date.month
    $year = $date.year

    new-item -type Directory -path "$Files_Folder$year$month"
    move-item $file "$Files_Folder$year$month"
}

运行 以上结果 PS 在上面的位置创建了一大堆文件夹和文件,我无法删除:

任何帮助将不胜感激

编辑:所以我尝试了 @Wasif Hasan 的脚本,如下所示:

$Files_Folder = "C:\Users\USER\Desktop\New folder1"
get-childitem | % {
    $file = $_.FullName
    $month = $file.substring(12, 2)
    $year = $file.substring(8, 4)
    $folder = Join-Path -Path "$($Files_Folder)" -ChildPath "$($Year)"
    $folder = Join-Path -Path "$($folder)" -ChildPath "$($Month)"
    $Exists = Test-Path "$($Folder)"
    If (!$Exists) { New-Item -Type directory -Path "$($Folder)" }
   Move-Item "$($File)" "$($Folder)"
}

但是我在 PS:

中得到了一堆错误

编辑:刚刚尝试了@Pavithran G 的脚本,修改为使用年和月变量而不是月和日期:

$loc = "C:\Users\nazadmin\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",''
$Month = $filename -replace "Status_\d{4}(\d{2})[\d_]*.txt",''
$folder = Join-Path -Path  $loc -ChildPath $Year
$folder = Join-Path -Path $folder -ChildPath $Month
$Exists = Test-Path $folder
If (!$Exists) 
{ 
    New-Item -Type directory -Path $folder 
}
Move-Item $outfile $folder
}

这非常有效!

编辑:刚意识到如果 YY 文件夹已经存在,脚本会尝试在 YY 文件夹中创建另一个 YY 文件夹,并抛出错误:

Move-Item : Access to the path 'C:\Users\USER\Desktop\New folder118' is denied.
At line:14 char:10
+ Move-Item <<<<  $outfile $folder
+ CategoryInfo          : WriteError: (C:\Users\USER...ew folder118:DirectoryInfo) [Move-Item], IOException
+ FullyQualifiedErrorId : MoveDirectoryItemIOError,Microsoft.PowerShell.Commands.MoveItemCommand

有什么办法可以避免这种情况?

编辑:将post一个新问题使其更清楚 干杯

$date 未声明为日期变量 (Get-Date)。使用 Join-Path 安全地创建路径并且您没有使用 Test-Path 检查文件夹是否存在。使用这个:

$date = Get-Date
$Files_Folder = "C:\Users\USER\Desktop\New folder"
get-childitem | % {
    $file = $_.FullName
    $month = $date.month
    $year = $date.year
    $folder = Join-Path -Path "$($Files_Folder)" -ChildPath "$($Year)"
    $folder = Join-Path -Path "$($folder)" -ChildPath "$($Month)"
    $Exists = Test-Path "$($Folder)"
    If (!$Exists) { New-Item -Type directory -Path "$($Folder)" }
   Move-Item "$($File)" "$($Folder)"
}

下面的脚本会根据that.So中的文件名创建文件夹,定时任务不需要添加,你可以随时运行,它会自动创建文件夹,但是不要不要更改您之前提到的文件名的格式。

$loc = "C:\Users\USER\Desktop\New folder"
$files = Get-ChildItem -Path $loc
for ($i=0; $i -lt $files.Count; $i++) 
{
$outfile = $files[$i].FullName
$filename = Split-Path -Path $outfile -Leaf -Resolve
$Month = $filename -replace "Status_\d{4}(\d{2})[\d_]*.txt",''
$date = $filename -replace "Status_\d{6}(\d{2})[\d_]*.txt",''
$folder = Join-Path -Path  $loc -ChildPath $Month
$folder = Join-Path -Path $folder -ChildPath $date
$Exists = Test-Path $folder
If (!$Exists) 
{ 
    New-Item -Type directory -Path $folder 
}
Move-Item $outfile $folder
}

确保,运行 Powershell 脚本处于管理员模式,因为有时它需要管理员权限才能移动或复制文件