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.
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:\
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:
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,
*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
Didn't want to create a junction, so I tested with this code:
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,
I now get the same result you do with
cmd_64 = DirWindows(0):'SysWOW64\CMD.EXE'
mklinkcmd = '/k mklink /?'
ShellExecute(cmd_64, mklinkcmd, '', @NORMAL, '')
exit
Quote from: richardh on June 25, 2014, 01:37:50 PM
Deana,
I now get the same result you do with
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,
Is it that without the /k the processing is hidden?
ShellExecute(Environment('ComSpec'), 'mklink', '', @NORMAL, '')
; results in open dos prompt no data
ShellExecute(Environment('ComSpec'), '/k mklink', '', @NORMAL, '')
; results in open dos prompt and see action
Thanks,
JH
yes /k is the "keep" window command line switch. Great for debugging command line issues.
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.
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?
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")
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.
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.
Allrighty Then!
After beating this issue to death I will post a working example
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
You are missing a close parentheses on this line: param2=strcat(NetDrive,'LinkTargetFolderName'
Also you may want to change /k to /c.
Keeping me in line aren't ya D... lol
Example corrected.
Just curious what constitutes a level up from Noob status?
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