我对 powershell 脚本有疑问。我有一个每日任务计划程序,它导出名为 12122_000000.txt 的文件并将其存储在 C:\ 驱动器上。我还有另一个 Report 文件夹,位于 C:\Report。在该报告文件夹中,我有很多名称为 12122_000001.txt、12122_000002.txt、12122_0000003.txt、12122_0000004.txt 等的文件。
我想要一个脚本来检查报告文件夹以查找最新的文件名。然后将C:\上的文件名12122_000000.txt移动到C:\Report并重命名,最大名称增加1。文件名格式必须为12122_xxxxxx.txt
我尝试编写脚本,但没有成功。请帮助我获得有关 powershell 的知识。
$SourceFile = "C:\12122_000000.txt"
$DestinationFolder = "C:\Report"
if (Test-Path $SourceFile)
{
$latest = Get-ChildItem -Path $DestinationFolder| Sort-Object Name -Descending | Select-Object -First 1
$i = 1
Move-Item -path $SourceFile -destination $latest.basename + $i++ + ".txt")
}
Thanks
这是增加此文件名并移动文件的简单方法:
$SourceFile = "C:\12122_000000.txt"
$DestinationFolder = "C:\Report"
if (Test-Path $SourceFile)
{
$latest = Get-ChildItem -Path $DestinationFolder| Sort-Object Name -Descending | Select-Object -First 1
#split the latest filename, increment the number, then re-assemble new filename:
$newFileName = $latest.BaseName.Split('_')[0] + "_" + ([int]$latest.BaseName.Split('_')[1] + 1).ToString().PadLeft(6,"0") + $latest.Extension
Move-Item -path $SourceFile -destination $DestinationFolder"\"$newFileName
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)