H 想将一些照片从一个文件夹复制到另一个文件夹。文件夹里有很多照片,但我只想复制我输入的某个文件名到多个文本框。
这是我到目前为止所做的:
` $USBFolder = "F:"
$Filname = $Textbox.Text -split ','
$ImageSource = "c:\Folder"
$电影名 | ForEach 对象 {Get-ChildItem -FIlter *.jpg | Where-Object {$_.Basename -match $Filename} | 复制项目 -Destination $USBFolder -Force} `
但它什么也没做。 请告诉我我做错了什么?
避免在单个管道中使用多种条件和 Cmdlet。
性能不佳,很难调试。
一步一步来,特别是如果你没有 PS 经验。
试试这段代码,并阅读评论。
$listOfNames = $Textbox.Text.Split(',')
$allPhotos = Get-ChildItem -Path <# The path to your folder #> -Filter *.jpg
foreach ($file in $allPhotos) {
# If the text from the list is exactly the file name, you want to use -in.
# I.E.: the list is 'superPicture', 'notSuperPic'
# and the file name is 'superPicture.jpg'.
# We want to know if the Base Name is IN that list.
if ($file.BaseName -in $listOfNames) {
Copy-Item -Path $file.FullName -Destination $USBFolder -Force
}
# If you want to know if a file name contains the text of any item of the list
# we iterate through the list and use the -like operator.
# The '*' is the wildcard here.
# I.E.: the file name is 'superMegaNeatPic.jpg'
# and the text is 'MegaNeat'
foreach ($text in $listOfNames) {
if ($file.BaseName -like "*$text*") {
Copy-Item -Path $file.FullName -Destination $USBFolder -Force
}
}
}
在您的情况下,使用 -match 并不理想,因为 -match 使用正则表达式,这会使简单文件名变得复杂。
重要的是要注意,在第一个“如果”中,我们正在检查文本是否“在”文本列表中。所以列表中的文本必须等于文件基本名称。
在第二个“如果”中,我们正在寻找包含列表中至少一个文本的文本的文件。
希望对您有所帮助!