😎

PowerShellファイルコピー(備忘録)

2023/01/24に公開

#同フォルダにあるファイルの先頭文字を読み取り、指定されたフォルダへ移動するスクリプト
[string]$Idoumoto_File = $null
[string]$Idoumoto_Path = 'C:\Users\username_MyDoc\powershell\test'
[string]$Idousaki1 = 'C:\Users\username_MyDoc\powershell\test\idousaki1'
[string]$Idousaki2 = 'C:\Users\username_MyDoc\powershell\test\idousaki2'
[string]$Idousaki3 = 'C:\Users\username_MyDoc\powershell\test\idousaki3'

$FILES = Get-ChildItem -path C:\Users\q3pta_MyDoc\powershell\test | Where-Object { ! $.PSIsContainer -and $.Name -like "?@??@???@????@?????@??????"}

#1,2,4 = Idousaki1
#1,3,4 = Idousaki2
#4,5 = Idousaki3

#/////失敗例/////////////////////////////////////
#$idoumoto_file = New-Object System.Collections.ArrayList #可変長
#$idoumoto_file.IsFixedSize | Write-Host #可変長=false
#idoumoto_file.Add("Idoumoto_File")
#/////失敗例2/////////////////////////////////////
#idoumoto_file[j] += Join-Path "Idoumoto_Path" "A_FILE"

$i = 1
foreach ($A_FILE in $FILES) {

if($A_FILE.Name -like "[1,2,4]*"){
$Idoumoto_File = Join-Path "$Idoumoto_Path" "$A_FILE"
Copy-Item "$Idoumoto_File" "$Idousaki1"
Write-Host ""$Idoumoto_File"を"$Idousaki1"へ移動しました。`r`n"
}

$i++

}

Discussion