I'm having issues running a loop which allows files within subfolders to be name from its parent directory. My current file structure is as follows:
C:\test data
Folder 1 [4k]
12345676.txt
35234443.mp4
23432234.avi
Folder 2 [4k]
56859522.txt
35234443.mp4
23432234.avi
The desired end-result.
C:\test data
Folder 1
Folder 1.txt
Folder 1.mp4
Folder 1.avi
Folder 2
Folder 2.txt
Folder 2.mp4
Folder 2.avi
Here is my code which I'm trying to modify
$path = "C:\test data"
Get-ChildItem $path -Directory | Foreach-Object {
$file = $_.Fullname
$file = $file -replace ('4K','')
$file = $file -replace ('\s+\[]','')
Write-Output $file
rename-Item -LiteralPath $_.FullName -NewName $file
Get-ChildItem $path
}
### Rename files in Subfolders
Get-ChildItem $path -Recurse -File | Foreach-Object {
Rename-Item -LiteralPath $_.FullName -NewName
($_.Directory.name) -ErrorAction SilentlyContinue
}
The script changes the name of the file but does not retain the extension.
C:\test data
Folder 1
Folder 1 (file)
Folder 1 (file)
Folder 1 (file)
Folder 2
Folder 2 (file)
Folder 2 (file)
Folder 2 (file)
Any information would be of great help since I'm still learning how to code in Powershell.