3

After successfully removing a bunch of Google Drive Folder duplicates, some files retain a "filename(2)"name.

Is there a way to batch rename every file so the parenthesis and the number inside the parenthesis is gone?

That includes folders and sub-folders.

Gabriel Meono
  • 990
  • 3
  • 18
  • 48
  • I've done this before, when I was testing 'owncloud' but, it's a pain since you can't use `for` loops with parenthesis. As PA asked above, what have you tried so far? – Alex May 17 '14 at 21:40

3 Answers3

3

Try like this :

Create a file test.bat with the code below in it and replace the path to test in the var $path

@echo off 
set $path="C:\Users\CN Micros\Desktop\PROGRAMMATION\test"
for /f "tokens=1-3 delims=^(^)" %%a in ('dir /b/a-d %$path%') do (
if exist %$path%\"%%a(%%b)%%c" echo ren %$path%\"%%a(%%b)%%c" "%%a%%c"
)
pause

Then run it in the CMD or by double clicking. If the output is ok for you remove the echo

The program create 3 tokens : %%a = what's before the (), %%b What's inside the () and %%c what's after the ().

Then we arrange this 3 tokens to rename the files without the ().

If you have some file who have the same final name ie : "file(1)name", "file(2)name" --> "filename" It will work only with the first one. If you have this case you have to add a counter at the end of file to be sure that they will be renamed.

SachaDee
  • 9,245
  • 3
  • 23
  • 33
  • Forgive my ignorance. How do you run this code? I tried to run it through CMD and as a bat without results. I have low experience coding. – Gabriel Meono May 17 '14 at 23:19
  • Please supply an explanation of the code in your answer, this will help future readers. – Lawrence Cherone May 17 '14 at 23:23
  • old bump, sorry, but how would one go about the case of removing a preceding space in the case of "My File (1).jpg" to prevent a space preceding the extension? – Robert Jan 06 '19 at 01:01
  • with a string substitution : `set "file=My File(1).jpg"` and then `set "file=%file: =%"` – SachaDee Jan 07 '19 at 11:39
2
@ECHO OFF
SETLOCAL
SET "sourcedir=U:\sourcedir"
FOR /f "delims=" %%a IN (
  'dir /b /s /a-d "%sourcedir%\*" '
 ) DO (
 SET "name=%%~na" 
 SETLOCAL ENABLEDELAYEDEXPANSION
 SET "newname=!name:)=!"
 SET "newname=!newname:(=!"
 IF "!name!" neq "!newname!" (
  IF EXIST "%%~dpa!newname!%%~xa" (ECHO cannot RENAME %%a
   ) ELSE (ECHO(REN "%%a" "!newname!%%~xa")
 )
 endlocal
)

GOTO :EOF

You'd need to set your required directory into sourcedir. I used u:\sourcedir which suits my testing.

The required REN commands are merely ECHOed for testing purposes. After you've verified that the commands are correct, change ECHO(REN to REN to actually rename the files.

Magoo
  • 77,302
  • 8
  • 62
  • 84
2

This will create renfiles.bat.txt for you to examine in Notepad and then rename to .bat and execute if you are happy with it.

@echo off
dir /b /a-d *(*).* |find /i /v "%~nx0" |find /i /v "repl.bat" |repl "(.*)\(.*\)(\..*)" "ren \q$&\q \q$1$2\q" xa >"renfiles.bat.txt"

This uses a helper batch file called repl.bat - download from: https://www.dropbox.com/s/qidqwztmetbvklt/repl.bat

Place repl.bat in the same folder as the batch file or in a folder that is on the path.

Edit: This version will recurse through subdirectories:

@echo off
dir /b /s /a-d *(*).* |find /i /v "%~nx0" |find /i /v "repl.bat" |repl ".*\\(.*)\(.*\)(\..*)" "ren \q$&\q \q$1$2\q" xa >"renfiles.bat.txt"
foxidrive
  • 40,353
  • 10
  • 53
  • 68
  • Is there a way to implement it to sub-folders as well? Thank you! – Gabriel Meono May 18 '14 at 09:13
  • Superb! Thanks a bunch. It's quite elegant how it displays the results ☺ – Gabriel Meono May 18 '14 at 09:38
  • In case your dropbox link disappears, here's the official thread of the repl.bat script for anyone who stumbles across in the future: http://www.dostips.com/forum/viewtopic.php?f=3&t=3855::: – Gabriel Meono May 18 '14 at 17:34
  • @GabrielMeono There is an official thread at SO too http://stackoverflow.com/questions/60034/how-can-you-find-and-replace-text-in-a-file-using-the-windows-command-line-envir – foxidrive May 18 '14 at 17:43