Help on string manipulation needed

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
Christian Birr
Posts: 2
Joined: 02 Nov 2011 08:42

Help on string manipulation needed

#1 Post by Christian Birr » 02 Nov 2011 08:56

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

jeb
Expert
Posts: 1043
Joined: 30 Aug 2007 08:05
Location: Germany, Bochum

Re: Help on string manipulation needed

#2 Post by jeb » 02 Nov 2011 09:37

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

Christian Birr
Posts: 2
Joined: 02 Nov 2011 08:42

Re: Help on string manipulation needed

#3 Post by Christian Birr » 02 Nov 2011 09:53

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

Post Reply