且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

powershell根据文件名的一部分移动文件

更新时间:2022-06-06 09:01:14

这利用了 FileInfo 类,以获取与目录有关的信息.

This makes use of the FileInfo class, to get information pertaining to the directory.

$SourceFolder = "G:\queries\"
$targetFolder = "G:\queries\"

# Find all files matching *.sql in the folder specified
Get-ChildItem -Path $SourceFolder -Filter *.sql | ForEach-Object {

    # Combine the source filename and target directory
    # The source filename has all instances of _ replaced with \
    # Cast the resulting string to a FileInfo object to take advantage of extra methods
    [System.IO.FileInfo]$destination = (Join-Path -Path $targetFolder -ChildPath $_.Name.replace("_","\"))

    # Create the directory if it doesn't already exits
    if (!(Test-Path) $destination.Directory.FullName)
    { 
        New-item -Path $destination.Directory.FullName -ItemType Directory 
    }

    # Copy the source to the target directory
    copy-item -Path $_.FullName -Destination $Destination.FullName 
}