Hopefully someone can help me on this:

I have a batchfile where I want to combine Variables and include a function with a Variable number for the concatenate - for better understanding see attached script:

set vSourcenm=tester.txt

Defines a Variable of the Filetype like ".txt"

set vTargettyp=%vSourcenm:~4%

set vDateTimeStamp=21062016_0908 (Simplified - sets the Date and Time)

set str_len=10 (this one is counted with a loop)

set vTargetnm=%vSourcenm:~0,-%str_len%%_%vDateTimeStamp%%vTargettyp%

The Output of vTargetnm should be :


Thanks for any hints!


This required the use of delayed expansion to allow the number in the variable to be evaluated before the sub string.

setlocal enabledelayedexpansion

set vSourcenm=tester.txt

rem Defines a Variable of the Filetype like ".txt"
set vTargettyp=%vSourcenm:~4%

rem define how much of the string to remove [equal to the part saved to the extension
set str_len=4

rem Remove the extension from the filename
set vSourcenm=!vSourcenm:~0,-%str_len%!

REM (Simplified - sets the Date and Time)
set vDateTimeStamp=21062016_0908

set vTargetnm=!vSourcenm!_!vDateTimeStamp!!vTargettyp!

echo !vTargetnm!


Your question did not agree with the example output, so the str_len was set to a value of 4 to demonstrate the delayed parsing of the environment variables.

Also the output of the %DATE% and %TIME% is based on system regional settings, so you will want to make sure those are set to what you need them before attempting to parse them in batch.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.