答案1
您可以运行这个 python 脚本,它将改变所有当前文件名,因此只需删除破折号后的最后一部分core-js-46asfv
即可。core-js
import os
def rename_all_files(path):
names = os.listdir(path)
print(names)
for dir in names:
if "-" in dir:
new_name = dir.split("-")
print(new_name)
name = ""
for item in range(len(new_name)-1):
name += new_name[item]
if item < len(new_name)-2:
name += "-"
new_name = name
print(new_name)
if os.path.exists(new_name):
continue
else:
os.rename(dir, new_name)
# This affects how many folders are removed that have the same name
# Either change it to a small value and re-run the program or set this to a large integer to delete it in one go
# This will affect the speed of the program, but it wont have a large impact
# Also re-running, won't delete already fixed folders
# Instead of 100, add a large number to make sure you don't have any left over files that didn't get deleted
# I suggest you leave 100 as the integer, because there is very low chance that there will be 101 files with the same name
for i in range(100):
new_list = os.listdir(path)
print(new_list)
for item in range(1, len(new_list)):
if new_list[item].startswith(new_list[item-1]):
print("item removed:", new_list[item])
os.rmdir(new_list[item])
rename_all_files("YOUR PATH HERE")
编辑后还删除了同名的剩余文件
请记住,此脚本只会删除文件名的最后部分
因此core-js-jsdbg
变成core-js
但如果你再次运行它,它将会变成core
这适用于所有文件。每个文件的最后一条破折号的最后一部分都会被删除
答案2
这种情况是我使用 Total Commander 的原因之一。它有一个多重命名工具:选择要重命名的文件并按Ctrl- M。它使用 RegExp 的变体,满足您的所有需求。当有多个文件夹需要重命名为同一个名称时,它会在名称末尾添加 (2)、(3) 等(如果是文件,则在扩展名之前)。
对于您的特定需求,您需要选择所有需要重命名的文件夹,按CTRL- M,然后输入文件名的重命名掩码:
[N-63-10]
63 只是一个数字,至少要和您当前最长的文件夹名称一样大,它可以更大,但请确保它不小于,否则它也会切断正常名称的末尾。10 是您要截掉的字符数 (+1)。就这么简单。
答案3
Windows 10 没有本机命令来批量重命名文件和/或文件夹,因此建议您查看以下列出的应用程序https://alternativeto.net/software/bulk-rename-utility/并选择一个满足您需求的。
答案4
PowerShell:尽我所能解决位置等不完整的信息。
Get-ChildItem -Directory | ? Name -match '\-' |
select fullname, @{N = 'New'; E = {$_.Name -replace '\-\w+$'}} |
group New | ForEach {
$_.Group | select -expand FullName -first 1} |
Rename-Item -NewName {$_ -Replace '\-\w+$'}
演示:
PS C:\...\Dupliate folder Names>gci
Directory: C:\Users\Keith\Sandbox\Dupliate folder Names
Mode LastWriteTime Length Name
---- ------------- ------ ----
d----- 2/15/2020 1:24 AM Newfolder
d----- 2/15/2020 1:23 AM Test1-123-456
d----- 2/15/2020 1:23 AM Test1-123-789
d----- 2/15/2020 1:51 AM Test2-654
d----- 2/15/2020 1:51 AM Test2-987
PS C:\...\Dupliate folder Names>Get-ChildItem -Directory | ? Name -match '\-' |
>> select fullname, @{N = 'New'; E = {$_.Name -replace '\-\w+$'}} |
>> group New | ForEach {
>> $_.Group | select -expand FullName -first 1} |
>> Rename-Item -NewName {$_ -Replace '\-\w+$'}
>> PS C:\...\Dupliate folder Names>gci
Directory: C:\Users\Keith\Sandbox\Dupliate folder Names
Mode LastWriteTime Length Name
---- ------------- ------ ----
d----- 2/15/2020 1:24 AM Newfolder
d----- 2/15/2020 1:23 AM Test1-123
d----- 2/15/2020 1:23 AM Test1-123-789
d----- 2/15/2020 1:51 AM Test2
d----- 2/15/2020 1:51 AM Test2-987