There is a very simple solution that uses variable expansion search and replace to inject a REM command.
@echo off
setlocal
for %%F in (*) do call set "var=%%F" & call :procName
exit /b
:procName
::truncate at the 1st occurance of ___
set "var=%var:___="&rem %
::print the result
set var
exit /b
The critical line is set "var=%var:___="&rem %
Suppose var=Part1___Part2
After substitution, the line becomes: set "var=Part1"&rem Part2
The above solution truncates at the first occurance of ___
.
A name like Part1___Part2___Part3
will result in Part1
.
If you want to truncate at the last occurance of ___
and get a result like Part1__Part2
, then the solution is more complicated.
I use search and replace to change ___
to a folder delimiter, then a 2nd FOR with the ~p modifier to strip off the last part, and then search and replace to restore the leading ___
delimiter(s).
@echo off
setlocal disableDelayedExpansion
for %%F in (*) do (
set "var=%%F"
setlocal enableDelayedExpansion
set "var2=!var:___=\!"
if "!var2!" neq "!var!" for %%A in ("\!var2!") do (
endlocal
set "var=%%~pA"
setlocal enableDelayedExpansion
set "var=!var:~1,-1!"
set "var=!var:\=___!"
)
echo var=!var!
endlocal
)