I have a massive set of folders all containing files with the same file extensions - ".mod." The folders are named randomly with strings of numbers, but I would like to name the folders each after the name of the ".mod" file in the folder, then remove the ".mod" file extension from the name of the folder itself. How could I implement this using a Powershell script?
#Rename a group of folders after files in those folders, using a regex?
8 messages · Page 1 of 1 (latest)
In other words, I'd like to rename folders by matching the name of the folders to files within them, which have the ".mod" extension.
for /d %%a in (*) do (
for %%b in ("%%a*.txt") do (
ECHO ren "%%a" "%%~nb"
)
)
I found this batch script which appears to do what I want if I replace .txt with .mod, but how do I avoid it recursively going over the subfolders inside those folders?
Update: batch script no workie
Without examples it's hard to know what you exactly want.
Try this to preview names, without actually editing anything
$all_mods = gci -Path '.' -Filter '*.mod' -File -Recurse
$new_names = $all_mods | %{
[pscustomobject]@{
Name = $_.Name
Parent = $_.Directory.FullName
NewName = Join-Path $_.Directory.Parent $_.Name
NewName2 = Join-Path $_.Directory.Parent.FullName $_.BaseName
}
}
$new_names|fl
$new_names| Out-GridView
Or show the examples of the full filepaths for a few before, and after cases
current file structure looks like this
12345
└── Alpha.mod
23456
└── Beta.mod
and desired structure
Alpha
└── Alpha.mod
Beta
└── Beta.mod
right?