ShellExecute mklink DOS command on Windows 7 Pro

Started by richardh, June 25, 2014, 11:18:53 AM

Previous topic - Next topic

richardh

I seem to be unable to run MKLINK DOS command on Windows 7 Pro 64

I am able to successfully create symbolic links and junctions manually using CMD prompt without issue, however,
I can't use Run, RunShell or ShellExecute to run the mklink command successfully.

Code (winbatch) Select

cmd_64 = DirWindows(0):'\Sysnative\CMD.EXE'
ShellExecute(cmd_64, 'mklink /J myjunction "':DirWindows(0):'\system32"', '', @NORMAL, '') ; no visible result

ShellExecute(Environment('ComSpec'), 'mklink 2', '', @NORMAL, '') ; purposely tried to produce an error using (mklink 2) no result except open cmd window

ShellExecute(Environment('ComSpec'), ' /?', '', @NORMAL, '') ; help switch passes correctly to CMD.EXE

ShellExecute(Environment('ComSpec'), 'mklink', '', @NORMAL, '') ;  open CMD window only



Can someone provide a tested and working example passing the mklink command?

Checked all junctions to confirm nothing was created during testing.
DIR /AL /S C:\


Deana

Two issues.

  • Remove the backslash before system32, because DirWindows(0) contains a backslash already.
  • Add /c or /k to the beginning of the mklink command line. /k will keep the DOS window open so you can check for errors. Once debugged, change to /c.

Code sample:
Code (winbatch) Select

cmd_64 = DirWindows(0):'Sysnative\CMD.EXE'
mklinkcmd = '/k mklink /J myjunction "':DirWindows(0):'system32"'
ShellExecute(cmd_64, mklinkcmd, '', @NORMAL, '')
exit


Reference: http://techsupt.winbatch.com/webcgi/webbatch.exe?techsupt/nftechsupt.web+WinBatch/How~To+Create~Junction~Points.txt

Deana F.
Technical Support
Wilson WindowWare Inc.

richardh

Deana,
*Remove the backslash before system32, because DirWindows(0) contains a backslash already... tried it both ways.

Did you happen to test this code snippet?

I ask because I'm beginning to wonder if it's my OS because this example doesn't seem to work for me either  :(

I hope I don't have to resort to sending keys to a CMD window or using a .bat file.

Thanks,
JH

Deana

Didn't want to create a junction, so I tested with this code:

Code (winbatch) Select

cmd_64 = DirWindows(0):'Sysnative\CMD.EXE'
mklinkcmd = '/k mklink /?'
ShellExecute(cmd_64, mklinkcmd, '', @NORMAL, '')
exit


On my Windows 7 x64 system, it successfully launches the command shell and displays help for mklink.  I am logged in as an administrator and using the manifest setting requested execution level =  highest available.

What happens exactly when you run this code on your system?
Deana F.
Technical Support
Wilson WindowWare Inc.

richardh

Deana,

I now get the same result you do with

Code (winbatch) Select
cmd_64 = DirWindows(0):'SysWOW64\CMD.EXE'
mklinkcmd = '/k mklink /?'
ShellExecute(cmd_64, mklinkcmd, '', @NORMAL, '')
exit



Deana

Quote from: richardh on June 25, 2014, 01:37:50 PM
Deana,

I now get the same result you do with

Code (winbatch) Select
cmd_64 = DirWindows(0):'SysWOW64\CMD.EXE'
mklinkcmd = '/k mklink /?'
ShellExecute(cmd_64, mklinkcmd, '', @NORMAL, '')
exit


SysWow64 is referencing the 32-bit command shell.

Are you running as 32-bit or 64-bit script? If 32-bit script on a 64-bit system then file redirection is at play. Please read: http://msdn.microsoft.com/en-us/library/windows/desktop/aa384187(v=vs.85).aspx
Deana F.
Technical Support
Wilson WindowWare Inc.

richardh

Deana,

Is it that without the /k the processing is hidden?

Code (winbatch) Select
ShellExecute(Environment('ComSpec'), 'mklink', '', @NORMAL, '')
; results in open dos prompt no data


Code (winbatch) Select
ShellExecute(Environment('ComSpec'), '/k mklink', '', @NORMAL, '')
; results in open dos prompt and see action


Thanks,
JH

Deana

yes /k is the "keep" window command line switch. Great for debugging command line issues.
Deana F.
Technical Support
Wilson WindowWare Inc.

richardh

Deana,

As stated previously when I use the /k switch ( '/k mklink /d ') mklink command runs fine and I've been manually closing the CMD window during testing.

Now that I'm done testing this module... I removed the ('/k ') and it no longer works.

Code (winbatch) Select
ShellExecute(Environment('ComSpec'), 'mklink / d parameters', '', @NORMAL, '')

'/k mklink / d (exact same parameters)' ; executes fine.

'mklink / d (exact same parameters)' ; doesn't work.

Any Ideas??? Does mklink command actually work on your end without the /k?

snowsnowsnow

You have to use either /k (for "keep") or /c (for "command").

You can't just remove the /k; you have to replace it with /c.

But it seems to me that you don't need to be using CMD at all.  Or ShellExecute, either.

How about just:

RunWait("mklink","params here")


td

I believe "mklink" is a command built into the command shell executable on Windows 7 and newer.  It is, therefore, necessary to use Cmd.exe to execute the "mklink" command.
"No one who sees a peregrine falcon fly can ever forget the beauty and thrill of that flight."
  - Dr. Tom Cade

snowsnowsnow

Quote from: td on June 30, 2014, 06:48:03 AM
I believe "mklink" is a command built into the command shell executable on Windows 7 and newer.  It is, therefore, necessary to use Cmd.exe to execute the "mklink" command.

OK - point taken.  ISTR that it was an external command at some point, but it may be memory playing tricks on me againââ,¬Â¦

But the advice about needing /c still applies.  FWIW, it has always annoyed me that COMMAND/CMD require the /c - in the long range scheme of things, it shouldn't be  necessary.  Then again, the Unix shells have a -c, so it was probably copied from there.  But Unix shells also allow you to read a file directly (e.g., sh file), so they need the "-c" option.  COMMAND/CMD don't support this, so they don't need /c.

richardh

Allrighty Then!

After beating this issue to death I will post a working example

Code (winbatch) Select
NetDrive='Z:\'
RoamProf = Environment("APPDATA")
param1=strcat('/c mklink /d ',RoamProf,'\LinkSourceFolderName ') ; leave space at end
param2=strcat(NetDrive,'LinkTargetFolderName')
LinkCommand = strcat(param1,param2)
returncode=RunShell(Environment("COMSPEC"), LinkCommand, "C:\", @HIDDEN, @NOWAIT)
message('Executed?',returncode)
exit


Enjoy..
JH

Deana

You are missing a close parentheses on this line:
Code (winbatch) Select
param2=strcat(NetDrive,'LinkTargetFolderName' 

Also you may want to change /k to /c.
Deana F.
Technical Support
Wilson WindowWare Inc.

richardh

Keeping me in line aren't ya D... lol
Example corrected.

Just curious what constitutes a level up from Noob status?

Deana

Quote from: richardh on June 30, 2014, 01:30:07 PM
Keeping me in line aren't ya D... lol
Example corrected.

Just curious what constitutes a level up from Noob status?

Jr Member = 50 posts
Full Member = 100 posts
Sr Member = 250 posts
Pundit = 512 posts
Deana F.
Technical Support
Wilson WindowWare Inc.