有没有一种方法可以批量删除900个文件的文件名的一部分?

时间:2019-09-27 22:48:09

标签: windows rename naming

需要一种删除文件名部分的方法。

尝试过一些Basic notepad ++东西

https://i.imgur.com/SM8QbWq.jpg

图像主要显示了我所需要的!

例如

sevenberry_island_paradise-SB-4131D1-4-S5090015(.jpg) 至 sevenberry_island_paradise-SB-4131D1-4(.jpg)

项目代码在SB-之后,例如4131D1-4,此后我不想要的所有内容。

从所有这些文件中删除此文件的任何方式都将带来巨大的巨大帮助!

谢谢!

1 个答案:

答案 0 :(得分:0)

该问题不适合发布,您需要发布尝试过的内容,并寻求有关您自己的代码以及遇到的任何错误消息或意外结果的帮助。话虽这么说,我看到了您的问题,找到解决方案似乎很有趣,所以我做到了。

此代码将找到指定目录内的所有文件(您也可以在-Recurse行中添加Get-ChildItem参数,以获取所有子目录中的文件)并重命名所有文件,并删除结尾使用RegEx的文件名。

复制文件,然后再尝试执行此操作。我已尽力创建一个适用于您所描绘的文件名的解决方案,但是如果文件名与所描绘的文件名有很大不同,则可能会产生意想不到的结果。 先备份。

# Specify the path in which all of your jpgs are stored
$path = 'C:\Path\To\Jpgs'
# Get all of the files we want to change, and only return files that have the .jpg extension
$jpgs = Get-ChildItem -Path "$path" <#-Recurse#> | Where-Object {$_.Extension -eq '.jpg'}
# Perform the same steps below on every file that we got above by using foreach
foreach ($jpg in $jpgs) {
    # Store the original file name in a variable for working on
    [string]$originalBaseName = "$($jpg.BaseName)"
    # Use RegEx to split the file name
    [string]$substringToReplace = ($originalBaseName -split '-[0-9]+-')[1]
    # Re-add the '-' to the string which you want to remove from the file name
    [string]$substringToReplace = '-' + $substringToReplace
    # Remove the portion of the file name you want gone
    [string]$newBaseName = $originalBaseName -replace "$substringToReplace",''
    # Rename the file with the new file name
    Rename-Item -Path "$($jpg.FullName)" -NewName "$newBaseName$($jpg.Extension)"
}