在命令行将目录树复制到单个目录
任何人都知道命令行实用程序(或可以作为命令行运行的实用程序)将目录树中的所有 .jpg 文件收集到单个文件夹,仅复制更改的文件?
我从 Renamer 开始,它非常适合重命名当前目录中的文件,但当我试图破坏这条路时却失败了。 这可能是因为我不太了解 Renamer。 我最终创建了一个文本文件目录转储,然后使用 REGEX 查找/替换来创建批处理文件,但这既不高效也不自动化。
正则表达式:
(G:\DIR\DIR\)([0-9]+\)([0-9]+\)([0-9]+\)([0-9]+\)(p [0-9]+.jpg)
更改
将此G:\DIR\DIR\00\00\00\00\p0000000000.jpg
为此
G:\DIR\DIR\p0000000000.jpg
(复制 \1\2\3\ 4\5\6 \1\6) 在批处理文件中。
我需要将整个事情作为计划任务运行,而无需真人登录。并不是真的在寻找 Zip 文件,因为我不想打扰系统处理器,而且大多数文件不会每天都发生变化。 这更多的是文件同步。
Anyone know of a command line utility (or one that can run as a command line) that will collect all the .jpg files in a directory tree to a single folder, only copying files that change?
I started with Renamer, which is great for renaming files in their current directories, but fell short when I tried to mangle the path. This is probably because I don't know Renamer that well. I ended up creating a text file directory dump, then using a REGEX find / replace to create a batch file, but this is hardly efficient nor automated.
The REGEX:
(G:\DIR\DIR\)([0-9]+\)([0-9]+\)([0-9]+\)([0-9]+\)(p[0-9]+.jpg)
changed this
G:\DIR\DIR\00\00\00\00\p0000000000.jpg
to this
G:\DIR\DIR\p0000000000.jpg
(copy \1\2\3\4\5\6 \1\6) in the batch file.
I need to run the whole thing as a scheduled task without a real person logging in. Not really looking for a Zip file because I don't want to disturb the system processor, plus most of the files will not change from day to day. This is more of a file sync.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
在 Windows 命令行中,您可以执行以下操作:
其中 A 是源目录,B 是目标目录。 您需要启用命令扩展,我认为这是默认设置。
评论中的一些注释:
如果您的任何路径中可能有空格,您将需要在第二个 %i 周围添加引号。 这可以防止 xcopy 命令将字符串解释为两个单独的参数。 您可能需要对 A 和 B 路径执行相同的操作。 像这样:
如果您将其放入 .bat 或 .cmd 文件中,您将需要像这样将百分比加倍。
xcopy 上的 /M 选项将仅复制设置了存档位的文件,然后取消设置该位。 这可以防止文件被复制两次。 如果您有其他进程也会更改此位,则可能会导致问题。 还可以使用 /D 选项,该选项将文件的上次修改时间与目标中的上次修改时间进行比较,并仅复制较新的文件。
In a Windows command line you can do this:
Where A is the source directory and B is the destination directory. You need to have command extensions enabled, which I believe is the default.
A couple of notes from the comments:
If any of your paths could have spaces in you will need to add quotes around the second %i. This prevents the string being interpreted by the xcopy command as two separate parameters. You may need to do the same around A and B paths. Like this:
If you are putting this inside a .bat or .cmd file you will need to double the percentage like this.
The /M option on xcopy will only copy files with the Archive bit set and then unset this bit. This prevents the files being copied twice. If you have other processes that also alter this bit it may cause issues. It is also possible to use the /D option which compares the file's last modified time with that in the destination and only copies newer files.
从路径格式来看,我猜你是在 Windows 上。
我还没有阅读全文,但是 http://www.infionline .net/~wtnewton/batch/batguide.html#6a 可能对您有帮助。
同一页面有dizzy.bat,(http://www.infionline.net/ ~wtnewton/batch/dizzy.bat),编辑它来做你想做的事情应该很简单。
I'm guessing you're on Windows from the path format.
I've not read the whole thing, but http://www.infionline.net/~wtnewton/batch/batguide.html#6a might help you.
The same page has dizzy.bat, (http://www.infionline.net/~wtnewton/batch/dizzy.bat) which should be trivial to edit to do what you want.
在 Unix 环境中,我会使用
find
或rsync
(也许还有 shell 的一些功能)。 Cygwin 和 MinGW 带有find
,也许带有rsync
。 您还可以在某个地方获得适用于 Windows 的find
独立端口。如果
SOURCE
shell 变量是包含要复制文件的子目录的目录,而DEST
shell 变量是将它们复制到的目录:find
本质上是递归的。 “-name \*.jpg
”选择与该模式匹配的文件。 您可以使用 -and 添加附加条件。cp
命令的--update
选项(或 -u)仅在更改或尚未复制时才复制文件。cp
还有其他可能也有用的选项。如果 $SOURCE 与 DIR/DIR/ 示例中的 $DEST 相同,则 find 还将找到目标文件(已复制),尽管这样没问题,cp 会识别出您正在尝试将同一文件复制到本身并跳过它,但如果您想避免浪费工作,您可以使用“for”和“if”(或其他内容)仅在 DIR/DIR/ 的子目录上运行 find。
您还可以使用 rsync,它具有可以从目标目录删除文件(如果文件也已从源目录中删除)的选项,以及许多其他此类变体。
In a Unix environment I would use
find
orrsync
(and maybe some features of the shell). Cygwin and MinGW come withfind
, maybe withrsync
. You can also probably get a standalone port offind
for Windows somewhere.If the
SOURCE
shell variable is the directory containing subdirectories with files to copy, and theDEST
shell variable is the directory to copy them to:find
is by nature recursive. "-name \*.jpg
" selects files that match that pattern. You can add additional conditions with -and. The--update
option to thecp
command (or -u) only bothers copying the file if changed or not yet copied. There are other options tocp
that might be useful too.If $SOURCE is the same as $DEST as in your DIR/DIR/ example, then find will also find the destination files (already copied), though this will be ok, cp will recognize that you are trying to copy the same file to itself and skip it, but if you want to avoid that wasted work you can use 'for' and 'if' (or something) to only run find on the subdirectories of DIR/DIR/.
You can also use rsync, which has options that can delete files from the destination directory if they have also been deleted from the source directory, and many other such variations.