誤解のために変更する必要があるスクリプトがあります。製造フロアにワークステーションがあり、次の構造のファイルを作成します - 04_R_____"109402"0076_9999992_35_401_"01_20121107"_134029_0667.I00.asd (ファイル名の引用部分は解析する必要がある部分。
ファイル名の最初の部分を含む配列を既に作成しており、powershell プログラムはそのデータを解析できます。ただし、ファイル名の 2 番目の部分には、パーツ番号、テスト ベンチ番号 (01、02、03 など)、および日付で作成されたフォルダー構造が必要です。フォルダーが存在しない場合は、一致する場合にのみフォルダーを作成します。
私の現在のスクリプトはプレフィックスでフィルタリングし(これは間違っています)、すべてのフォルダーを毎日作成します(一致ではありません)。部分文字列を使用して、01、02、03 などをキャッチするために非常に多くの文字を除外したいと考えています。ホイールを再作成せずに、いくつかの変更を加えて現在のコードを使用することは可能でしょうか? 私のテストコードはすべて含まれており、どんな助けでも大歓迎です!
- 109402 = パーツリスト
- 01 - テストベンチマシン
- 20121107 - 日付
コード:
$source ="\\127.0.0.1\baunhof\*"
$archive = "\\127.0.0.1\error\\"
#$past=(Get-date).AddDays(-2)
$destination ="\\127.0.0.1\TestFolder1\\"
$destination1="\\127.0.0.1\TestFolder2\\"
$destination2="\\127.0.0.1\TestFolder3\\"
$destination3="\\127.0.0.1\TestFolder4\\"
#array for all destinations
$destination_array=@("$destination", "$destination1", "$destination2", "$destination3")
#creates folder yyyy/mm/dd
#$today = (Get-date -format yyyy/MM/dd)
#new-item -type directory ($today)
$DTS = ( get-date ).ToString('yyyy/MM/dd')
#array for file prefix
$File_Array_8HP70=@("*108701*")
$File_Array_8HP70X=@("*108702*")
$File_Array_9HP48=@("*109401*", "*1094080*", "*1094090*")
$File_Array_9HP48X=@("*109402*", "*1094091*", "*1094082*", "*1094092*")
#test bench number array filter
$test_bench_01=@("*_01_*")
$test_bench_02=@("*_02_*")
$test_bench_03=@("*_03_*")
$test_bench_04=@("*_04_*")
#Error log function: will write to application on server
function Write-EventLog {
param([string]$msg = "Default Message", [string]$type="Information")
$log = New-Object System.Diagnostics.EventLog
$log.set_log("Application")
$log.set_source("PSscript")
$log.WriteEntry($msg,$type)
}
Write-Eventlog "Acoustic file parse program has started"
# if statement checks if $destination_array[0] is false then new item
$destination_array[0] = "\\127.0.0.1\TestFolder1\today\"
If (!(Test-Path -path $destination_array[0])) {
new-item -type directory "\\127.0.0.1\TestFolder1\$DTS"
new-item -type directory "\\127.0.0.1\TestFolder1\P01\$DTS"
new-item -type directory "\\127.0.0.1\TestFolder1\P02\$DTS"
new-item -type directory "\\127.0.0.1\TestFolder1\P03\$DTS"
new-item -type directory "\\127.0.0.1\TestFolder1\P04\$DTS"
}
$destination_array[1] = "\\127.0.0.1\TestFolder2\today\"
If (!(Test-Path -path $destination_array[1])) {
new-item -type directory "\\127.0.0.1\TestFolder2\$DTS\"
new-item -type directory "\\127.0.0.1\TestFolder2\P01\$DTS"
new-item -type directory "\\127.0.0.1\TestFolder2\P02\$DTS"
new-item -type directory "\\127.0.0.1\TestFolder2\P03\$DTS"
new-item -type directory "\\127.0.0.1\TestFolder2\P04\$DTS"
}
$destination_array[2] = "\\127.0.0.1\TestFolder3\today\"
If (!(Test-Path -path $destination_array[2])) {
new-item -type directory "\\127.0.0.1\TestFolder3\$DTS\"
new-item -type directory "\\127.0.0.1\TestFolder3\P01\$DTS"
new-item -type directory "\\127.0.0.1\TestFolder3\P02\$DTS"
new-item -type directory "\\127.0.0.1\TestFolder3\P03\$DTS"
new-item -type directory "\\127.0.0.1\TestFolder3\P04\$DTS"
}
$destination_array[3] = "\\127.0.0.1\TestFolder4\today\"
If (!(Test-Path -path $destination_array[3])) {
new-item -type directory "\\127.0.0.1\TestFolder4\$DTS\"
new-item -type directory "\\127.0.0.1\TestFolder4\P01\$DTS"
new-item -type directory "\\127.0.0.1\TestFolder4\P02\$DTS"
new-item -type directory "\\127.0.0.1\TestFolder4\P03\$DTS"
new-item -type directory "\\127.0.0.1\TestFolder4\P04\$DTS"
}
$destination="\\127.0.0.1\TestFolder1\$DTS"
$destination1="\\127.0.0.1\TestFolder2\$DTS"
$destination2="\\127.0.0.1\TestFolder3\$DTS"
$destination3="\\127.0.0.1\TestFolder4\$DTS"
$destination_array=@ ("$destination", "$destination1", "$destination2", "$destination3")
# filter works below - need to use array
#$files = get-childitem $source -filter "108701*" -recurse
#foreach ($file in $files)
#{move-item $file.fullname $destination_array[0] -force}
$File_Array_8HP70_start = $File_Array_8HP70 | % {$_+"*"}
$files = get-childitem $source -include $File_Array_8HP70_start -recurse
foreach ($file in $files) {
move-item $file.fullname $destination_array[0] -force
}
#filter test bench
$files01 = gci $destination_array[0] -filter "01_*" -recurse
$files02 = gci $destination_array[0] -filter "02_*" -recurse
$files03 = gci $destination_array[0] -filter "03_*" -recurse
$files04 = gci $destination_array[0] -filter "04_*" -recurse
$destination_array[0]="\\127.0.0.1\TestFolder1\P01\$DTS"
foreach ($file in $files01) {
move-item $file.fullname $destination_array[0] -force
}
$destination_array[0]="\\127.0.0.1\TestFolder1\P02\$DTS"
foreach ($file in $files02) {
move-item $file.fullname $destination_array[0] -force
}
$destination_array[0]="\\127.0.0.1\TestFolder1\P03\$DTS"
foreach ($file in $files03) {
move-item $file.fullname $destination_array[0] -force
}
$destination_array[0]="\\127.0.0.1\TestFolder1\P04\$DTS"
foreach ($file in $files04) {
move-item $file.fullname $destination_array[0] -force
}
$File_Array_8HP70X_start = $File_Array_8HP70X | % {$_+"*"}
$files = get-childitem $source -include $File_Array_8HP70X_start -recurse
foreach ($file in $files) {
move-item $file.fullname $destination_array[1] -force
}
#$files02 = gci $destination_array[1] -filter "02_*" -recurse
$files01 = gci $destination_array[1] -filter "01_*" -recurse
$files02 = gci $destination_array[1] -filter "02_*" -recurse
$files03 = gci $destination_array[1] -filter "03_*" -recurse
$files04 = gci $destination_array[1] -filter "04_*" -recurse
$destination_array[1]="\\127.0.0.1\TestFolder2\P01\$DTS"
foreach ($file in $files01) {
move-item $file.fullname $destination_array[1] -force
}
$destination_array[1]="\\127.0.0.1\TestFolder2\P02\$DTS"
foreach ($file in $files02) {
move-item $file.fullname $destination_array[1] -force
}
$destination_array[1]="\\127.0.0.1\TestFolder2\P03\$DTS"
foreach ($file in $files03) {
move-item $file.fullname $destination_array[1] -force
}
$destination_array[1]="\\127.0.0.1\TestFolder2\P04\$DTS"
foreach ($file in $files04) {
move-item $file.fullname $destination_array[1] -force
}
$File_Array_9HP48_start = $File_Array_9HP48 | % {$_+"*"}
$files = get-childitem $source -include $File_Array_9HP48_start -recurse
foreach ($file in $files) {
move-item $file.fullname $destination_array[2] -force
}
#$files03 = gci $destination_array[2] -filter "03_*" -recurse
$files01 = gci $destination_array[2] -filter "01_*" -recurse
$files02 = gci $destination_array[2] -filter "02_*" -recurse
$files03 = gci $destination_array[2] -filter "03_*" -recurse
$files04 = gci $destination_array[2] -filter "04_*" -recurse
$destination_array[2]="\\127.0.0.1\TestFolder3\P01\$DTS"
foreach ($file in $files01) {
move-item $file.fullname $destination_array[2] -force
}
$destination_array[2]="\\127.0.0.1\TestFolder3\P02\$DTS"
foreach ($file in $files02) {
move-item $file.fullname $destination_array[2] -force
}
$destination_array[2]="\\127.0.0.1\TestFolder3\P03\$DTS"
foreach ($file in $files03) {
move-item $file.fullname $destination_array[2] -force
}
$destination_array[2]="\\127.0.0.1\TestFolder3\P04\$DTS"
foreach ($file in $files04) {
move-item $file.fullname $destination_array[2] -force
}
$File_Array_9HP48X_start = $File_Array_9HP48X | % {$_+"*"}
$files = get-childitem $source -include $File_Array_9HP48X_start -recurse
foreach ($file in $files) {
move-item $file.fullname $destination_array[3] -force
}
#$files04 = gci $destination_array[3] -filter "04_*" -recurse
$files01 = gci $destination_array[3] -filter "01_*" -recurse
$files02 = gci $destination_array[3] -filter "02_*" -recurse
$files03 = gci $destination_array[3] -filter "03_*" -recurse
$files04 = gci $destination_array[3] -filter "04_*" -recurse
$destination_array[3]="\\127.0.0.1\TestFolder4\P01\$DTS"
foreach ($file in $files01) {
move-item $file.fullname $destination_array[3] -force
}
$destination_array[3]="\\127.0.0.1\TestFolder4\P02\$DTS"
foreach ($file in $files02) {
move-item $file.fullname $destination_array[3] -force
}
$destination_array[3]="\\127.0.0.1\TestFolder4\P03\$DTS"
foreach ($file in $files03) {
move-item $file.fullname $destination_array[3] -force
}
$destination_array[3]="\\127.0.0.1\TestFolder4\P04\$DTS"
foreach ($file in $files04) {
move-item $file.fullname $destination_array[3] -force
}
#move files to c:\Error if older than 2 days
$file_2 = gci $source -recurse|where {$_.LastWriteTime -lt (get-date).AddDays(-2)}
foreach ($file in $file_2) {
move-item $file.fullname $archive -force
}
Write-Eventlog "Acoustic file parse program has completed"