我必須遵循這樣的模式文件夾:重命名文件,包括家長和Grantparent文件夾 - PowerShell的
C:\root folder\grandparent folder\parent folder\00001.pdf
C:\root folder\grandparent folder\parent folder\00002.pdf
我想的PDF文件重命名爲類似的根文件夾,文件夾祖父母,父母folder.1 .pdf和根文件夾 - 祖父母文件夾 - 父文件夾.2.pdf等,如果可能的話,將此文件移動到根文件夾級別。
我發現這個PowerShell腳本做了類似的事情,但它只需要父文件夾的名稱。
這是我有:
#######Rename script#############
$path = Split-Path -parent $MyInvocation.MyCommand.Definition
Function renameFiles
{
# Loop through all directories
$dirs = dir $path -Recurse | Where { $_.psIsContainer -eq $true }
Foreach ($dir In $dirs)
{
# Set default value for addition to file name
$i = 1
$newdir = $dir.name + "_"
# Search for the files set in the filter (*.pdf in this case)
$files = Get-ChildItem -Path $dir.fullname -Filter *.pdf -Recurse
Foreach ($file In $files)
{
# Check if a file exists
If ($file)
{
# Split the name and rename it to the parent folder
$split = $file.name.split(".pdf")
$replace = $split[0] -Replace $split[0],($newdir + $i + ".pdf")
# Trim spaces and rename the file
$image_string = $file.fullname.ToString().Trim()
"$split[0] renamed to $replace"
Rename-Item "$image_string" "$replace"
$i++
}
}
}
}
# RUN SCRIPT
renameFiles