Page 1 of 1

Help on string manipulation needed

Posted: 02 Nov 2011 08:56
by Christian Birr
Hello all,

I try to extract the first 6 characters of a given filename to produce a command. Given a file name of M00091.het the following script

FOR %%f IN (*.het) DO F:/HERC370/HERCULES/HETINIT -i D:/Programme/HERCULES/zOS/tapes/HSM1/DONE/%%f %%F:~0,5 HERCULES

should produce a command

HETINIT D:/Programme/HERCULES/zOS/tapes/HSM1/DONE/M00091.HET M00091 HERCULES

but the string extraction only produces %F:~0,5 . So what am I missing ? Any help is appreciated.

Greetings from lower bavaria

Christian

Re: Help on string manipulation needed

Posted: 02 Nov 2011 09:37
by jeb
It can't work this way, as in batch the substring syntax is only allowed for variables not for parameters.
Btw. mixing of %%f and %%F can't work, as parameters are case sensitive, but variables are not.

So you have to copy the value first and as you are inside brackets you should use the delayed expansion syntax,
as the percent syntax will fail here, as percents are expanded before the block is executed (while parsing),
delayed expansion (!var!) will be evaluated at execution time.

Code: Select all

setlocal EnableDelayedExpansion
FOR %%f IN (*.het) DO (
   set "str=%%f"
   set "prefix=!str:~0,5!"
   F:/HERC370/HERCULES/HETINIT -i D:/Programme/HERCULES/zOS/tapes/HSM1/DONE/%%f !prefix! HERCULES
)


jeb

Re: Help on string manipulation needed

Posted: 02 Nov 2011 09:53
by Christian Birr
jeb,

thank you. I'm not at all a scripting guy on windows, more Assembler and REXX on mainframes. Will have to catch up with DOS, too.
Once again, thank you very much.

Christian