简体   繁体   English

创建一个exe脚本到select并移动我电脑上的所有图像

[英]Create an exe script to select and move all images on my computer

This PowerShell script is intended to copy photos from one or more source directories to a destination directory, organizing them by image type (JPEG, PNG, BMP, and HEIC).此 PowerShell 脚本旨在将照片从一个或多个源目录复制到目标目录,并按图像类型(JPEG、PNG、BMP 和 HEIC)组织它们。

It asks the user to enter the paths of the source directories and the source directories to exclude, as well as the path of the destination directory.它要求用户输入源目录的路径和要排除的源目录,以及目标目录的路径。 These paths are stored in the variables $sources, $excluded, and $destination, respectively.这些路径分别存储在变量 $sources、$excluded 和 $destination 中。

The script then converts the string contained in $sources into an array using the -split instruction, which allows for splitting a string based on a specified separator (here, a comma).然后,该脚本使用 -split 指令将 $sources 中包含的字符串转换为数组,该指令允许根据指定的分隔符(此处为逗号)拆分字符串。 If the user did not specify any source directories to exclude, the $excluded variable is initialized to an empty array using the Where-Object instruction.如果用户没有指定要排除的任何源目录,则使用 Where-Object 指令将 $excluded 变量初始化为一个空数组。

The path of the destination directory is added to the $excluded array in order to avoid copying the photos to the destination directory itself.目标目录的路径被添加到 $excluded 数组中,以避免将照片复制到目标目录本身。

The script then checks if the destination directory exists, and creates it if it does not using the New-Item instruction.然后脚本检查目标目录是否存在,如果不存在则使用 New-Item 指令创建它。 It also creates destination directories for each file type (JPEG, PNG, BMP, and HEIC).它还为每种文件类型(JPEG、PNG、BMP 和 HEIC)创建目标目录。

Finally, the script searches the computer's file system for files and copies the found photos to the destination directories according to their extension.最后,脚本会在计算机的文件系统中搜索文件,并根据扩展名将找到的照片复制到目标目录。 It uses the Get-ChildItem instruction to retrieve the files in the source directories and the Copy-Item command to copy these files to the destination directories.它使用 Get-ChildItem 指令检索源目录中的文件,并使用 Copy-Item 命令将这些文件复制到目标目录。 The Where-Object clause is used to verify that the file is not in one of the excluded source directories. Where-Object 子句用于验证文件不在排除的源目录之一中。

$sources = Read-Host "Please enter the paths of the source directories (separated by commas) :"

$excluded = Read-Host "Please enter the paths of the source directories to exclude (separated by commas) :"

$destination = Read-Host "Please enter the path of the destination directory :"

$sources = $sources -split ","

$excluded = $excluded -split "," | Where-Object {$_}

$excluded += $destination

if (!(Test-Path $destination)) { New-Item -ItemType Directory -Path $destination }

"JPG", "PNG", "BMP", "HEIC" | ForEach-Object { New-Item -ItemType Directory -Path "$destination\$_" }

foreach ($source in $sources) {
    if ($excluded -notcontains $source) {
        "jpg", "png", "bmp", "heic" | ForEach-Object {
            $extension = $_.ToUpper()
            Get-ChildItem -Recurse $source -Filter "*.$_" | Where-Object {$_.Directory.FullName -notcontains $excluded} | Copy-Item -Destination "$destination\$extension"
        }
    }
}

This script works very well except that it still adds the photos of the excluded folders?这个脚本工作得很好,只是它仍然添加了排除文件夹的照片? Why ?为什么 ?

I would use a regex for this to exclude the folders.我会为此使用正则表达式来排除文件夹。 Also I'd use Group-Object to group the found files by extension, making it easier to copy to the destination folders:此外,我会使用Group-Object按扩展名对找到的文件进行分组,从而更容易复制到目标文件夹:

$sources     = Read-Host "Please enter the paths of the source directories (separated by commas) :"
$excluded    = Read-Host "Please enter the paths of the source directories to exclude (separated by commas) :"
$destination = Read-Host "Please enter the path of the destination directory :"

$sources  = @($sources -split "," | Where-Object {$_ -match '\S'}).Trim()
$excluded = @($excluded -split "," | Where-Object {$_ -match '\S'}).Trim() + $destination

# create a regex of the folders to exclude
# each folder will be Regex Escaped and joined together with the OR symbol '|'
$notThese = ($excluded | ForEach-Object { [Regex]::Escape($_) }) -join '|'

# enumerate the paths for the specified files
Get-ChildItem -Path $sources -Recurse -File -Include "*.JPG", "*.PNG", "*.BMP", "*.HEIC" |
Where-Object{ $_.DirectoryName -notmatch $notThese } | 
Group-Object Extension |
ForEach-Object {
    $targetPath = Join-Path -Path $Destination -ChildPath $_.Name.TrimStart(".").ToUpper()
    # create the target path if this does not already exist
    $null = New-Item -Path $targetPath -ItemType Directory -Force
    # next copy the files in this group
    $_.Group | Copy-Item -Destination $targetPath -Force
}

Replace:代替:

$_.Directory.FullName -notcontains $excluded

with the following, which reverses the operands passed to the -notcontains operator :使用以下内容,它反转传递给-notcontains运算符的操作数:

$excluded -notcontains $_.Directory.FullName

Alternatively, use the related -notin operator :或者,使用相关的-notin运算符

$_.Directory.FullName -notin $excluded

Both -contains / -notcontains and -in / -notin test an array (collection) for containing a scalar (single object), through element-by-element equality testing. -notcontains -contains -in / -notin都通过逐个元素的相等性测试来测试包含标量(单个对象)的数组(集合)。 They only differ by the order of operands:它们仅在操作数的顺序上有所不同:

# -contains / -notcontains: array is the *LHS* (left-hand side) operand
@(1, 2, 3) -contains 2 # -> $true

# -in / -notin: array is the *RHS* (right-hand side)
2 -in @(1, 2, 3) # -> $true

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

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