[Solved] the cmdcmdline variable output problem

Microsoft Windows
Post Reply
User avatar
MigrationUser
Posts: 336
Joined: 2021-Jul-12, 1:37 pm
Contact:

[Solved] the cmdcmdline variable output problem

Post by MigrationUser »

08 Feb 2015 18:51
boushta
here i dragged 4 files on a batch file,here is the output of cmdcmdline variable

Code: Select all

@echo off
echo %cmdcmdline%
C:\Windows\system32\cmd.exe /c ""C:\Users\boushta\Desktop\test.cmd" D:\wxchjfg.txt D:\w&xc.txt D:\dl.txt"
is there away to correct this output or is there a way to surround by double quotes the others files like :

Code: Select all

C:\Windows\system32\cmd.exe /c ""C:\Users\boushta\Desktop\test.cmd" "D:\wxchjfg.txt" "D:\w&xc.txt" "D:\dl.txt""
having this result i can parse those files correctly and work with them


note that i dragdroped those files on a batch file not passed them manually
Last edited by boushta (11 Feb 2015 14:39)

----------------------------

#209 Feb 2015 21:20
bluesxman

If you're just after the passed files, they are all present in the special variable %*. You can do this to quote them and store in a variable:

Code: Select all

@echo off
set "files="
for %%F in (%*) do (
  if not defined files (
    set "files="%%~F""
  ) else (
    call set "files=%%files%% "%%~F""
  )
)
set files
NB - your initial command line is hard limited to 8186 characters (I believe), so if you have long file paths and/or large numbers of files you will get an error.
Last edited by bluesxman (09 Feb 2015 21:26)
cmd | *sh | ruby | chef

----------------------------

#310 Feb 2015 07:35
foxidrive

Note that drap and drop fails in Windows if the file or folder contains an & character and has no spaces or long filename elements.
If you explain the task then you may get better solutions, as everything to do with batch relies on the exact task - or you may get an answer but it will fail in different circumstances.

Run this and then drag the file on top of the batch file to see.

Code: Select all

@echo off
echo(%*
echo done
pause

break >"one-two-&-three.txt"
----------------------------

#410 Feb 2015 16:32
boushta

thanks for replies but none of the solution worked because of the ampersand "&" char
again i dragged the following files all simultaneously on the batch file

Code: Select all

D:\the-best-slide-there.jpg
D:\1xc&qsd.txt
D:\kjh.txt
and the both result are :
bluesxman

Code: Select all

files="D:\the-best-slide-there.jpg" "D:\1xc"
foxidrive

Code: Select all

"D:\the-best-slide-there.jpg" D:\1xc
done
as you can see any string after the ampersand is truncated
hopefully i didn't get a cmd crash this time using your code when i drag a file containing an ampersand , unfortunately the output can not complete

i do not think there is a way other than using cmdcmdline variable, because i thinks this is the only variable that preserve & in dragged params
sadly it does not surround by quotes dragged files which do not contain space
and even if the cmdcmdline var is the only way,it is really hard to manipulate it to give you the wanted result
please is there any way around to preserve all dragged files even if & exist

----------------------------

#510 Feb 2015 19:15
Liviu

Dave Benham's code at https://stackoverflow.com/questions/854 ... ampersands … ampersands will work for some "&^!" poison chars, but still fail with the default batch separators for example "1=2;3,4.txt". I don't know of a foolproof pure batch solution, and it may be easier to just write a VBS or JS wrapper to (at least) parse the command line.

----------------------------

#610 Feb 2015 20:54
boushta


it works for ampersand but still it will create another problem
it is a pity
please how to write a js wrapper if this can solve the problem and finally pass the full arguments throught a js file

----------------------------

#710 Feb 2015 23:51
Liviu
boushta wrote:
it works for ampersand but still it will create another problem
It doesn't _create_ it, it just doesn't solve it. The same problem exists with other approaches that rely on the builtin command line parsing.
boushta wrote:
please how to write a js wrapper if this can solve the problem and finally pass the full arguments throught a js file
drop.js

Code: Select all

var sCmd = '%comspec% /c '
         + WScript.CreateObject("Scripting.FileSystemObject")
                  .GetParentFolderName(WScript.ScriptFullName)
         + '\\drop.cmd';
var vArgs = WScript.Arguments;
for(var n = 0; n < vArgs.length; n++) sCmd += ' "' + vArgs(n)+ '"';
WScript.CreateObject("WScript.Shell").Run(sCmd);
drop.cmd

Code: Select all

@echo off
for %%v in (%*) do echo %%v
pause
Copy the two files someplace into the same directory, then drag the files you want to process onto drop.js (not drop.cmd). Note that the other limitations remain (such as the max length of the command line), but each filename will be quoted (thus immune to &^=,; etc) by the time the .cmd batch receives it.


----------------------------

#811 Feb 2015 14:40
boushta

it works
Post Reply