1

I have the following batch statement:

for /f "delims=" %%x in (file.lst) do set "offendingfile=%%x"

Although for some really odd reason, when it is called it outputs:

"C:\Windows\calc.exe "

instead of

"C:\Windows\calc.exe"

Since there is a trailing space, I can't use it properly with any other statements in the batch file, does anyone know why it does this and how to fix this, as its been driving me nuts!

Ash Burlaczenko
  • 24,778
  • 15
  • 68
  • 99
Jay
  • 53
  • 1
  • 1
  • 5
  • You might find [this solution](http://stackoverflow.com/questions/3001999/how-to-remove-trailing-and-leading-whitespace-for-user-provided-input-in-a-batch/3002207#3002207) useful. – Andriy M Jun 03 '11 at 15:32

2 Answers2

1

does your file.lst file has a trailing space after the file name?

I checked this with file.lst having: c:\windows\calc.exe and the output was correct, but if the file.lst file contains c:\windows\calc.exe<SPACE>, the output is the same that you are getting (and is the expected output as well).

Vikram.exe
  • 4,565
  • 3
  • 29
  • 40
0

I believe that the delims= portion of the for statement is removing the default behavior of using spaces as delimiters. If you remove that portion, then it should remove the trailing blank:

for /f %%x in (file.lst) do set "offendingfile=%%x"
Mark Wilkins
  • 40,729
  • 5
  • 57
  • 110
  • 1
    This does solve the problem of trailing blanks as well as introduce another one, of incomplete paths/names, like `Program Files`. – Andriy M Jun 03 '11 at 15:08
  • @Andriy: Yes, you are correct. For tasks like this, it is often easier to resort to something like Ruby, Python, Perl, etc. If the OP is using something like [Take Command](http://jpsoft.com/index.html), then functions like @trim are available. Otherwise it might require something like is [suggested here](http://www.dostips.com/DtTipsStringManipulation.php#Snippets.TrimRightFOR) – Mark Wilkins Jun 03 '11 at 15:26