2013-02-14 126 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而在与德国系统,给你一个日期字符串日期字符串的区域设置为2013.02.15。如果您希望日期部分由正斜杠分隔(当您在路径中使用该日期时,PowerShell将解释为路径分隔符),则需要在格式字符串中转义正斜杠:yyyy\/MM\/dd

编辑:正则表达式有2个目的:

  • 限制处理以仅那些该模式匹配的文件,并且
  • 授予访问权限的零件清单和测试台部件文件名。

的模式是从你给的例子文件名派生:

04_R___ 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:单点后跟小写字母asd(扩展名)。
  • $:字符串的结尾。
+0

您好,感谢您的回复。我正在查看您的代码,并非常感谢您花时间编写上面的代码。 – user1801422 2013-02-20 15:43:32

+0

Ansgar - 你能否用正则表达式来帮助:$ 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} \。asd $“我对这段代码进行了总体分析,因为我对.NET不是很熟悉.net再次感谢您的帮助! – user1801422 2013-02-20 18:27:45

+0

@ user1801422查看更新后的答案。我已经在注释行中给出了代码的一般分类。 – 2013-02-20 18:57:08