我试图计算目录中的文件数,然后按父文件夹重命名文件。第一个文件是.extension,后面的每个文件都是.extension。这是我到目前为止:
$RootDir = Read-Host -Prompt 'What is the root directory?'
$grabFiles = Get-ChildItem $RootDir -Filter *.txt-recurse
#count the number of pst files in directory recursively
$fileNum = (Get-ChildItem $grabFiles | Measure-Object).Count
#Gather parent directory
$parent = Resolve-Path . | Split-Path -Leaf
for($x=0; x -lt $fileNum; $x++)
#loop to rename file recursively
#starting by <ParentDirectory> for the first file then <ParentDirectoy>1...2...3... for every other file
{
if($x -lt 1){
$_ | Rename-Item -NewName $parent".pst" -Verbose
}
else
{
$_ | Rename-Item -NewName $parent + $x".pst" -Verbose
}
}
这是你想要做的吗?它也应该递归地工作
$Files = Get-ChildItem -Path C:\Users\User1\Temp\testdir
$Count = 1
foreach ($txt in $Files)
{
Rename-Item -Path $txt.FullName -NewName ($txt.Directory.Name + $count + ".pst") -Verbose
$Count++
另类。如果我们从0开始计数,我们可以在循环后从名称中删除0
$Path = "C:\Users\User1\Temp\testdir"
$Files = Get-ChildItem -Path $Path
$Count = 0
foreach ($txt in $Files)
{
Rename-Item -Path $txt.FullName -NewName ($txt.Directory.Name + $count + ".pst") -Verbose
$Count++
}
Rename-Item -Path ($path + $txt.Directory.Name + 0 + ".pst" ) -NewName ($path + $txt.Directory.Name + ".pst" )
}