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

  • 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 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 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 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 at 21:40
  • 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 at 0:10
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

|improve this answer|||||
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.

|improve this answer|||||
  • that's what I also meant, sorry for the misunderstanding :) edited the answer ^_^ – Yoyó Feb 16 at 14:41
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.

|improve this answer|||||
  • ren in cmd and Rename-Item in PowerShell can do a mass rename. You can do a loop for even more flexibility – phuclv Feb 15 at 0:08
  • @phuclv Excellent! Hope you will make than an answer, I will vote it up. – K7AAY Feb 15 at 0:13
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
|improve this answer|||||

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.