-3

I'm trying to get info on trying to remove a specific thing from multiple folder names.. Preferably in such a way that it can be in a batch file.

For example.

Before:

Test1 [REMOVEME]
Test2 [REMOVEME]
Test3 [REMOVEME]
Test4 [REMOVEME]

After:

Test1
Test2
Test3
Test4

I've seen plenty of info about how to do this with files. But not folders. Is this doable with a batch file so it could be easily used later? Or will i need to use third party software?

Also. Am using Windows 10.

Thanks, Ben.

  • Check out this thread https://stackoverflow.com/questions/17271586/rename-multiple-files-in-cmd – Sushant Rajbanshi Feb 08 '18 at 05:56
  • 1
    Please take the [tour], read [Ask] and [MCVE]. Questions of the form "is it doable" or "is it possible" are not good SO questions. It usually is possible, the real question is, have you bothered to do the research and made any attempt at all to solve this problem on your own? Did you even search for similar problems here on SO? – jwdonahue Feb 08 '18 at 06:30
  • I bet you do not actually have [] in the names. – Gerhard Feb 08 '18 at 06:44
  • 1
    @GerhardBarnard - I don't see how that's relevant, since all he needs to do is use substring replacement. – SomethingDark Feb 08 '18 at 07:52
  • I have searched around quite a bit. I am yet to find a way to rename multiple FOLDERS.. Rather than files. Also the square brackets are in the name. – Ben Braskus Cole Feb 08 '18 at 07:59
  • @SomethingDark I prefer if peopke use actual examples. He might have numerous replace strings and then we do the work to give him code based on the example, and it does not work. – Gerhard Feb 08 '18 at 08:00
  • I haven't has issues with modifying the 'code' before. Such as this example: get-childitem *.mp3 | foreach { rename-item $_ $_.Name.Replace("Radiohead -", "") } Which i've used before to rename files. However the get-childitem doesn't seem to be able to target folders? – Ben Braskus Cole Feb 08 '18 at 08:06
  • The string i am trying to remove is always the same. [YTS.AG]. However the name before it varies. – Ben Braskus Cole Feb 08 '18 at 08:12
  • 1
    Weird that you've tagged this question [batch-file] when the code you posted in the comments is Powershell. (Also, I don't know Powershell, but I'm guessing that `*.mp3` is keeping you from picking up directory names.) – SomethingDark Feb 08 '18 at 09:03
  • I wasn't trying to use that code for my purposes. Was more an example of what i was trying to do with folders. I tried modifying it my self with info i found googling around but didn't have any luck. I managed to get it to remove what i wanted if i give it the name of the folder. But not removing what i want regardless of what the rest of the folder name was. – Ben Braskus Cole Feb 08 '18 at 10:48
  • **1.** You didn't said what is the "specific thing" you want to remove: include it _the spaces_ before the left square bracket? **2.** I don't understand why you posted a code you "used before to rename files" as "an example of what you trying to do with _folders_"! (Hint: files and folders are two different things). **3.** This code do what you want: `for /F "tokens=1* delims=[" %%a in ('dir /B /A:D') do ECHO ren "%%a[%%b" "%%a"` – Aacini Feb 08 '18 at 14:34

2 Answers2

0

use a for /d to get matching foldernames and a second for /f to split it by space (take the part before the first space):

@echo off
for /d %%a in ("* [remove]") do (
  for /f %%b in ("%%a") do (
    ECHO ren "%%a" "%%b"
  )
)

or if you prefer a one-liner (less flexible):

for /f "tokens=1,*" %%a in ('dir /b /ad "* [remove]"') do ECHO ren "%%a %%b" "%%a"

(for both examples: remove the ECHO, when the output is what you want)

Stephan
  • 53,940
  • 10
  • 58
  • 91
  • That one-liner works perfectly in a Batch-File like this: for /f "tokens=1,*" %%a in ('dir /b /ad "* [YTS.AG]"') do ren "%%a %%b" "%%a" Thank you very much! – Ben Braskus Cole Feb 08 '18 at 22:10
0

The directories can be renamed with PowerShell. If the directory name matches the pattern, Rename-Item will do it. When you are confident that the directories will be renamed correctly, remove the -WhatIf from the Rename-Item cmdlet.

$matchpattern = ' \[REMOVEME\]'

Get-ChildItem -Directory |
    ForEach-Object {
        if ($_.Name -match $matchpattern) {
            Rename-Item -LiteralPath $_.FullName -NewName $($_.Name -replace $matchpattern,'') -WhatIf
        }
    }

Alternatively, the work could be lessened by using a Filter on Get-ChildItem. However, that would require two (2) different match patterns to be maintained.

Get-ChildItem -Directory -Filter '* [REMOVEME]*' |
    ForEach-Object {
        Rename-Item -LiteralPath $_.FullName -NewName $($_.Name -replace ' \[REMOVEME\]','') -WhatIf
    }

EDIT To run this in a batch file, put the code above into a file such as renamepattern.ps1. Then, run it in a batch file.

powershell -NoProfile -File .\renamepattern.ps1
lit
  • 14,456
  • 10
  • 65
  • 119
  • Thank you for your help. Putting this: $matchpattern = ' \[YTS.AG\]' Get-ChildItem -directory | ForEach-Object { if ($_.Name -match $matchpattern) {Rename-Item -LiteralPath $_.FullName -NewName $($_.Name -replace $matchpattern,'') } } into Powershell does work. But does not in batch-file. I tried replacing 'directory' with the actual directory but then it says it can't find it. Am i missing something? Or can i not simplify this enough to be in a batch-file? – Ben Braskus Cole Feb 08 '18 at 22:03
  • @BenBraskusCole - I have edited the answer to show how to run it in a batch file. This code is PowerShell. It is not a cmd.exe .bat script. There are many cases where the selected answer will not produce correct results. Try a directory named `First Test [REMOVEME]`. – lit Feb 08 '18 at 23:52