0

Ex: doc_1.2.3.jar

Expected output: doc.jar

dir /b
for %%f in (*_*.jar) do call :ProcessFile %%f
goto :finished

:ProcessFile
set str=%1
rename %1 %str:_=%
goto :eof

:finished
echo ----
dir /b

I tried using wildcards, but it doesn't seem to work

rename %1 %str:_*.jar=.jar%
  • Use `%~n1` to get the file name and `%~x1` to get the extension; then use string substitution: `set str=%~n1?`, `set strR=%str:*_=%`, `set strL=!str:%strR%?=!` (variable `strL` contains the result); the last one requires [delayed expansion](http://stackoverflow.com/a/10558905/5047996)... this splits the string (file name) at the first `_`, the `?`, which is invalid in file names, is just used temporarily... – aschipfl Nov 27 '15 at 22:24

1 Answers1

0

A way :

@echo off

for /f "tokens=1,2 delims=_" %%a in ('dir /b/a-d *.jar') do rename "%%a_%%b" "%%a.jar" 2>nul
SachaDee
  • 9,245
  • 3
  • 23
  • 33