我剛剛開始了解 PowerShell。
我有一個腳本(從一個退出的老管理員那里得到它),它與檔案一起作業,根據需要重命名。
舊檔案名例如:
mcgruz 16.11.2021 03_30_1720211116_033432.xls
被重命名為
20211116_mcgruz
或其他:
prim43 15.11.2021 23_00_1920211115_320117.xls
被重命名為
20211115_prim43
腳本:
# Definition
$srcFld = "\\domain\MC_REPORT\INCOMING"
$arcFld = "\\domain\MC_REPORT\ARC"
$UnsortFld = "\\domain\MC_REPORT\UNSORT"
$destFld = $UnsortFld
$mcgruzFld = "\\domain\MC_REPORT\RAIL_RUN_DATE"
$mcdatcFld = "\\domain\MC_REPORT\DEMURRAGE_AT_THE_CUSTOMS"
$demurrFld = "\\domain\MC_REPORT\DEMURRAGE"
$prim43Fld = "\\domain\MC_REPORT\FLAT_CAR_PRICE_GROUP"
$podsylFld = "\\domain\MC_REPORT\PODSYL"
$pogruzFld = "\\domain\MC_REPORT\POGRUZKA"
$claimsFld = "\\domain\MC_REPORT\CLAIMS"
$dislzvFld = "\\domain\MC_REPORT\DISLZV"
$renummFld = "\\domain\MC_REPORT\RENUM"
$kontdislFld = "\\domain\MC_REPORT\KONTDISL"
$regex = "(\b\w \b).(\d [.]\d [.]\d )"
#
$folder = Get-ChildItem $srcFld -Filter *.xls
foreach($file in $folder) {
$fn=($file.Name -split $regex)
$fn[2] = date $fn[2] -format yyyyMMdd
$filenew = $fn[2] "_" $fn[1] ".xls"
switch ($fn[1])
{
renumm {
$destFld = $renummFld
}
mcgruz {
$destFld = $mcgruzFld
}
mcdatc {
$destFld = $mcdatcFld
}
prim43 {
$destFld = $prim43Fld
}
demurr {
$destFld = $demurrFld
}
podsyl {
$destFld = $podsylFld
}
pogruz {
$destFld = $pogruzFld
}
dislzv {
$destFld = $dislzvFld
}
claims {
$destFld = $claimsFld
}
kontdisl {
$destFld = $kontdislFld
}
default {
$destFld = $UnsortFld
}
}
Copy-Item $file.FullName "$destFld\$filenew" -Recurse
Copy-Item $file.FullName "$arcFld\$filenew" -Recurse
Remove-Item $file.FullName -recurse
}
但是最近,供應商更改了檔案,現在腳本無法正常作業。
新檔案名:prim4320211205_230050.xls腳本將其重命名為:_.xls而不是安全的舊檔案。
我必須更改代碼,但我不知道要更改什么。
$regex = "(\b\w \b).(\d [.]\d [.]\d )"
#
$folder = Get-ChildItem $srcFld -Filter *.xls
foreach($file in $folder) {
$fn = ($file.Name -split $regex)
$fn[2] = date $fn[2] -format yyyyMMdd
$filenew = $fn[2] "_" $fn[1] ".xls"
我會很感激你的幫助
嘗試在本地機器上使用代碼。和 powershell ISE 發送錯誤:
$fn[2] = date $fn[2] -format yyyyMMdd
~~~~~~
CategoryInfo : WriteError: (:) [Get-Date],
ParameterBindingException
FullyQualifiedErrorId :
ParameterBindingFailed,Microsoft.PowerShell.Commands.GetDateCommand
uj5u.com熱心網友回復:
要處理這兩種檔案命名格式,您可以使用以下內容:
foreach ($file in (Get-ChildItem -Path $srcFld -Filter '*.xls' -File)) {
if ($file.BaseName -match '^(. )(\d{8})_.*') {
# testing new format like 'prim4320211205_230050'
$name = $matches[1]
$filenew = '{0}_{1}{2}' -f $matches[2], $matches[1], $file.Extension
}
elseif ($file.BaseName -match '^(\w ) (\d{1,2}\.\d{1,2}\.\d{4}) .*') {
# testing old format like 'mcgruz 16.11.2021 03_30_1720211116_033432'
$name = $matches[1]
$date = [datetime]::ParseExact($matches[2], 'dd.MM.yyyy', $null)
$filenew = '{0:yyyyMMdd}_{1}{2}' -f $date, $matches[1], $file.Extension
}
else {
# output a warning thatthe filename could not be parsed out
Write-Warning "could not rename file '$($file.FullName)'"
continue # skip this file and proceed with the next one
}
# now proceed with the rest of your code
$destFld = switch ($name) {
'renumm' { $renummFld }
'mcgruz' { $mcgruzFld }
# etc.
}
$file | Copy-Item -Destination (Join-Path -Path $destFld -ChildPath $filenew)
$file | Copy-Item -Destination (Join-Path -Path $arcFld -ChildPath $filenew)
$file | Remove-Item -Force
}
uj5u.com熱心網友回復:
regex101.com 上解釋的正則運算式,帶有測驗字串:regex101: build, test, and debug regex
(\b\w \b).(\d [.]\d [.]\d )
1st Capturing Group (\b\w \b)
\b assert position at a word boundary: (^\w|\w$|\W\w|\w\W)
\w matches any word character (equivalent to [a-zA-Z0-9_])
matches the previous token between one and unlimited times, as many times as possible, giving back as needed (greedy)
\b assert position at a word boundary: (^\w|\w$|\W\w|\w\W)
. matches any character (except for line terminators)
2nd Capturing Group (\d [.]\d [.]\d )
\d matches a digit (equivalent to [0-9])
matches the previous token between one and unlimited times, as many times as possible, giving back as needed (greedy)
Match a single character present in the list below [.]
. matches the character . with index 4610 (2E16 or 568) literally (case sensitive)
\d matches a digit (equivalent to [0-9])
matches the previous token between one and unlimited times, as many times as possible, giving back as needed (greedy)
Match a single character present in the list below [.]
. matches the character . with index 4610 (2E16 or 568) literally (case sensitive)
\d matches a digit (equivalent to [0-9])
matches the previous token between one and unlimited times, as many times as possible, giving back as needed (greedy)
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/374214.html
標籤:电源外壳
