我有一个需要因错误传达而改变的脚本:我们在prod floor上有工作站创建具有以下结构的文件 - 04_R _____“109402”0076_9999992_35_401_“01_20121107”_134029_0667.I00.asd(文件名的引号部分是必须要解析的部分。
我已经使用文件名的第一部分创建了一个数组,并且powershell程序能够解析该数据;但是在文件名的第二部分,必须有一个文件夹结构,由部件号,测试台编号(01,02,03等)创建,然后按日期创建。如果文件夹不存在,则仅在匹配时创建文件夹。
我当前的脚本按前缀过滤(这是错误的)并每天创建所有文件夹(不是匹配)。我想使用子字符串来排除这么多字符以捕获01,02,03等。是否可以不重新创建方向盘并使用我当前的代码进行一些更改?我的所有测试代码都包含在内,任何帮助都会得到极大的赞赏或修改!
代码:
$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"
答案 0 :(得分: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
将使用区域日期分隔符为您提供日期字符串,即在具有美国语言环境的系统上,它将生成日期字符串{{1}而在具有德语语言环境的系统上,日期字符串为2013/02/15
。如果希望日期部分用正斜杠分隔(在路径中使用该日期时PowerShell会将其解释为路径分隔符),则需要以格式字符串2013.02.15
转义正斜杠。
编辑:正则表达式有两个目的:
模式源自您提供的示例文件名:
04_R ___的 109402 0076_9999992_35_401_的 01_20121107 _134029_0667.I00.asd
yyyy\/MM\/dd
:字符串的开头。^
:2位数后跟下划线。\d{2}_
:单个大写字母后跟3个下划线。[A-Z]___
:一组6位数(代表零件清单编号)。稍后可以通过(\d{6})
。$matches[1]
:4位数字后跟下划线。\d{4}_
:7位数后跟下划线。\d{7}_
:2位数后跟下划线。\d{2}_
:3位数后跟下划线。\d{3}_
:一组2位数(代表测试台机器编号)后跟下划线。稍后可以通过(\d{2})_
。$matches[2]
:8位数(日期)后跟下划线。\d{8}_
:6位数后跟下划线。\d{6}_
:4个数字后跟一个点。\d{4}\.
:单个大写字母后跟2位数字。[A-Z]\d{2}
:单个点后跟小写字母\.asd
,a
和s
(扩展名)。d
:字符串的结尾。