[英]Moving files named with a number
請協助,我只想移動文件名上帶有 1 或 2 的文件。 例如:text1.txt,text2.txt,text3.txt,text4.txt,text5.txt 只有 text1.txt 和 text2.txt 必須移動。 我的 PowerShell 腳本復制了所有文件。
由於您的問題不清楚,我不確定這是否是您要找的。
但是,以下示例可能會有用。 我創建了一個名為pattern1
的數組,其中包含 2 個元素: text1.txt
和text2.txt
。 當您運行腳本時,它會要求您按回車鍵繼續。 執行完成后,您會注意到它會打印出過程詳細信息(文件夾中有多少文件以及復制了多少文件):
$SourceFolder = "D:\TestFolder\"
$targetFolder = "D:\TestFolder\Destination\"
$numFiles = (Get-ChildItem -Path $SourceFolder -Filter *.TXT).Count
$pattern1 = "text1.txt","text2.txt"
$i=0
clear-host;
Write-Host 'This script will copy ' $numFiles ' files from ' $SourceFolder ' to ' $targetFolder
Read-host -prompt 'Press enter to start copying the files'
Get-ChildItem -Path $SourceFolder -Filter *.TXT | %{
[System.IO.FileInfo]$destination = (Join-Path -Path $targetFolder -ChildPath $_.Name.replace("_","\"))
if(!(Test-Path -Path $destination.Directory )){
New-item -Path $destination.Directory.FullName -ItemType Directory
}
[int]$percent = $i / $numFiles * 100
if($pattern1.Contains($_.Name)){
copy-item -Path $_.FullName -Destination $Destination.FullName
Write-Progress -Activity "Copying ... ($percent %)" -status $_ -PercentComplete $percent -verbose
$i++}
}
Write-Host 'Total number of files read from directory '$SourceFolder ' is ' $numFiles
Write-Host 'Total number of files that was copied to '$targetFolder ' is ' $i
Read-host -prompt "Press enter to complete..."
clear-host;
為了自動化,它可以更通用。 例如,您可以修改它以檢查數組的元素(在本例中為pattern1
),如果任何文件在其名稱中包含該元素(數組/ pattern1
),則復制文件。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.