Home > Back-end >  Removing certain parts of a files name using cmd
Removing certain parts of a files name using cmd

Time:08-08

I've been attempting to remove a certain string from a bunch of files but I am unable to do it. The part I want to rename is the .english in my files. How would I do this?

https://i.stack.imgur.com/rhRKG.png

CodePudding user response:

@ECHO OFF
SETLOCAL ENABLEDELAYEDEXPANSION
rem The following setting for the source directory is a name
rem that I use for testing and deliberately includes spaces to make sure
rem that the process works using such names. These will need to be changed to suit your situation.

SET "sourcedir=u:\your files"

FOR /f "delims=" %%b IN (
 'dir /b /a-d "%sourcedir%\*.english.*" '
 ) DO (
 SET "newname=%%b"
 ECHO REN "%sourcedir%\%%b" "!newname:.english.=.!"
)

GOTO :EOF

Always verify against a test directory before applying to real data.

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.

CodePudding user response:

Because your string splitting is at periods, ., I'd offer the following methodology.

In cmd.exe:

for %g in ("*.english.wav") do @for %h in ("%~ng") do @ren "%~g" "%~nh%~xg"

From a batch file:

@For %%G In ("*.english.wav") Do @For %%H In ("%%~nG") Do @Ren "%%~G" "%%~nH%%~xG"

As your question is unclear as to the intended final filename.

If you wanted to replace .english with something else, lets say .german, rather than simply removing it, then change:

"%~nH%~xG", or "%%~nH%%~xG"

To:

"%~nH.german%~xG", or "%%~nH.german%%~xG"

  • Related