Delete last 2 characters from file names in a folder using CMD -


hi i'm trying replace _c @ end of each file name. used @echo off ren . ?????????.*

this works fine file name width differ else can select last 2 characters , rename it?

you can use enhanced expanding of environment variable, e.g.:

%path:~0,-2% 

would extract last 2 characters of path variable. more details, check of set command.

and can use command apply on every file. don't forget use delayed expansion (using exclamation instead of percent) inside cycle.

for /f %i in ('dir *.* /b') ( set a=%~ni ren %i !a:~0,-2!%~xi ) 

check of command more details.

update: revised cycle , created usable batch code (including explanation comments), works long filenames containing spaces. put .bat file run batch inside directory, need rename files. note: percent needs doubled when used inside batch file.

@echo off rem use extensions , delayed expansion setlocal enableextensions enabledelayedexpansion /f "tokens=*" %%i in ('dir *.* /b') ( rem skip batch if "%0" neq "%%i" ( rem show renamed file echo %%i rem set filename without extension set a=%%~ni rem rename file minus last 2 characters appended extension (which includes period) ren "%%i" "!a:~0,-2!%%~xi" )) 

Comments

Popular posts from this blog

java - Spring Data JPA: Why findOne(id) executing delete query internally? -

python - Mongodb How to add addtional information when aggregating? -

java - Incorrect order of records in M-M relationship in hibernate -