我目前正在大学二年级,因此我的编程技能和知识并不像我希望的那么强。我暑假期间正在一家网络开发公司实习,分配给我的第一项任务让我完全不知所措。这就是我来这里寻求帮助的原因。
主文件夹中有许多子文件夹,每个子文件夹中有许多 .js、.cs 和 .php 文件 - 大约 1000 个文件。但大约有 300 个没有被使用。我需要打开每个子文件夹,看看这些文件是否被任何其他文件使用/调用。如果不是,我需要将未使用文件的位置存储在文本文件中。
我做了一些研究,发现命令 grep -r filename *
就是这样做的,但是在命令行上我无法弄清楚如何循环遍历文件夹并根据内容更改文件名文件夹内。我的工作站是 Windows 系统,安装了 Cygwin。
I am currently in my second year of college, therefore my programming skills and knowledge are not as strong as I like them to be. I am doing an internship for a web development company during my summer break and I am completely stomped on the first task that was assigned to me. That's why I'm here asking for some assistance.
In a main folder there are many sub-folders and within each sub-folder there are many .js .cs and .php files - about 1000 files. But about 300 are not being used. I need to open up each of the sub-folders and see if any of these files are used/called by any other files. If they are not, I need to store the location of the unused file in a text file.
I did some research and found out that the command grep -r filename *
does just that, but on the command-line I cannot figure out how to loop through the folders and change the filename based on the content inside the folders. The workstation I have is in Windows with Cygwin installed.
发布评论
评论(4)
这将为您提供一个像这样的 csv 文件,其中包含每个文件被引用的次数。
编辑以在 grep 之前删除文件路径
this will give you a csv file like this with the number of times each file is referenced.
edited to remove file path before grepping
这不需要双循环吗? (大 O2)。您必须在每个文件中搜索该文件的每个实例。
我会使用 Perl 而不是 Awk 或 BASH(尽管可以在 BASH 中做到)。
我采取的捷径是仅查找文件的基本名称而不是全名。理论上,我应该从根目录查找它的全名,以及它与文件本身的关系的名称。不过,我现在懒得这么做。最有可能的是,您不必担心这一点。
Doesn't this require a double loop? (Big O2). You have to search each file for every instance of the file in it.
I'd use Perl instead of Awk or BASH (although it is possible to do in BASH).
I'm taking a shortcut of looking just for the file's basename and not the full name. In theory, I should be looking for both its full name from the root, and its name in relationship to the file itself. However, I'm too lazy to do that right now. Most likely, you don't have to worry about that.
唷,棘手。至少如果你必须考虑“被使用”这一点。
对于 .cs,您可以使用导入语句,但这些语句不会轻易让您断定文件是否正在使用。导入可能在包级别上工作,除非我弄错了(更像是一个java人......)。
我认为 JavaScript 和 php 文件的情况会变得更糟。
也许您应该问,为什么该报告首先有价值?
phew, tricky. At least if you have to take into consideration the 'being used' bit.
In the case of .cs, you can have import statements that won't easily allow you to conclude whether a file is in use. The import might work on a package-level, unless I'm mistaken (being more of a java guy...).
And I assume it gets worse for JavaScript and php files.
Maybe you should ask, why that report is valuable in the first place?
这只是一个草稿,你需要研究所有命令并做你自己的逻辑......
This is only a draft, you need research about all commands and do your own logic...