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
  • 3

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
  • 4
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,725
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
  • 10,694
  • 1
  • 20
  • 35