2017-03-06 8 views
2

レポートの名前を変更してフォルダに入れるスクリプトを作成しています。ファイル名でレポートの名前を変更します

レポート名を正しい日付に変更する必要があります(日付はファイル名にする必要があります)。

たとえば、 "Report_2015_02"という名前のレポートがあります。/ システム時間ではなく、というファイル名から1か月を引く必要があります。

この場合、新しいレポート名が「Report_2015_01」であることを

を必要とします。しかし、現時点では、私はSYSTEMTIMEを使用していますので、SYSTEMTIME(2017_03)マイナス1ヶ月2017_02ですのでREPORTNAMEは「Report_2017_02」になります。 ..

Q:システム時間の代わりにファイル名の日付を使用するにはどうすればよいですか?と文字列として

#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) 

答えて

1

あなたは月につかむために簡単な正規表現を使用することができ、は整数ににそれを投げ、substract 1、最後に形式それは:

ここに私のコードです2つの小数:

$fileName = 'Report_2015_02' # Probably want to use Get-ChildItem here... 
$fileMonth = [int][regex]::Match($fileName, '\w+_\d+_(\d+)').Groups[1].Value 
$fileMonth = "{0:D2}" -f ($fileMonth - 1) 
関連する問題