I use this executable to copy large numbers of files from [location A] to [location B]. It checks timestamps, and if the
same file exists in both places, it copies the file only if the file at [location A] is newer than the one at [location B].
The main use of cpin is to make sure two directories contain the same information. (The timestamp check helps it avoid
redundantly copying files.) Cpin can be used with wildcard characters, and can run recursively.
I use cpin to to synchronize directories between by desktop computer and my laptop, including synchronizing my music
collection on both computers. It can also be used to copy files for backup. To use it for backups, write a script that
collects files that you want backed up (for example, web browser bookmarks, your photos and documents, etc) and place copies
into a backup location (e.g. a backup drive), or into a single directory which gets backed-up.
As a developer, I also use this utility in my builds to copy images and sounds from the 'source' directory to the 'build'
directory. Developers can use this as a post-build task for the compiler. It is extremely fast because it only copies files
when they change.
This utility cannot be used to backup an entire hard-drive. I think it has problems copying files that are currently open,
and it fails when that happens.
Usage:
cpin [-frtvc] source target
source can be a file, wildcard, or directory.
target can be a file or directory.
"r" (recursive)
Normally, cpin will not recurse into subdirectories. If the "r" flag is used, cpin will copy all
subdirectories.
"v" (verbose)
Output (to stdout) the name of each file as it is copied.
"t" (test)
If the "t" flag is used, cpin will not copy any files. If used with the "v" flag, it will output
a list of files which would've been copied/overwritten.
"c" (no complain)
Normally, if you tell cpin to copy a source file which does not exist, it will complain. If you
don't want cpin to complain when the source file does not exist, use the "c" flag.
"f" (force)
Don't check the timestamps. Instead, always overwrite the target with the source.
Notes:
* cpin will automatically expand environment variables (e.g. %HOME%).
* cpin will automatically create parent directories in the target (e.g. if target is "c:\test1\test2\test3\", but "c:\test1" doesn't exist, then it will automatically create "c:\test1\" and "c:\test1\test2\").
* Quotation marks can be used to resolve ambiguities in filenames. For example, if a directory contains spaces, then quotes should be used: [cpin "c:\My Stuff\*.*" c:\BackupDirectory\]
Examples:
Copy all files in [ c:\MyDirectory ] to [ d:\OtherDirectory\ ], and do not recurse:
cpin c:\MyDirectory\*.* d:\OtherDirectory\
Copy jpg files in [ c:\MyDirectory ] to [ d:\OtherDirectory\ ], and do not recurse:
cpin c:\MyDirectory\*.jpg d:\OtherDirectory\
Copy all files in [ c:\MyDirectory ] to [ d:\OtherDirectory\ ], and recurse into subdirectories:
cpin -r c:\MyDirectory\*.* d:\OtherDirectory\
Copy all files in [ c:\MyDirectory ] to [ d:\OtherDirectory\ ], do it recursively, and tell you each file that is being copied:
cpin -rv c:\MyDirectory\*.* d:\OtherDirectory\
Copy all files in [ c:\MyDirectory ] to a directory on another computer [ \\Mylaptop\OtherDirectory\ ], and do it recursively.
(The computer must have access and write permissions on the "Mylaptop" computer before this works. It must be setup beforehand.)
cpin -r c:\MyDirectory\*.* \\Mylaptop\OtherDirectory\
When using cpin in a script, I recommend using the "set" command to simplify the script:
set cpin=c:\Toolkit\cpin
%cpin% -r "C:\SomeDirectory\*.*" "\\Mylaptop\SomeDir\"
To get a list of instructions on using cpin, type "cpin" with no arguments:
cpin
Ambiguities:
There are situations where the desired result is ambiguous.
The cpin command attempts to resolve these situations intelligently.
For example, given this command, what does the cpin command do?
cpin c:\test1\File1.txt c:\test2\test3\test4\test5
* If "test5" exists as a directory, File1.txt will be copied into the "test5" directory.
* If "test5" exists as a file, File1.txt will (if newer) be copied (and renamed) to
"test5" in the "test4" directory.
* If "test5" does not exist, cpin will assume "test5" is a filename. File1.txt will be
copied (and renamed) to "test5" in the "test4" directory.
If the user wants cpin to copy File1.txt into a directory named "test5", but "test5" does not exist,
the user should make sure that the target location is unambiguous. For example:
cpin c:\test1\File1.txt c:\test2\test3\test4\test5\
cpin c:\test1\File1.txt c:\test2\test3\test4\test5\.
If the source contains a wildcard (example: "cpin c:\test1\*.txt c:\test2\test3\test4\test5")
"test5" is assumed to be a directory name.
Potential Problems and Pitfalls:
Users should be aware that cpin can overwrite changes in files under certain conditions. For example, if you are using
this script to synchronize two directories:
cpin -r "c:\sync\*.*" "\\MyLaptop\sync\"
That script will make sure any new or changed files in "c:\sync\" get copied over to "\\Mylaptop\sync\". The potential
pitfall in that script is this: if you change a file inside "\\Mylaptop\sync\", the change won't get copied back to the
"C:\sync\" directory. Later, changing that same file inside "c:\sync\" will cause the script to overwrite the version
on MyLaptop.
A similar problem can occur when you use a script to update both directions. The following script causes changes to be
copied both directions. (If a file in "c:\sync\" gets changed or added, it will be copied over to "\\MyLaptop\sync\". If a
file in "\\MyLaptop\sync\" gets changed or added, it will be copied back to "c:\sync\".)
cpin -r c:\sync\*.* \\MyLaptop\sync\
cpin -r \\MyLaptop\sync\*.* c:\sync\
The pitfall with that script is this: if you change "c:\sync\SomeFile", change "\\MyLaptop\sync\SomeFile", and then
call the script, the newest change will overwrite the earlier change. In this case, the "\\MyLaptop\sync\SomeFile" version
will overwrite the "c:\sync\SomeFile" file. To avoid this, make sure the script gets called in between changing the file in
both places. It's okay to change "c:\sync\SomeFile", call the cpin script, then change "\\MyLaptop\sync\SomeFile".
(A more sophisticated utility could detect that both files have changed, warn the user, and ask for instructions, but cpin
isn't that sophisticated.)
Also, if you are copying files both directions like this, and you want to delete a file, you have to delete the file in both
locations before calling the script - otherwise, it will keep re-copying the file from the other location.
Known Problems:
Occasionally, cpin will copy a file, but the duplicate file will have a size of 0. This is rare, but it does happen occasionally.
The elusiveness of this bug has prevented me from tracking down and fixing the problem.
Restrictions on Use:
1. Don't be evil.
2. Use this executable however you want and change the source code if you want - unless it conflicts with #1.
3. Feel free to share the executable and source code with everyone. Tools in the public domain help everyone.
4. Don't claim you wrote it, or sell it.
This software is provided 'as-is'. If it causes problems or fails to work properly, then don't come after me for damages.
You have the source code, too, which means you can spot any potential problems.
Downloads:
cpin executable
cpin source code (written in C++ with Visual Studio)