lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


I'm facing a weird issue, I'm calling an os command with an argument. Now both the command and the argument contain spaces in their filenames, hence I need to quote them with " ".

The issue; it won't work if both (command and argument) are quoted, all other cases it does work as expected.

Test code showing the issue (even without spaces in the names):
--8x--------------------------------------------------
md = [[c:\temp\exec_test.bat]]
target = [[C:\rocktree\lib\lua\pe-parser.lua]]

local function Q(arg)
  return '"'..arg..'"'
end

local function test_exec(cmd, target)
  cmd = cmd .." ".. target
  print("","Testing:", cmd)
  print("","",os.execute(cmd))
  print("")
end

print("execute")
test_exec(cmd, target)       -- none quoted
test_exec(Q(cmd), target)    -- command quoted
test_exec(cmd, Q(target))    -- argument quoted
test_exec(Q(cmd), Q(target)) -- both quoted   --> fails
--8x--------------------------------------------------


The command called is a simple batch script displaying the command called and its first 3 parameters;
--8x--------------------------------------------------
@echo off
echo CMD: =%0=
echo 1  : =%1=
echo 2  : =%2=
echo 3  : =%3=
--8x--------------------------------------------------

The output
------------------------------------------------------
C:\Users\Thijs\Desktop>c:\temp\exec_test.lua
execute
        Testing:        c:\temp\exec_test.bat C:\rocktree\lib\lua\pe-parser.lua
CMD: =c:\temp\exec_test.bat=
1  : =C:\rocktree\lib\lua\pe-parser.lua=
2  : ==
3  : ==
                0

        Testing:        "c:\temp\exec_test.bat" C:\rocktree\lib\lua\pe-parser.lua
CMD: =c:\temp\exec_test.bat=
1  : =C:\rocktree\lib\lua\pe-parser.lua=
2  : ==
3  : ==
                0

        Testing:        c:\temp\exec_test.bat "C:\rocktree\lib\lua\pe-parser.lua"
CMD: =c:\temp\exec_test.bat=
1  : ="C:\rocktree\lib\lua\pe-parser.lua"=
2  : ==
3  : ==
                0

        Testing:        "c:\temp\exec_test.bat" "C:\rocktree\lib\lua\pe-parser.lua"
The filename, directory name, or volume label syntax is incorrect.
                1


C:\Users\Thijs\Desktop>
------------------------------------------------------

Apparently the command is not found in the final (both quoted) case.
Anyone seen this before, or knows how to resolve this?

Thijs



PS: If I execute the exact same command on a command prompt, it works as expected.

------------------------------------------------------
C:\Users\Thijs\Desktop>"c:\temp\exec_test.bat" "C:\rocktree\lib\lua\pe-parser.lua"
CMD: ="c:\temp\exec_test.bat"=
1  : ="C:\rocktree\lib\lua\pe-parser.lua"=
2  : ==
3  : ==

C:\Users\Thijs\Desktop>
------------------------------------------------------