Disconected
Disconected

Reputation: 11

Batch Rename by Replace substring

Hello :) when a friend give you a mp3 file, most often, the title is named that way: "avenged_sevenfold_-_buried_alive.mp3". i want to make a script which permit me to rename every file and folder of current repertoryby replacing "_" by " "(space). this is very long to rename each file manually; my script is here:

@echo off
title Merci qui ?
:debut
echo do you want to rename every files/foldes by replacing "_" by " "? (y/n)
set /p choix=


if %choix%==y (
goto renomage)

if %choix%==n (
goto end)

goto debut

:renomage

set /a x=1 :pointeur de fichier

set /a compteurA=0
set /a compteurB=0
for /f %%a in ('dir /b /a:-d *.*') do set /a compteurA+=1   
for /f %%a in ('dir /b /a:d *.*') do set /a compteurB+=1
echo %compteurA% fichiers %compteurB% dossiers
set /a n=compteurA+compteurB

echo number of folder/files batch included %n%


:while1 
if %x% GTR %n% goto prochain1 

set Strin=???name?ofthe?dossier?number?x?????????????
for /f "tokens=1,* delims=[,]" %%A in ('"%comspec% /u /c echo:%Strin%|more|find /n /v """') do          `set /a l=%%A-4`
echo Le nom contient %l% lettres.

:leftspace

set Strin=%Strin:_= %

set str=%Strin:~0,1%

if %str% EQU " "( 
set Strin=%Strin:~1,%l%-1%
goto leftspace)


SET /A x +=1
goto while1
:prochain1
echo Success ! (or not) 
pause
:end

i just need help for this line:

set Strin=???name?ofthe?dossier?number?x?????????????

otherwise the rest should be ok. if you have some critics or something, go on =)

Upvotes: 1

Views: 1756

Answers (2)

Michael Innes
Michael Innes

Reputation: 2082

This solution is reposted from How to Batch Rename Files in Windows: 4 Ways to Rename Multiple Files by Chris Hoffman

PowerShell offers much more flexibility for renaming files in a command-line environment. Using PowerShell, you can pipe the output of one command – known as a “commandlet” in PowerShell terms — to another command, just like you can on Linux and other UNIX-like systems.

Open PowerShell ISE

First of all, open Powershell ISE and then navigate to the directory (folder) that has the files and folders you'd like to rename by using this command:

cd "C:\your\directory\"

Navigate to your folder

The two important commands you’ll need are Dir, which lists the files in the current directory, and Rename-Item, which renames an item (a file, in this case). Pipe the output of Dir to Rename-Item and you’re in business.

After you launch PowerShell ISE, use the cd command to enter the directory containing your files. You should put the files in their own directory so you don’t accidentally rename other files.

For example, let’s say we don’t want the underscore character in our file names – we’d rather have a space instead.

The following command lists the files in the current directory and pipes the list to Rename-Item. Rename-Item replaces each underscore character with a space.

Dir | Rename-Item –NewName { $_.name –replace "_"," " }

Replace the "_" and "" parts of the command to replace other characters in file names.

Consult Microsoft’s documentation on the Rename-Item commandlet if you want help performing other, more advanced operations.

Upvotes: 2

Magoo
Magoo

Reputation: 80213

@ECHO OFF
SETLOCAL
SET "sourcedir=U:\sourcedir"
FOR %%f IN (ad a-d ) DO FOR /f "delims=" %%a IN (
  'dir /b /s /%%f "%sourcedir%\*" '
  ) DO CALL :chgnam %%a

GOTO :EOF

:chgnam
SET "name=%*"
SET "newname=%name:_= %"
IF "%newname%" neq "%name%" ECHO REN "%name%" "%newname%"

GOTO :eof

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 and directories. Directories are renamed first.

You will need to change sourcedir to point to your required directory. All files and directories in the tree which contain the _ would be renamed - if it's possible to rename them.

[revised and corrected version]

@ECHO OFF
SETLOCAL
SET "sourcedir=U:\sourcedir"
PUSHD "%sourcedir%"
FOR %%f IN (ad a-d ) DO FOR /f "delims=" %%a IN (
  'dir /b /s /%%f "%sourcedir%\*" '
  ) DO CALL :chgnam "%%a" "%%~nxa"
POPD

GOTO :EOF

:chgnam
SET "name=%~2"
SET "newname=%name:_-_=-%"
SET "newname=%newname:_= %"
IF "%newname%" neq "%name%" ECHO(REN "%~1" "%newname%"

GOTO :eof

The original version would show incorrect ren syntax (the new-name may not contain a path)

If you want to run on the current directory, simply set the value of sourcedir to a single dot.

Upvotes: 0

Related Questions