PowerShell按模式重命名文件:在SxxExx前添加空格
Solution for Renaming Video Files with Space Before Season/Episode Tag
Absolutely, we can adjust your PowerShell script to insert that space before the SxxExx pattern. Here's how to modify it to get your desired filename format:
cd 'C:\Users\ME\Desktop\HERE' Get-ChildItem -Recurse | Where-Object { -Not $_.PsIsContainer -AND $_.Name -match '\.' } | ForEach-Object { # Remove all dots from the base name $cleanBase = $_.BaseName.Replace(".", "") # Insert space before the SxxExx pattern $formattedBase = $cleanBase -replace '(S\d+E\d+)', ' $1' # Combine with original extension $newName = $formattedBase + $_.Extension # Rename the file Rename-Item -Path $_.FullName -NewName $newName -PassThru }
Key Changes Explained:
- Fixed the File Filter: Your original
whereclause used-match "."which matches any filename (since.is a regex wildcard for any character). I changed it to-match '\.'to only target files with actual dots in their name. - Regex for Space Insertion: The
-replace '(S\d+E\d+)', ' $1'part looks for the patternSfollowed by one or more digits, thenEfollowed by one or more digits. It replaces that match with a space plus the original pattern, inserting the space exactly where you need it.
Optional Adjustments:
- Case Insensitivity: If your files might have lowercase
s/e(likes01e01), add the case-insensitive flag to the regex:$formattedBase = $cleanBase -replace '(?i)(s\d+e\d+)', ' $1' - Precise Matching: To avoid adding a space if
Scomes right after a digit (e.g.,FileName2S01E01), use a positive lookbehind to ensureSis preceded by a non-digit character:$formattedBase = $cleanBase -replace '(?<=[^\d])(S\d+E\d+)', ' $1'
Testing this with your example files will turn:
File.Name.S01.E01.Mp4→FileName S01E01.Mp4File.Name.S11.E01.Mp4→FileName S11E01.Mp4File.Name.S21.E01.Mp4→FileName S21E01.Mp4
内容的提问来源于stack exchange,提问作者Defca Trick
相关产品推荐
相关产品推荐

