1

The problem here is that, after creating a symlink to a batch file, the link does not run the batch file. Example:

mklink x x.cmd

When clicking on x it opens x.cmd in Notepad! Ordinary shortcuts (.lnk-files) work as expected.

> ftype cmdfile
cmdfile="%1" %*
> assoc .cmd
.cmd=cmdfile

So each .cmd-file should run itself.

Is there a solution?

Andreas Spindler
  • 7,568
  • 4
  • 43
  • 34
  • Would a method of programmatically creating a .lnk file be an acceptable solution? – rojo Feb 20 '13 at 15:36
  • In fact I'm using `mklink` from a batch file to create some links. I could use Cygwin's `mkshortcut`, or even create an intermediate .cmd-file that `start`s the actual .cmd-file. However, `mklink` would be the natural solution. – Andreas Spindler Feb 20 '13 at 15:39
  • yeah, I get an `Open With` dialog when double-clicking anything I create via `mklink`, either soft or hard. Unless you create the link as a hard link and give it a .cmd or .bat extension, the only solution I can think of is to create an .lnk file rather than a symlink. It doesn't have to be with cygwin's `mkshortcut` though. It can be done with vbscript. – rojo Feb 20 '13 at 16:00
  • It seems like MS paid no attention on file links. `start x` opens notepad too, and running `.\x` says the command was not found. Its interesting how the simplest functions on Windows misbehave. Although `mklink` was an long-awaited and important feature, it seems as if only separate shortcut files can provide working file links. – Andreas Spindler Feb 20 '13 at 16:22
  • Agreed. Well, if you are considering making a .lnk file instead of a symlink, see this [how to make a shortcut from CMD](http://superuser.com/questions/392061/) page for ideas. – rojo Feb 20 '13 at 17:55
  • When you double-click a file, the file extension is used to determine the action to take. So it's hardly surprising that when you double-click a file named "x" (no extension) it doesn't get treated as a batch file. It's somewhat odder that even a properly-extensioned symlink to a batch file doesn't work (it does from the command line, but not from Explorer) but OTOH I don't think symlinks were ever intended as a replacement for shortcuts. – Harry Johnston Feb 21 '13 at 04:40
  • Before the extension is seen the file path has to be resolved... Shortcuts are an MSDOS-anachronism. They existed already in Windows 1 as .pif-files. Since then they can only be created programmatically. Therefore we only have `mklink` (and maybe `linkd` from the Windows 2003 Resource Kit). And they do not work. – Andreas Spindler Feb 21 '13 at 08:48
  • And, btw, a symlink is a symlink is a symlink. Like a finger pointing to the moon is not the moon. It is just an indirection with an arbitary name. – Andreas Spindler Feb 21 '13 at 09:05
  • Just because UNIX conflates symlinks and shortcuts doesn't make it a law of nature! Shortcuts contain more information than symlinks; for example, they can specify a current directory, command-line parameters, and a different icon. They're not an anachronism by any means. Symlinks, on the other hand, are followed by the file system, not by the application, so the application doesn't see the extension of the target. (Which is just as well, since they're mostly used to fool applications into opening a different file than the one they think they're opening.) – Harry Johnston Apr 06 '13 at 02:13
  • Incidentally, if you're wanting to be able to do everything from the command line, you should be looking at migrating to PowerShell. The legacy cmd.exe *is* an anachronism, albeit a still useful one IMO. – Harry Johnston Apr 06 '13 at 23:07
  • Unlike their ancestors (.pif-files) shortcuts are no longer recognized by Windows/Explorer. They have a shadow existence. For example, directory shortcuts are displayed like normal files. But symbolic links to directories appear aligned with real directories, and when grouping by directory, directory shortcuts are just files. IMHO this makes them less useful. Over the years I also found that a .cmd-file in general works better than a shortcut: it can do everything a shortcut does, is more flexible and easier to edit. – Andreas Spindler Apr 08 '13 at 10:03

1 Answers1

3

One way would be to make hard links (mklink /H). But since I think you just need some kind of shortcut, try this:

1) Make a shell script *.vbs like this (shortcut_helper.vbs):

set WshShell = WScript.CreateObject("WScript.Shell" )
set oShellLink = WshShell.CreateShortcut(Wscript.Arguments.Named("shortcut") & ".lnk")
oShellLink.TargetPath = Wscript.Arguments.Named("target")
oShellLink.Arguments = Wscript.Arguments.Named("arg")
oShellLink.WindowStyle = 1
oShellLink.Save

2) Make your batch script start it like this from an batch file:

path_to_vbs\shortcut_helper /target:"file_path\file.bat" /shortcut:"shortcut_name" /arg:"optional_arguments"

Now 2) creates shortcuts (*.lnk) for you and you can then move them anywhere you like =)

Be careful though, *.vbs files MAY need admin rights in some circumstances.

QAT
  • 53
  • 5