-1

These are the questions I used to build these commands:

  1. Assign output of a program to a variable using a MS batch file
  2. The filename, directory name, or volume label syntax is incorrect inside batch
  3. How to set commands output as a variable in a batch file

If I run the command, by creating a temp file, everything works fine:

"%CYGWIN_ROOT%bin\cygpath.exe" -u "%InstallImprovedSettings%" > motherfockingtemp.txt
set /p InstallImprovedSettingsUnix=<motherfockingtemp.txt
"%CYGWIN_ROOT%\bin\rm" -fv motherfockingtemp.txt

But, creating a file to assign a variable is too much overkill. If I try to do so, without creating temporary and silly files as follows:

FOR /F "tokens=* USEBACKQ" %%g IN (`'%CYGWIN_ROOT%bin\cygpath.exe' -u "%InstallImprovedSettings%"`) do (
SET "InstallImprovedSettingsUnix=%%F"
)

Batch gives the error:

FOR /F "tokens=* USEBACKQ" %g IN (`'C:\CygwinPortable\Cyg  win\bin\cygpath.exe' -u "C:\CygwinPortable\Cyg  win\cygwin-install-improved-settings.sh"`) do (SET "InstallImprovedSettingsUnix=%F" )
The filename, directory name, or volume label syntax is incorrect.

And if I replace single quotes with double quotes:

FOR /F "tokens=* USEBACKQ" %g IN (`"C:\CygwinPortable\Cyg  win\bin\cygpath.exe" -u "C:\CygwinPortable\Cyg  win\\cygwin-install-improved-settings.sh"`) do (SET "InstallImprovedSettingsUnix=%F" )
'C:\CygwinPortable\Cyg' is not recognized as an internal or external command,
operable program or batch file.

How can I use commands with spaces on their name?

Magoo
  • 77,302
  • 8
  • 62
  • 84
Evandro Coan
  • 8,560
  • 11
  • 83
  • 144

1 Answers1

2

You cannot use single-quotes to just enclose the command itself; either use single-quotes to enclose the whole command line including all the arguments, or, when using the usebackq option, use back-ticks instead.

The last attempt of yours complies with that (here once again but with the correct for meta-variable used in the loop body):

for /F "tokens=*" %%g in ('"%CYGWIN_ROOT%\bin\cygpath.exe" -u "%InstallImprovedSettings%"') do (set "InstallImprovedSettingsUnix=%%g")

But this still fails, because for /F executes the cygpath.exe command line in a new Command Prompt instance by cmd /c, which strips the first and last quotation marks and leaves the following invalid command line behind:

%CYGWIN_ROOT%\bin\cygpath.exe" -u "%InstallImprovedSettings%

To prevent this behaviour, just place an additional pair of quotation marks; it is a good idea to escape them in order not to have to change any other potential escaping:

for /F "tokens=*" %%g in ('^""%CYGWIN_ROOT%\bin\cygpath.exe" -u "%InstallImprovedSettings%"^"') do (set "InstallImprovedSettingsUnix=%%g")
aschipfl
  • 33,626
  • 12
  • 54
  • 99