2
我正在編寫重命名報告並將它們放入文件夾的腳本。按文件名重命名報告
報告名稱必須更改爲正確的日期(日期必須在文件名中)。
例如我有一個名爲「Report_2015_02」的報告/我必須從文件名減去一個月 - 而不是系統時間。
在這種情況下,新的報告名稱必須是「Report_2015_01」
但在我使用SYSTEMTIME的瞬間,所以REPORTNAME將是「Report_2017_02」因爲SYSTEMTIME(2017_03)減去一個月的2017_02。 ..
問:如何使用文件名的日期而不是系統時間?
這裏是我的代碼:
#today's date (year-month-day)
$todaydate = Get-Date -Format yyyy-MM-dd
#arrays (today => systemtime)
$todaydate = $todaydate.Split('-')
$todaydate[0] #year
$todaydate[1] #month
$todaydate[2] #day
#arrays yesterday (systemtime - one day)
$yesterdaysdate = Get-Date((Get-Date).AddDays(-1)) -Format yyyy-MM-dd
$yesterdaysdate = $yesterdaysdate.Split('-')
$yesterdaysdate[0] #year
$yesterdaysdate[1] #month
$yesterdaysdate[2] #day
#arrays yesterday (systemtime - one day)
$lastmonth = Get-Date((Get-Date).AddMonths(-1)) -Format yyyy-MM-dd
$lastmonth = $lastmonth.Split('-')
$lastmonth[0] #year
$lastmonth[1] #month
$lastmonth[2] #day
#Example 1: Filename "Report_Telephone_yyyy-mm" => in this case "Report_Telephone_2016-12"
#it renames the file -> minus one month, so the name must be "Report_Telephone_2016-11"
$filename='Report_Telephone_'+ $lastmonth[0]+'-'+ $lastmonth[1] + '.xlsx'
write-host $filename
rename-item 'c:\Reporting\Report_Telephone_' + $todaydate[0] +'-' + $todaydate[1] + '.xlsx' -NewName $filename
$sourcepath='C:\Reporting\'+ $filename
write-host $sourcepath
$destinationpath='C:\Reporting\'+ $lastmonth[0]+'\'+ $lastmonth[1]
write-host $destinationpath
if(test-path $destinationpath)
{
}
else
{
mkdir $destinationpath
}
move-item -path $sourcepath -destination $destinationpath
#Example 2: Filename "Report_Outlook_yyyy-mm-dd" => in this case "Report_Outlook_2016-12-14"
#it renames the file -> minus one day, so the name must be "Report_Outlook_2016-12-13"
$filename='Report_Outlook_'+ $lastmonth[0]+'-'+ $lastmonth[1] + '.xlsx'
write-host $filename
rename-item 'c:\Reporting\Report_Outlook_' + $todaydate[0] +'-' + $todaydate[1] + '.xlsx' -NewName $filename
$sourcepath='C:\Reporting\'+ $filename
write-host $sourcepath
$destinationpath='C:\Reporting\'+ $yesterdaysdate[0]+'\'+ $yesterdaysdate[1]
write-host $destinationpath
if(test-path $destinationpath)
{
}
else
{
mkdir $destinationpath
}
move-item -path $sourcepath -destination $destinationpath
#that is just for generating log-files
[System.IO.File]::WriteAllText("$sourcepath\Protokoll_$todaydate.xls", $output)