TechLifeWeb

Exploring the digital life

How to remove a string from file names with a batch file


Published by 
 on 

This example code will remove a string called "mysting" from all the files in C:\Users\thisuser\Downloads

@echo off
SetLocal ENABLEDELAYEDEXPANSION
set codepath=%~dp0
cd "%codepath%"

::Set the folder that you want to loop through
set foldertocheck=C:\Users\thisuser\Downloads
::Set the string you want ot remove from the file names
set stringtoremove=mystring

FOR %%G IN ("%foldertocheck%\*.*") DO (
    set filename="%%~nG%%~xG"
    set filename=!filename:%stringtoremove%=!
    REM Rename only if there is a difference between new filename and old filename in %%G
    IF NOT !filename! == "%%~nG%%~xG" ren "%%~G" !filename!
)

Windows Batch File Examples