DOS batch: How to remove last part of folders names -


in batch file folder list of subfolders unknown number of underscores e.g.:

a_b_10  c_d_e_2  f_17 

i need remove last token of names i.e.

a_b  c_d_e  f 

thanks

you try last part underscore , remove string, works when last part unique.

in sample last part seems number in spite of other parts.
uses trick parse parts of string replace delimiter linefeed character.

@echo off setlocal enabledelayedexpansion set lf=^   /f "delims=" %%x in ('dir /b /ad') (     call :removelastpart "%%~x" ) exit /b  :removelastpart set "str=%~1" %%l in ("!lf!") (   /f "delims=" %%p in ("!str:_=%%~l!") set "last=%%p" ) echo last part '!last!'  rem ** remove last part replacing nothing ** set "str=!str:_%last%=!" echo !str! exit /b 

Comments

Popular posts from this blog

c# - How to get the current UAC mode -

postgresql - Lazarus + Postgres: incomplete startup packet -

angularjs - ng-repeat duplicating items after page reload -