Thomas Morse
Thomas Morse

Reputation: 59

Trying to get the last token in a file string but getting delimiter=\" was unexpected at this time

I'm not sure what is going wrong. I've been trying to follow code examples that I've found from other's that were asking a question along what I'm trying to do, and even have followed what the answers have said, and can't figure out what I'm doing wrong.

SET mypath=%~dp0
SET mypath=%mypath:~0,-1%

echo Selected Text file = %mypath%
SET STRING=%mypath%
echo %STRING%
pause
FOR /f "tokens=3* delimiters=\" %%i in ("%STRING%") do (
SET VAR= %%i
echo %%i
echo %VAR%
)
pause

the error that I get is that delimiters=\" was unexpected at this time.

Upvotes: 1

Views: 130

Answers (1)

npocmaka
npocmaka

Reputation: 57252

batch scripting language is not exactly the Shakespere's language

FOR /f "tokens=3* delims=\" %%i in ("%STRING%") do (

And as Dennis van Gils mentioned in the comments you need delayed expansion:

setlocal enableDelayedExpansion
SET mypath=%~dp0
SET mypath=%mypath:~0,-1%

echo Selected Text file = %mypath%
SET STRING=%mypath%
echo %STRING%
pause
FOR /f "tokens=3* delims=\" %%i in ("%STRING%") do (
  SET VAR=%%i
  echo %%i
  echo !VAR!
)
pause

Upvotes: 1

Related Questions