Quick Links

If you have a high number of folders set up for your work and need to use the same script file in all of them during the work day, then what is the easiest way to accomplish that beyond a lot of copying and pasting? Today's SuperUser Q&A post has some helpful answers and advice for a frustrated reader.

Today’s Question & Answer session comes to us courtesy of SuperUser—a subdivision of Stack Exchange, a community-driven grouping of Q&A web sites.

Screenshot courtesy of csaveanu (Flickr).

The Question

SuperUser reader Elliot is looking for the best way to have the same file appear to be in multiple folders at the same time:

I have 50+ folders, each of which contains a large amount of data that needs to be processed. All of them are processed using the same exact code, utilizing os.path.dirname(os.path.realpath(file)) to get the directory in which the python script is located so there is no manual editing needed by the user, they just need to double click.

I need the script to appear as if it is in each folder while actually being in only one place so that I can edit it once, then when it is run from any of these locations have the folder path be correct. The alternative is editing the master and then pasting it one folder at a time through all 50+ folders every time I update the code, which is very tedious and error prone. On Linux, I could set this up with a symbolic link, but I cannot figure out a way to do this with Windows.

Alternatively, a way to paste the file into all the target directories at once, instead of one at a time, would accomplish the same goal.

Is there a way to do this rather than copying and pasting the script file one folder at a time?

The Answer

SuperUser contributor gronostaj has the answer for us:

You need a Symbolic Link or a Hard Link.

Symbolic Links (or Symlinks for short) are quite similar to shortcuts: there is one actual file and multiple references (Symlinks) to it. They even have that little arrow on the icons. Unlike shortcuts, Symlinks can have any extension.

Hard Links bind a file on a hard drive to a location in the directory tree. Each file has at least one Hard Link, otherwise it would not exist in any directory. If a file has multiple Hard Links, the original one cannot be distinguished from the others and the file physically exists in only one location.

Both Have Their Limitations:

  • Some software does not play nicely with Symlinks.
  • Deleting the original file leaves all of its Symlinks broken.
  • You cannot Hard Link folders (but you can create a Directory Junction if a Symlink is not enough).
  • Creating cross-partition Hard Links is impossible.

Symlinks are usually sufficient.

To Create a Symlink or a Hard Link:

1. Launch a privileged command line: Press the Windows Key, type cmd, then press Ctrl+Shift+Enter.

2. Issue the mklink command:

  • mklink link_name link_target for a file Symlink
  • mklink /d link_name link_target for a folder Symlink
  • mklink /h link_name link_target for a file Hard Link
  • mklink /j link_name link_target for a Directory Junction

Have something to add to the explanation? Sound off in the comments. Want to read more answers from other tech-savvy Stack Exchange users? Check out the full discussion thread here.