Thursday, March 7, 2013

make mine a double

using the copy command

when managing a windows server i sometimes find myself needing to copy many files. for the sake of efficiency i head for the command line. the copy command can be used to quickly make a copy of a file:

C:\>copy q1report.txt q2report.txt

this copy command takes two arguments. the first is the file to copy, the second is the name of the new file to create. when you just want to make a copy of a file in a new location, use the destination directory as the second argument:

C:\>copy q1report.txt reports

this will make a copy of q1report.txt in the c:\reports directory. you can copy multiple files at once using wildcards:

C:\>copy q*report.txt reports

retaining permissions with xcopy

when you copy a file only the contents are duplicated. a file's permissions don't copy with it, but the duplicate inherits the permissions of the directory where it gets created. if you want to copy both the file and its access control list, you'll need to use xcopy:

C:\>xcopy /o q1report.txt reports

xcopy can also be used to copy whole directory trees:

C:\>xcopy /s reports d:\

this command will copy the entire directory structure under c:\reports to drive d.

concatenating files with copy

when you need to combine several files into one the copy command can be used for concatenation. just join all the old filenames together with plus symbols and specify the new filename.

C:\>copy q1report.txt+q2report.txt+q3report.txt+q4report.txt annual_report.txt

this works great for combining ascii text files together, but it's not generally very useful with binary files like those created by a word processor.

copy as a text editor

the copy command can also be used as a quick-and-dirty plain text editor. start with the command copy con filename and then type the lines you want in your file. when you're done, type ctrl-z.

C:\>copy con ls.bat
@echo off
dir
^Z

now your batch file is saved and ready to rock 'n' roll.

for more advanced features, check out the command line help for copy and xcopy, and, as always, be seeing you.

No comments:

Post a Comment