1

How can I do renaming all of those files? Example: core-js-46asfv to core-js So those numbers and letters that are generated I want removed.

I cloned repository from Bitbucket and this is what happened with node modules. Angular app needs those folders to be named correctly (without generated letters at the end) in order to work.

Picture of files

private7
  • 121
  • I want to have only one core-js. So other can be removed or just stay with its name core-js-2. – private7 Feb 14 '20 at 21:24
  • No program is using them now until I rename them to right name (which is without those generated letters and numbers). – private7 Feb 14 '20 at 21:28
  • I cloned repository from Bitbucket and this is what happened with node modules. Angular app needs those folders to be named correctly (without generated letters at the end) in order to work. – private7 Feb 14 '20 at 21:35
  • 1
    That's a bit different. Maybe a better title for the question would be "BitBucket repository clone gone wrong". – Andrew Morton Feb 14 '20 at 21:40
  • Is the answer here of any help? The program suggested in the answer there can rename folders as well. https://superuser.com/questions/16007/how-can-i-mass-rename-files – Sam Forbis Feb 14 '20 at 21:56
  • 2
    @private7 you must put all the things in the question instead of in the comment. No one would read comments and may ask about duplicate file names after removing the trailing again – phuclv Feb 15 '20 at 00:10
  • You need to be more clear in your requirements. Many current answers remove the last - and following characters but this will convert convert-source-map to convert-source. Do you want this? Perhaps you want to remove the - and following characters only if there are exactly 8 of them and they are valid hex? (This wouldn't necessarily always be true). Are you sure it doesn't matter which core-js-something gets renamed core-js and which gets renamed core-js-2? I think sorting the clone as @AndrewMorton suggested in a comment would be a better solution than making complex rules. – lx07 Feb 15 '20 at 08:38

4 Answers4

2

These kinds of situations are one of the reasons why I use Total Commander. It has a Multi-Rename Tool: select the files to rename and press Ctrl-M. It uses a variant of RegExp, satisfying all your needs. When there are multiple folders that would be renamed into the same thing, it adds (2), (3) and so on to the end of the name (before extension, if it's a file).

For your specific need, you would need to select all the folders you need to rename, press CTRL-M, and enter the rename mask for the file name:

[N-63-10]

The 63 is just a number at least as large as your current longest foldername, it can be bigger, just make sure it's not smaller, since then it will chop off the end of the normal name too. The 10 is the number of characters you want to bite off (+1). It's that simple.

Yoyó
  • 21
1

You can run this python script and it will change all the current file names, so core-js-46asfv will become core-js after just removing the last part after the dash.

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")

Edited to also remove leftover files with the same name

Keep in mind that this script will delete only the last part of the file's name

So core-js-jsdbg becomes core-js BUT IF YOU RUN IT AGAIN IT WILL BECOME core

This applies to all files. Every file will get the last part of the last dash removed

Filip
  • 126
0

Windows 10 does not have a native command to rename files and/or folders en masse, so suggest you review the apps listed at https://alternativeto.net/software/bulk-rename-utility/ and select one which meets your needs.

K7AAY
  • 9,631
0

PowerShell: Doing my best with incomplet info regarding location, etc.

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+$'}

Demo:

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
Keith Miller
  • 9,620