使用Powershell脚本将文件夹中所有文件的名称更改为文件夹名称

Def*_*ick 2 powershell

我有一个类似于以下的文件夹系统:

Folder A
  Folder.A.S01.E01.MP4
  Folder.A.S01.E02.MP4
  Folder.A.S02.E01.TEST.THIS.MP4
  Folder.A.S02.E01.TEST.THIS.MP4
Run Code Online (Sandbox Code Playgroud)

有没有一种方法可以遍历文件夹并重命名文件,如下所示:

Folder A
  Folder A S01E01.MP4
  Folder A S01E02.MP4
  Folder A S02E01.MP4
  Folder A S02E01.MP4
Run Code Online (Sandbox Code Playgroud)

Which I understand is just removing the . in the name of all files in the folder.

I have tried the following, but it removes the . file extension as well and "breaks" the file.

cd 'C:\Users\ME\Desktop\HERE'
dir -recurse | where {-Not $_.PsIscontainer -AND $_.name -match "."} | 
foreach {
$New=$_.name.Replace("."," ")
Rename-Item -path $_.Fullname -newname $New -passthru
}
Run Code Online (Sandbox Code Playgroud)

Thanks in advance,

von*_*ryz 5

FileInfo具有BaseName和的属性Extension。前者仅包含不带路径的文件名,后者显然是扩展名。像这样结合两者,

$New=$_.BaseName.Replace("."," ")+$_.Extension
Rename-Item -path $_.Fullname -newname $New -passthru
Run Code Online (Sandbox Code Playgroud)

话虽如此,请考虑使用下划线而不是空格。使用命名为like的文件通常会更容易一些

Folder_A_S02_E01_TEST_THIS.MP4
Run Code Online (Sandbox Code Playgroud)

代替

Folder A S02 E01 TEST THIS.MP4
Run Code Online (Sandbox Code Playgroud)