Powershell将所有下载移动到另一个文件夹功能

2024-09-25 08:28:23 发布

您现在位置:Python中文网/ 问答频道 /正文

我想创建一个函数,根据文件“C:\Users\dGud\Desktop\TP4\Telechargement”到特定文件夹的类型,将文件移动到文件夹中

我想为PowerShell提供与此相同的功能:

for root, dirs, files in os.walk("C:\Users\dgoud\Desktop\TP4\Telechargement"):

我的PowerShell功能:

 function DeplacerDansBonDossier {
            param (
                $extension
            )

        foreach ($file in $files) {
            $extn = [IO.Path]::GetExtension($line)
            if ($extn -eq ".txt" )
            {
                Move-Item -Path "C:\Users\dgoud\Desktop\TP4\Documents"
            }

            elseif ($extn -eq ".mp3" )
            {
                Move-Item -Path "C:\Users\dgoud\Desktop\TP4\Musique"
            }

            elseif ($extn -eq ".wav" )
            {
                Move-Item -Path "C:\Users\dgoud\Desktop\TP4\Musique"
            elseif ($extn -eq ".mp4" )
            {
                Move-Item -Path "C:\Users\dgoud\Desktop\TP4\VidBo"
            }

            elseif ($extn -eq ".mkv" )
            {
                Move-Item -Path "C:\Users\dgoud\Desktop\TP4\VidBo"
            }
        }
    }
}

Tags: 文件path功能文件夹moveitemuserseq
3条回答

不言而喻,实现这一目标有很多方法。您可能根本不需要函数,因为这些文件可以直接通过管道传输到ForEach-Object循环。无论如何,如果我们正在使用一个函数,我将首先将由Get-ChildItem生成的[FileInfo]对象集合发送给该函数。这是在扩展的地方

可能看起来像这样:

Function DeplacerDansBonDossier {
    param (
        $Files
    )

foreach ( $file in $files ) {
    $extn = $File.Extension
    
    if ($extn -eq ".txt" ) {
        Move-Item -Path $File -Destination "C:\Users\dgoud\Desktop\TP4\Documents" 
    }
    elseif ($extn -eq ".mp3" ) {
        Move-Item -Path $File -Destination "C:\Users\dgoud\Desktop\TP4\Musique"
    }
    elseif ($extn -eq ".wav" ) {
        Move-Item -Path $File -Destination "C:\Users\dgoud\Desktop\TP4\Musique"
    }
    elseif ($extn -eq ".mp4" ) {
        Move-Item -Path $File -Destination "C:\Users\dgoud\Desktop\TP4\VidBo"
    }
    elseif ($extn -eq ".mkv" ) {
        Move-Item -Path $File -Destination "C:\Users\dgoud\Desktop\TP4\VidBo"
    }
}

} # End Function DeplacerDansBonDossier

在您的演示代码中,您有扩展名,但是您手头上没有文件对象,因此没有任何东西可以移动

另一个使用switch语句的变体@AbrahamZinala建议:

Function DeplacerDansBonDossier {
    param (
        $Files
    )

foreach ($file in $files) {
    $extn = $File.Extension
    
    Switch ($extn)
    {
        '.txt' { Move-Item -Path $File -Destination "C:\Users\dgoud\Desktop\TP4\Documents"; Break }
        '.mp3' { Move-Item -Path $File -Destination "C:\Users\dgoud\Desktop\TP4\Musique";   Break }
        '.wav' { Move-Item -Path $File -Destination "C:\Users\dgoud\Desktop\TP4\Musique";   Break }
        '.mp4' { Move-Item -Path $File -Destination "C:\Users\dgoud\Desktop\TP4\VidBo";     Break }
        '.mkv' { Move-Item -Path $File -Destination "C:\Users\dgoud\Desktop\TP4\VidBo";     Break }
    }    
}

} # End Function DeplacerDansBonDossier

$Files = Get-ChildItem -Path C:\temp\SourceFolder

DeplacerDansBonDossier $Files

显然Switch使事情更简洁,但没有描述性。另外,请注意break语句。与If\ElseIf不同的是Switch语句将计算多个条件和脚本块。我们用Break停止这种行为

不是多余的,但这里还有很多事情可以做。例如,我们可以通过管道启用函数,以便它可以在Get-ChildItem之后立即通过管道传输,但考虑到前面提到的ForEach-Object的潜力也会运行得差不多,这可能不值得

带有ForEach-Object的示例:

Get-ChildItem -Path C:\temp\SourceFolder |
ForEach-Object{
    foreach ( $file in $files ) {
        $extn = $File.Extension
        
        if ($extn -eq ".txt" ) {
            Move-Item -Path $_ -Destination "C:\Users\dgoud\Desktop\TP4\Documents" 
        }
        elseif ($extn -eq ".mp3" ) {
            Move-Item -Path $_ -Destination "C:\Users\dgoud\Desktop\TP4\Musique"
        }
        elseif ($extn -eq ".wav" ) {
            Move-Item -Path $_ -Destination "C:\Users\dgoud\Desktop\TP4\Musique"
        }
        elseif ($extn -eq ".mp4" ) {
            Move-Item -Path $_ -Destination "C:\Users\dgoud\Desktop\TP4\VidBo"
        }
        elseif ($extn -eq ".mkv" ) {
            Move-Item -Path $_ -Destination "C:\Users\dgoud\Desktop\TP4\VidBo"
        }
    }
}

注意:我不会在ForEach-Object脚本块中使用Switch$_Switch上下文中的操作不同

注:所有这些都未经测试。您可能需要对-Path参数使用.FullName属性,可以是$File.FullName$_.FullName*-Itemcmdlet可能会以这种方式过于挑剔

我想这就是你要找的。此函数只接受文件作为输入,请注意,我使用的是下面的Get-ChildItem -File。另一点需要考虑的是,如果在目标文件夹中存在一个具有相同名称的^ {< CD2>},{{CD3>}将抛出以下错误:

Move-Item : Cannot create a file when that file already exists.

如果要替换现有文件,可以使用-Force,如果存在同名的现有文件,可以在其中添加新条件

function DeplacerDansBonDossier {
[cmdletbinding()]
param(
    [parameter(mandatory,valuefrompipeline)]
    [System.IO.FileInfo]$File
)

    begin
    {
        $Paths = @{
            Documents = "C:\Users\dgoud\Desktop\TP4\Documents"
            Music = "C:\Users\dgoud\Desktop\TP4\Musique"
            Video = "C:\Users\dgoud\Desktop\TP4\VidBo"
        }
    }
    
    process
    {
        switch -Regex($File.Extension)
        {
            '^\.txt$'{
                $File | Move-Item -Destination $Paths['Documents']
                Write-Verbose ('Moved {0} to {1}' -f $file.Name,$Paths['Documents'])
                break
            }

            '^\.mp3$|^\.wav$'{
                $File | Move-Item -Destination $Paths['Music']
                Write-Verbose ('Moved {0} to {1}' -f $file.Name,$Paths['Music'])
                break
            }

            '^\.mp4$|^\.mkv$'{
                $File | Move-Item -Destination $Paths['Video']
                Write-Verbose ('Moved {0} to {1}' -f $file.Name,$Paths['Video'])
                break
            }
        }
    }
}

如何使用该功能:

Get-ChildItem 'C:\Users\dgoud\Desktop\TP4\Telechargement' -File | DeplacerDansBonDossier

# If you want to see which Files are being moved and their destination
# you can use -Verbose
Get-ChildItem 'C:\Users\dgoud\Desktop\TP4\Telechargement' -File | DeplacerDansBonDossier -Verbose 

如果扩展总是映射到这些路径,那么可以将它们存储在哈希表中

function DeplacerDansBonDossier {
    [CmdletBinding()]
    param (
        [Parameter(Mandatory, ValueFromPipeline)]
        [System.IO.FileInfo]
        $File,

        [Parameter(Mandatory = $false)]
        [ValidateNotNullOrEmpty()]
        [string]
        $Username = $env:USERNAME
    )

    begin {
        $extensionDestinationMappings = @{
            ".txt" = "C:\Users\$Username\Desktop\TP4\Documents"
            ".mp3" = "C:\Users\$Username\Desktop\TP4\Musique"
            ".wav" = "C:\Users\$Username\Desktop\TP4\Musique"
            ".mp4" = "C:\Users\$Username\Desktop\TP4\VidBo"
            ".mkv" = "C:\Users\$Username\Desktop\TP4\VidBo"
        }
    }

    process {
        $extension = $File.Extension

        # Check extension exists in hashtable beforehand
        if ($extensionDestinationMappings.ContainsKey($extension)) {
            Move-Item -Path $File.FullName -Destination $extensionDestinationMappings[$extension] 
        }
    }
}

相关问题 更多 >