将大量文件分发到较小组的脚本
我的文件夹包含大量不同大小的文件(例如 1000 个以上),我想将它们移至较小的组中,例如每个文件夹 100 个文件。
我编写了一个 Apple 脚本,该脚本对文件进行计数,创建一个编号的子文件夹,然后将 100 个文件移动到新文件夹(可以指定文件数量),该脚本循环直到移动到的文件数量少于指定数量它创建的最后一个文件夹。
问题是它运行速度慢得可怕。我正在寻找可以在我的 MacBook 和/或 Linux 机器上运行的 Apple 脚本或 shell 脚本,这将有效地将文件移动到更小的组中。
文件如何分组并不是特别重要,我只是希望每个文件夹中的文件更少。
I have folders containing large numbers of files (e.g. 1000+) of various sizes which I want to move in to smaller groups of, say, 100 files per folder.
I wrote an Apple Script which counted the files, created a numbered subfolder, and then moved 100 files in to the new folder (the number of files could be specified) which looped until there were less than specified number of files which it moved in to the last folder it created.
The problem was that it ran horrendously slowly. I'm looking for either an Apple Script or shell script I can run on my MacBook and/or Linux box which will efficiently move the files in to smaller groups.
How the files are grouped is not particularly significant, I just want fewer files in each folder.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这应该可以帮助您开始:
嵌套的 if 语句获取最后剩余的文件。修改供您使用后,您可以根据需要添加一些额外的检查。
This should get you started:
The nested if statement gets the last remaining files. You can add some additional checks as you see needed after you modify for your use.
这是一个巨大的混乱,但它不应该太慢:
它完全不区分哪些文件去哪里。
This is a tremendous kludge, but it shouldn't be too terribly slow:
It's completely indiscriminate as to which files go where.
解决目录中文件过多问题的最常见方法是按名称的前几个字符进行细分。例如:
之前:
之后:
如果细分得不够好,则更进一步:
这应该可以很快完成,因为脚本执行的移动命令可以使用简单的 glob 扩展来选择要移动的所有文件,ala
mv aa* a/aa
,而不是必须在每个文件上单独运行移动命令(这将是我对原始脚本为何缓慢的第一个猜测)The most common way to solve the problem of directories with too many files in them is to subdivide by the the first couple characters of the name. For example:
Before:
After:
If that isn't subdividing well enough, then go one step further:
This should work quite quickly, because the move command that your script executes can use simple glob expansion to select all the files to move, ala
mv aa* a/aa
, as opposed to having to individually run a move command on each file (which would be my first guess as to why the original script was slow)