0

I am trying to write a script that prints a variable. This is the code:

@echo off

set workspace=%CD%
set artifact_ids=online_deploy

for %%x in (%artifact_ids%) do (
    echo %workspace%
)

I get this error when I run the script from any folder inside C:\Program Files (x86): \ was unexpected at this time.

I am guessing there is a problem with the path name since I can run it succesfully from anywhere else except for Program Files (x86).

Any help will be appreciated.

Sabin Antohe
  • 41
  • 1
  • 6

3 Answers3

2

Use the short name for the "Program Files (x86)" directory. Actually, it works on all directories. There might be other directories that contain spaces and special characters.

set "workspace=%CD%"
FOR /F "tokens=*" %%p IN ("%workspace%") DO (SET "workspace=%%~sp")
lit
  • 14,456
  • 10
  • 65
  • 119
2

The problem is, that the variable is expanded "too early", so the ) from Program Files (x86) accidently closes the for loop.

There are at least three methods to safely handle this issue (besides using shortnames):

1) enclosing the string in quotes: echo "%workspace%", set "var=%workspace%"
2) escaping the closing paranthese
3) Delayed expansion

The following example shows all three methods:

@echo off
setlocal enabledelayedexpansion
set "workspace=%ProgramFiles(x86)%"
set "artifact_ids=online_deploy"
for %%x in (%artifact_ids%) do (
    echo using quotes: "%workspace%"
    echo escaping:     %workspace:)=^)%
    echo delayed exp.: !workspace!
)

What method is the best, depends on your acutal code. There are pros and cons with all of them.

Stephan
  • 53,940
  • 10
  • 58
  • 91
0

Sadly, your code as posted does not demonstrate the problem. You're apparently substituting echo for the actual command you're using. Commendable, but in this case, misleading.

The problem is that the ) is being interpreted as the closing parenthesis of the do. How to fix it will depend on your actual code.

Magoo
  • 77,302
  • 8
  • 62
  • 84
  • What do you mean? Have you tried running the code yourself? The problem is reproducible with echo also... – Sabin Antohe Dec 11 '17 at 18:00
  • @SabinAntohe, we have no idea what the current directory is when you run that batch file. – Squashman Dec 11 '17 at 18:17
  • Yes I've tried your code as posted. It produce a report `Program Files (x86` - no "was unexpected at this time" as reported. – Magoo Dec 11 '17 at 18:18
  • @Squashman : you think maybe "I can run it succesfully from anywhere else except for Program Files (x86)" might be a clue??? – Magoo Dec 11 '17 at 18:19
  • Yeah, sorry. To reproduce this problem you must run the script from a folder inside Program Files (x86). For example: Program Files (x86)\Folder – Sabin Antohe Dec 11 '17 at 18:21
  • So it's the same problem - I ran it from `program files (x86)`, but if it was say `program files (x86)\somewhere...` then the commands that `cmd` would see would be `echo program files (x86)\somewhere...` where the `do (` is matched by the `)` of `x(86)` and `cmd` wonders what to do with `\somewhere...`. – Magoo Dec 11 '17 at 18:25
  • I think I understand what you mean now. Yes, my real code is much more complex. @lit Solution seems to work for the code I pasted here. Will see tomorrow if this work also on the real script. Thanks. – Sabin Antohe Dec 11 '17 at 18:34
  • Oh, it will - but the same problem will occur with any directory containing `)` - and probably also any of the usual gang of `poison` characters (which have special meaning for batch) - especially `&`. As another approach, I'd try using subroutines - see `call /?` from the prompt. – Magoo Dec 11 '17 at 18:37