3

Im quite new to batch programming and i wanted to remove the last characters on my filename.

10_myfile_12345_6789.txt
11_myfile_12345_0987.txt

I want to remove the last 4 digits on my filename how i could do that?

I have tried this

@echo off
setlocal enabledelayedexpansion
set X=3
set FOLDER_PATH=
pushd %FOLDER_PATH%
for %%f in (*) do if %%f neq %~nx0 (
    set "filename=%%~nf"
    ren "%%f" "!filename!%%~xf"
)
popd
PAUSE

but it removes on first and last characters, i only saw this here too, im still quite confused how this works

Xander Vane
  • 197
  • 2
  • 4
  • 20
  • what language. what have you tried? – Jeremy Jan 13 '15 at 21:29
  • I've edited my post, i'm runnong it on dos – Xander Vane Jan 13 '15 at 21:39
  • @XanderVane Are you trying to remove the file extension? Or are you trying to remove the four characters before the file extension? For example - Should `10_myfile_12345_6789.txt` become `10_myfile_12345_6789`, or `10_myfile_12345_.txt`? – unclemeat Jan 13 '15 at 21:58
  • @unclemeat im trying to remove the four characters before the file extension – Xander Vane Jan 13 '15 at 22:03
  • 1
    There are many utilities, like RenameMaster, that can do this. Why can you not use one of them? – Dour High Arch Jan 13 '15 at 22:05
  • im not allowed to install anything in my laptop even if its freeware cause of security risk – Xander Vane Jan 13 '15 at 22:06
  • That RenameMaster was awesome! It removed the last character from the file name of 630 files, saving me tons of time. I didn't even need to install anything. It's just a ZIP file with an executable that runs. – CaptainGenesisX Aug 26 '22 at 17:53

2 Answers2

8

With your recent clarification - I would do the following.

@echo off
setlocal enabledelayedexpansion
set FOLDER_PATH=C:\Some\Path\
for %%f in (%FOLDER_PATH%*) do if %%f neq %~nx0 (
    set "filename=%%~nf"
    ren "%%f" "!filename:~0,-4!%%~xf"
)
PAUSE

This will change your examples

10_myfile_12345_6789.txt
11_myfile_12345_0987.txt

Into

10_myfile_12345_.txt
11_myfile_12345_.txt

If you want to remove the trailing _ simply change !filename:~0,-4! to !filename:~0,-5!. This is simple string manipulation.

unclemeat
  • 5,029
  • 5
  • 28
  • 52
  • thanks for this, but it seems that the directory name was changed – Xander Vane Jan 13 '15 at 22:26
  • @XanderVane I didn't actually test my solution - My bad. See the updated version. Added `*` in the for loop to get contents of directory, instead of the directory itself. – unclemeat Jan 13 '15 at 22:28
0
::working script to rename + remove suffix
::fixed problem file is not found while rename.
@echo off
set /a count = 0
for %%i in ("*.ts") do (set fname=%%i) & call :rename
goto :eof
:rename
::template name ==>   names__1xxx.ts
::to rename the begin  change zero to something
set name=%fname:~0,-8%
set /a count=count+1
::by random or count i bypass the problem of file not found while rename
ren "%fname%" "%name%_%count%.ts`

Results :

before : names__1xxx.ts

after : names__1.ts

DIMM_V2
  • 105
  • 1
  • 9