1

In a batch file I am reading the content of a file to version parameter

set /p version=<"\\10.10.20.13\Versions Control\File.txt"

in File.txt the value is This is a text with an extra space in the end

How can I read the content of File.txt and trim the extra space after the last char This is a text?

user829174
  • 6,132
  • 24
  • 75
  • 125
  • possible duplicate of [How to remove trailing and leading whitespace for user-provided input in a batch file?](http://stackoverflow.com/questions/3001999/how-to-remove-trailing-and-leading-whitespace-for-user-provided-input-in-a-batch) – DavidPostill Jun 09 '15 at 12:00

1 Answers1

0

You can use set again to extract part of a variable (substring).

set /p version=<"\\10.10.20.13\Versions Control\File.txt"
set result=%version:~0,-1%
  • ~0 don't skip any characters (start at the beginning)
  • -1 keeps all characters except the last one.

Source Variables: extract part of a variable (substring)

Syntax

  %variable:~num_chars_to_skip%
  %variable:~num_chars_to_skip,num_chars_to_keep%

This can include negative numbers:

  %variable:~num_chars_to_skip, -num_chars_to_keep%
  %variable:~-num_chars_to_skip,num_chars_to_keep%
  %variable:~-num_chars_to_skip,-num_chars_to_keep%

...

Examples

SET _test=123456789abcdef0

...

::Extract everything BUT the last 7 characters

SET _result=%_test:~0,-7%
ECHO %_result% =123456789
DavidPostill
  • 7,734
  • 9
  • 41
  • 60