gpt4 book ai didi

powershell - 使用Powershell脚本将新文件夹文件重命名为下一个增量编号

转载 作者:行者123 更新时间:2023-12-03 00:03:11 26 4
gpt4 key购买 nike

非常感谢您的帮助
我首先要提到的是,我一直找不到任何特定的解决方案,因此我对使用Powershell编程非常陌生,因此我的要求是

我希望在powershell中编写(并在以后安排)脚本,以查找具有特定名称的文件-RFUNNEL,然后将其重命名为R0000001。随时在文件夹中只有一个这样的“RFUNELL”文件。但是,当下一个脚本运行并找到新的RFUNNEL文件时,我会将其重命名为R0000002,依此类推,以此类推。

我已经为此苦苦挣扎了几个星期,而我遇到的看似相似的解决方案并没有太大帮助-可能是因为我承认自己对Powershell的经验有限。

最佳答案

其他人可能可以使用较少的语法来做到这一点,但是请尝试以下操作:

$rootpath = "C:\derp"

if (Test-Path "$rootpath\RFUNNEL.txt")
{ $maxfile = Get-ChildItem $rootpath | ?{$_.BaseName -like "R[0-9][0-9][0-9][0-9][0-9][0-9][0-9]"} | Sort BaseName -Descending | Select -First 1 -Expand BaseName;
if (!$maxfile) { $maxfile = "R0000000" }
[int32]$filenumberint = $maxfile.substring(1); $filenumberint++
[string]$filenumberstring = ($filenumberint).ToString("0000000");
[string]$newName = ("R" + $filenumberstring + ".txt");

Rename-Item "$rootpath\RFUNNEL.txt" $newName;
}

关于powershell - 使用Powershell脚本将新文件夹文件重命名为下一个增量编号,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16466151/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com