繁体   English   中英

如何修改此 PS 脚本以将文件移动到正确的文件夹中?

[英]How do I amend this PS script to move files into the correct folders?

我当前的 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 将 map 到 2018,MM 将 map 到 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 可以一次性创建完整路径。

此外,使用ForEach-Object并在该循环中使用$_自动变量通过管道传递Get-Childitem的结果要简单得多。

像这样的东西:

$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 }

简单的老式 bash 方式,示例:

p:\DANE\telefony\Marcin\Archiwum>..\..\move-archive.bat
move IMG-20220119-WA0000.jpg 2022\2022-01\
move IMG-20220119-WA0001.jpg 2022\2022-01\

move-archive.bat代码

@echo off
rem not %var% but !var! give us access to internal loop variable value with enabledelayedexpansion.
setlocal enabledelayedexpansion
rem Run by scheduler script in p:\dane\telefony\marcin\move-archive.bat
rem p:
rem cd p:\dane\telefony\marcin\archiwum
FOR %%V IN (*.*) DO (
rem echo "****** %%V *********"
SET filedate=%%~tV
rem echo !filedate!
SET fileyear=!filedate:~6,4!
rem echo !fileyear!
SET filemonth=!filedate:~3,2!
rem echo !filemonth!
rem create directory as yyyy\yyyy-MM
mkdir !fileyear!\!fileyear!-!filemonth! 2>nul
echo move %%V !fileyear!\!fileyear!-!filemonth!\
move %%V !fileyear!\!fileyear!-!filemonth!\ >nul
)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM