2013-02-14 2 views
1

오해로 인해 스크립트를 변경해야합니다. 다음 구조로 파일을 생성하는 작업장에는 04_R _____ "109402"0076_9999992_35_401_ "01_20121107"_134029_0667.I00의 워크 스테이션이 있습니다. asd (파일 이름의 따옴표 부분은 구문 분석해야하는 부분입니다.)파일 이름을 구문 분석하고 폴더 구조를 만듭니다.

이미 파일 이름의 첫 번째 부분으로 배열을 만들었으며 powershell 프로그램이 해당 데이터를 구문 분석 할 수 있지만 파일 이름의 두 번째 부분에는 부품 번호, 테스트 벤치 번호 (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 
[email protected]("$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 
[email protected]("*108701*") 
[email protected]("*108702*") 
[email protected]("*109401*", "*1094080*", "*1094090*") 
[email protected]("*109402*", "*1094091*", "*1094082*", "*1094092*") 

#test bench number array filter 
[email protected]("*_01_*") 
[email protected]("*_02_*") 
[email protected]("*_03_*") 
[email protected]("*_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" 
[email protected] ("$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" 

답변

1

. 하지마.

하자 PowerShell은 당신을 위해 작업을 수행합니다

$DTS = (Get-Date).FormatDate('yyyy/MM/dd') 

$parts_lists = @(
    @("108701"), 
    @("108702"), 
    @("109401", "1094080", "1094090"), 
    @("109402", "1094091", "1094082", "1094092") 
) 

$destination_dirs = @(
    "\\127.0.0.1\TestFolder1", 
    "\\127.0.0.1\TestFolder2", 
    "\\127.0.0.1\TestFolder3", 
    "\\127.0.0.1\TestFolder4" 
) 

# The following regular expression defines 2 sub-matches for parts list 
# and test bench. 
$re = "^\d{2}_[A-Z]___(\d{6})\d{4}_\d{7}_\d{2}_\d{3}_(\d{2})_\d{8}_\d{6}_\d{4}\.[A-Z]\d{2}\.asd$" 

Get-ChildItem $source -Recurse | ? { $_.Name -match $re } | % { 
    # process only files that match the given regular expression 

    # iterate over all 4 parts lists 
    for ($i = 0; $i -le 3; $i++) { 
    if ($parts_lists[$i] -contains $matches[1]) { 
     # if the first sub-match (the parts list number) is found in the current 
     # parts list, construct a destination path from the corresponding base 
     # directory, the test bench number and the date. 
     $dest = Join-Path $destination_dirs[$i] -ChildPath "P$($matches[2])\$DTS" 

     # Create the destination if it doesn't exist. Creating it here ensures 
     # that a destination folder is only created when there's actually a 
     # file going into it. 
     if (-not (Test-Path -LiteralPath $dest)) { 
     New-Item -Type Directory $dest 
     } 

     # Move the file ... 
     Move-Item $_.FullName $dest -Force 
     # ... end exit from the for-loop (no need to check other parts lists 
     # once we found a match). 
     break 
    } 
    } 
} 

내가 그렇게 내 샘플 코드는 약간의 조정이 필요할 수 있지만, 그것은 당신에게 일반적인 아이디어를 줄 것이다, 내가 완벽하게 코드를 이해할 수 있는지 확실하지 않습니다.

당신이 알아야 할 한가지는 날짜 형식 yyyy/MM/dd이 지역 날짜 분리자를 사용하여 날짜 문자열을 제공한다는 것입니다. 즉, 미국 로캘이있는 시스템에서 날짜 문자열 2013/02/15을 생성하는 반면, 독일어 locale 날짜 문자열은 2013.02.15입니다. 날짜 부분을 슬래시로 구분하려면 (경로에서 해당 날짜를 사용할 때 PowerShell에서 경로 구분 기호로 해석 함) 형식 문자열에서 슬래시를 이스케이프해야합니다 (yyyy\/MM\/dd).

편집 : 정규 표현식은이 목적을 가지고 다음의 부품 목록 및 테스트 벤치 부품에 대한 액세스 권한을 부여

  • 패턴과 일치하는 파일 만에 처리를 제한하고,
  • 을 파일 이름.

패턴은 당신이 준 예를 들어 파일 이름에서 유래 :

04_R___ 109402 0076_9999992_35_401_ 01_20121107 _134029_0667.I00.

  • ^ ASD

    : 문자열의 시작입니다.
  • \d{2}_ : 밑줄이 뒤에 오는 2 자리.
  • [A-Z]___ : 하나의 대문자와 그 뒤에 3 개의 밑줄.
  • (\d{6}) : 6 자리 그룹 (부품 목록 번호를 나타냄). 그룹은 나중에 $matches[1]을 통해 액세스 할 수 있습니다.
  • \d{4}_ : 4 자리수 뒤에 밑줄이옵니다.
  • \d{7}_ : 7 자리와 그 뒤에 밑줄.
  • \d{2}_ : 밑줄이 뒤에 오는 2 자리.
  • \d{3}_ : 3 자리수 뒤에 밑줄이옵니다.
  • (\d{2})_ : 2 자리 그룹 (테스트 벤치 시스템 번호를 나타내는)과 그 뒤에 밑줄이옵니다. 그룹은 나중에 $matches[2]을 통해 액세스 할 수 있습니다.
  • \d{8}_ : 8 자리 (날짜)와 그 뒤에 밑줄이옵니다.
  • \d{6}_ : 6 자리 뒤에 밑줄이옵니다.
  • \d{4}\. : 4 자리 숫자 뒤에 단일 점이옵니다.
  • [A-Z]\d{2} : 대문자 하나와 2 자리.
  • \.asd : 단일 점 다음에 소문자 a, sd (확장자)이옵니다.
  • $ : 문자열의 끝.
+0

안녕하십니까. 답장을 보내 주셔서 감사합니다. 지금 당장 귀하의 코드를 살펴보고 위의 코드를 작성하는 데 정말로 감사드립니다. – user1801422

+0

Ansgar - regex : $ re = "^ \ d {2} _ [AZ] ___ (\ d {6}) \ d {4} _ \ d {7} _ \ d {2} _d {3} _ (\ d {2}) _ \ d {8} _ \ d {6} _ \ d {4}. [AZ] \ d {2} \. 나에게 익숙하지 않은이 코드를 가진 일반적인 고장 .net 도움을 다시 한번 감사드립니다! – user1801422

+0

@ user1801422 업데이트 된 답변보기 나는 이미 주석 줄에서 코드의 일반적인 분류를 내 렸습니다. –

관련 문제