Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Copy a directory tree to a single directory at a command line

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.

like image 574
Zachary Scott Avatar asked Mar 01 '23 18:03

Zachary Scott


1 Answers

In a Windows command line you can do this:

for /R A %i IN (*.jpg) DO xcopy %i B /M /Y

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:

for /R "A" %%i IN (*.jpg) DO xcopy "%%i" "B" /M /Y

If you are putting this inside a .bat or .cmd file you will need to double the percentage like this.

for /R A %%i IN (*.jpg) DO xcopy %%i B /M /Y

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.

like image 104
Martin Brown Avatar answered Mar 03 '23 08:03

Martin Brown