简体   繁体   English

将一个文件夹中的文件路径递归映射到另一个文件夹

[英]Recursively mapping file paths in one folder to another folder

Let's say I have a folder (folder_1) with the following structure: 假设我有一个具有以下结构的文件夹(folder_1)

/folder_1
  /dir_1
     - file_1_1.txt
     - file_1_2.txt
  /dir_2
     - file_2_1.txt
     /dir_2_1
       - file_2_1_1.txt
  - file_1.txt

Now, let's say I have another folder (folder_2) with the following structure: 现在,假设我有另一个具有以下结构的文件夹(folder_2)

/folder_2
  /dir_1
     - file_1_1.txt
     - default.txt
  /dir_2
     - file_2_1.txt
     - default.txt
  - default.txt

I need to map every file in folder_1 to a file in folder_2 such that: 我需要在FOLDER_1每个文件映射到一个文件中folder_2这样的:

  1. /folder_1/dir_1/file_1_1.txt maps to /folder_2/dir_1/file_1_1.txt . /folder_1/dir_1/file_1_1.txt映射到/folder_2/dir_1/file_1_1.txt
  2. /folder_1/dir_1/file_1_1.txt maps to /folder_2/dir_1/default.txt /folder_1/dir_1/file_1_1.txt映射到/folder_2/dir_1/default.txt
  3. /folder_1/dir_2/file_2_1.txt maps to /folder_2/dir_2/file_2_1.txt /folder_1/dir_2/file_2_1.txt映射到/folder_2/dir_2/file_2_1.txt
  4. /folder_1/dir_2/dir_2_1/file_2_1_1.txt maps to /folder_2/dir_2/default.txt /folder_1/dir_2/dir_2_1/file_2_1_1.txt映射到/folder_2/dir_2/default.txt
  5. /folder_1/file_1.txt maps to /folder_2/default.txt /folder_1/file_1.txt映射到/folder_2/default.txt

I am not the best communicator, so hopefully, the above pattern makes sense to you guys. 我不是最好的交流者,所以希望上述模式对你们来说有意义。 The question is language agnostic really, but an answer in PHP and/or Javascript would be really great. 这个问题确实是语言不可知的,但是用PHP和/或Javascript回答是非常好的。

So far, I was able to accomplish this in PHP using FileIterator, RecursiveDirectoryIterator, and a bunch of custom classes that extract and then map the path to the files one by one. 到目前为止,我已经可以使用FileIterator,RecursiveDirectoryIterator和一堆自定义类在PHP中完成此任务,这些类提取并逐一将路径映射到文件。

This makes me wonder if I am missing an easier way to do this simple mapping. 这使我想知道是否缺少一种更简单的方法来执行此简单映射。 Maybe using regex named groups or something? 也许使用正则表达式命名组或什么?

**Edit: ** **编辑:**

Is it possible that for each file (file path) in folder_1 , we use a regex pattern to find (reduce) the best match out of a map of all file paths in folder_2 ? 是否有可能针对folder_1中的每个文件(文件路径),使用正则表达式模式从Folder_2中所有文件路径的映射中查找(减少)最佳匹配?

Further edit: 进一步编辑:

This is for mapping data files in folder_1 to template files in folder_2 . 这是映射的数据文件中FOLDER_1folder_2模板文件。 If for a file in folder_1 , an exact matching file path (including filename) in folder_2 is not found, we look for default.txt . 如果在FOLDER_1文件,在folder_2精确匹配的文件路径(包括文件名)未找到,我们寻找default.txt If default.txt is not found, then we move up a directory and use that parent directory's default.txt . 如果未找到default.txt ,则我们将目录上移并使用该父目录的default.txt This way, we keep moving up directory levels till we find the first default.txt . 这样,我们将继续提升目录级别,直到找到第一个default.txt为止。

First, use your recursive directory scanner to scan all of the folder_2 directory tree. 首先,使用递归目录扫描程序扫描所有folder_2目录树。 Build a hash table that contains the file names, without the folder_2 prefix. 建立一个哈希表,其中包含文件名,没有folder_2前缀。 So your hash table would contain: 因此,您的哈希表将包含:

/dir_1
/dir_1/file_1_1.txt
/dir_1/default.txt
/dir_2/file_2_1.txt
/dir_2/default.txt
/default.txt

Now, start scanning folder_1 . 现在,开始扫描folder_1 When you get a file, strip folder_1 from the front, and look for the resulting string in the hash table. 当您得到一个文件时,从前面剥离folder_1 ,然后在哈希表中查找结果字符串。 If it's there, then you have a match. 如果在那里,那么您有一场比赛。

If the file is not there, replace the last segment with "default.txt", and try again. 如果文件不存在,请用“ default.txt”替换最后一段,然后重试。 So, when you begin scanning folder_1 , you get: 因此,当您开始扫描folder_1 ,您将获得:

/folder_1/dir_1/file_1_1.txt

You look up dir_1/file_1_1.txt in the hash table and find it. 您在哈希表中查找dir_1/file_1_1.txt并找到它。 You have a match. 你有一场比赛。

Next, you get /folder_1/dir_1/file_1_2.txt . 接下来,您将获得/folder_1/dir_1/file_1_2.txt You look up /dir_1/file_1_2.txt in the hash table and don't find it. 您在哈希表中查找/dir_1/file_1_2.txt ,但找不到它。 So you replace file_1_2.txt with default.txt , giving you /dir_1/default.txt . 因此,您将file_1_2.txt替换为default.txt ,从而得到/dir_1/default.txt You look that up in the hash table, find it, and you have a match. 您在哈希表中查找,找到它,然后找到一个匹配项。

Now, if /dir_1/default.txt did not exist, then you would again adjust the file name to remove the last directory. 现在,如果/dir_1/default.txt不存在,那么您将再次调整文件名以删除最后一个目录。 That is, you'd remove /dir_1 , and you'd look up /default.txt in the hash table. 也就是说,您将删除/dir_1 ,并在哈希表中查找/default.txt

In pseudo code it looks like this: 用伪代码看起来像这样:

for each file in folder_1
    name = strip `/folder_1` from the name
    if name in hash table then
        match found
        continue (next file)
    end if
    replace file name (everything after the last '/') with "default.txt"
    do
        if name in hash table then
            match found
            continue (next file)
        end if
        remove the last slash, and everything between it and the previous slash.
        (so "/dir_1/default.txt" becomes "/default.txt")
    while name.length > 0

    // if you get here, no match was found
end for

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM