0

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 :

tester_21062016_0908.txt

Thanks for any hints!

1 Answer 1

0

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!

pause

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.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .