如何使单个文件“出现”同时出现在多个文件夹中?

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.

今天的“问答”环节由SuperUser提供,它是Stack Exchange的一个分支,该社区是由社区驱动的Q&A网站分组。

Screenshot courtesy of csaveanu (Flickr).

屏幕截图由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:

超级用户阅读器Elliot正在寻找使同一文件同时出现在多个文件夹中的最佳方法:

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.

我有50多个文件夹,每个文件夹包含大量需要处理的数据。 使用os.path.dirname(os.path.realpath(file))来使用相同的精确代码处理所有文件,以获取python脚本所在的目录,因此用户无需手动编辑,它们只需双击即可。

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.

我需要脚本显示为好像它位于每个文件夹中,而实际上仅位于一个位置,以便我可以对其进行一次编辑,然后从这些位置中的任何一个运行脚本时,文件夹路径都是正确的。 另一种方法是编辑母版,然后每次更新代码时,一次将其粘贴到所有50个以上的文件夹中,一次粘贴一个文件夹,这非常繁琐且容易出错。 在Linux上,我可以使用符号链接进行设置,但是我无法找到使用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:

超级用户贡献者gronostaj为我们提供了答案:

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.

硬链接将硬盘驱动器上的文件绑定到目录树中的某个位置。 每个文件至少具有一个Hard Link ,否则它将不在任何目录中。 如果文件具有多个“硬链接” ,则无法将原始文件与其他文件区分开,并且文件实际上仅存在于一个位置。

Both Have Their Limitations:

两者都有其局限性:

  • Some software does not play nicely with Symlinks.

    某些软件不能很好地与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.

1.启动特权命令行:按Windows键,键入cmd ,然后按Ctrl + Shift + Enter

2. Issue the mklink command:

2.发出mklink命令:

  • mklink link_name link_target for a file Symlink

    文件的mklink link_name link_target符号链接

  • mklink /d link_name link_target for a folder Symlink

    mklink / d文件夹的link_name link_target符号链接

  • mklink /h link_name link_target for a file Hard Link

    mklink / h link_name link_target文件的硬链接

  • mklink /j link_name link_target for a Directory Junction

    mklink / j link_name link_target用于目录连接



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.

有什么补充说明吗? 在评论中听起来不错。 是否想从其他精通Stack Exchange的用户那里获得更多答案? 在此处查看完整的讨论线程

翻译自: https://www.howtogeek.com/214212/how-do-you-make-a-single-file-appear-to-be-in-multiple-folders-at-the-same-time/

相关文章: