Mostly Linux & Python syntax notes and hyperlinks.

Thursday, December 4, 2014

Windows Batch Script: substring to generate output name from input name without extension

If you want your batch script to create an output file name from an input file name by removing its 3-character extension, then you can use the batch substring method.
The substring syntax :~ is inserted between the opening and closing % signs that surround your variable.

Thus, instead of %complete_variable_name%,you have
%complete_variable_name:~[chars-to-skip],[chars-to-collect]%
  •  if [chars-to-skip] is negative, then it starts from the end of the string
  •  if [chars-to-collect] is negative, then it ends that many characters from the end of the string
e.g.
C:> set filename=abcde.txt 
C:> echo %filename% 
abcde.txt 
C:> echo %filename:~0,-3% 
abcde. 
C:> echo %filename:~-3,3% 
txt 
C:> set outname=%filename:~0,-4%.output 
C:> echo %outname% 
abcde.output

No comments:

Post a Comment