powershell - 按文件名重命名报告

标签 powershell

我正在编写一个脚本,用于重命名报告并将其放在文件夹中。

报告名称必须更改为正确的日期(日期必须在文件名中)。

例如,我有一个名为“Report_2015_02”的报告/我必须从文件名中减去一个月而不是系统时间

在这种情况下,新的报告名称必须为“Report_2015_01”

但目前我正在使用systemtime,因此报表名称将为“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)

最佳答案

您可以使用简单的正则表达式来获取月份,将其转换为整数,减去1,最后将其格式化为带有两位小数的字符串:

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

关于powershell - 按文件名重命名报告,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42620238/

相关文章:

powershell - 如何合并和压缩多个脚本和CSS文件供生产使用?

loops - 如何使用 PowerShell 为一组具有增量值的 IIS 网站设置绑定(bind)端口号?

visual-studio - 在 Visual Studio 中使用 PowerShell 保存文件

powershell - 如何自动更新在powershell中运行的后台作业的数量?

powershell - 从 powershell 运行 aws emr create-cluster 时出错

powershell - 在 Powershell 中使用变量 Get-ADUser -Filter

Powershell Get-Service 命令输出服务代码而不是文本状态?

sql-server-2008 - 为什么 sqlpsx 不包含在 SQL Server 本身中?

powershell - 如何在 VSTS 中的托管代理上运行或安装工具

search - powershell搜索用户创建的文档?