24
Windows Scripting Utilities_xcopy Table of Contents xcopy ............................................................................................................................................... 2 xcopy Syntax -1 ............................................................................................................................... 3 xcopy Syntax -2 ............................................................................................................................... 5 xcopy Options -1 ............................................................................................................................. 6 xcopy Options -2 ............................................................................................................................. 9 xcopy Strings ................................................................................................................................. 12 xcopy Exclude................................................................................................................................ 13 xcopy Examples -1......................................................................................................................... 14 xcopy Examples -2......................................................................................................................... 16 xcopy Exit Codes ........................................................................................................................... 21 ROBOCOPY .................................................................................................................................... 22 Notices .......................................................................................................................................... 24 Page 1 of 24

Windows Scripting Utilities xcopy...copy don't go because this script just . stops. So what you can do is you . can put the /c, it'll hit an error, it'll do . whatever it needs to,

  • Upload
    others

  • View
    5

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Windows Scripting Utilities xcopy...copy don't go because this script just . stops. So what you can do is you . can put the /c, it'll hit an error, it'll do . whatever it needs to,

Windows Scripting Utilities_xcopy

Table of Contents

xcopy ............................................................................................................................................... 2

xcopy Syntax -1 ............................................................................................................................... 3

xcopy Syntax -2 ............................................................................................................................... 5

xcopy Options -1 ............................................................................................................................. 6

xcopy Options -2 ............................................................................................................................. 9

xcopy Strings ................................................................................................................................. 12

xcopy Exclude ................................................................................................................................ 13

xcopy Examples -1 ......................................................................................................................... 14

xcopy Examples -2 ......................................................................................................................... 16

xcopy Exit Codes ........................................................................................................................... 21

ROBOCOPY .................................................................................................................................... 22

Notices .......................................................................................................................................... 24

Page 1 of 24

Page 2: Windows Scripting Utilities xcopy...copy don't go because this script just . stops. So what you can do is you . can put the /c, it'll hit an error, it'll do . whatever it needs to,

xcopy

2

xcopy

Extended copy

A command for copying multiple files or entire directories across file systems or networks

Run from the command prompt or in Batch files

Great tool for exfiltration of complex directory structures or entire drives

Allows for very selective copying• Include & exclude• Date modified

Deprecated by robocopy starting with Vista

**002 So one of the tools, xcopy. Short for extended copy. Lets you do some things that the basic copy command there in the windows command line cannot do. You can do multiple files and you can do entire directories, or cross-file systems. And also cross- networks. You run this command from the command line and therefore you can put it in your Batch file. It's a great tool for this says exfiltration, which kind of sounds hacker-ish, but perhaps file transfer might be a better term with complex directory structures or even entire drives. So that's pretty powerful when you can pull the entire drive. You can be pretty selective in what you want or

Page 2 of 24

Page 3: Windows Scripting Utilities xcopy...copy don't go because this script just . stops. So what you can do is you . can put the /c, it'll hit an error, it'll do . whatever it needs to,

what you don't want by doing some including or some excluding. And you can also base this, which is kind of nice if you're doing backups, you can base things on dates when they were last modified. Technically, it's deprecated, but they had it on Windows 7, and Windows 8, I found. So still useful and can be done, can be used, in your scripts in those Windows systems.

xcopy Syntax -1

3

xcopy Syntax -1

xcopy source [destination] [options]source – defines the files or top level folder to copy from

• The only required parameterdestination – specifies location where source file or folders should be copied

• If there is no destination, the folder where xcopy was run will be used

• If destination directory does not exist, will prompt

Use quotes “” if the path contains spaces

**003 So just some basic syntax. The only thing you need that's not optional is a source, wherever you're pulling your files from. The destination and the other options are

Page 3 of 24

Page 4: Windows Scripting Utilities xcopy...copy don't go because this script just . stops. So what you can do is you . can put the /c, it'll hit an error, it'll do . whatever it needs to,

as, that says, optional. The source you just got to let it know where the file's coming from or the top level folder you want to pull from. Destination, got to make sure same thing. Let them know where you want to send it to. So xcopy, if you run it with just the source and no destination, it assumes that you want to copy it into your current folder that you're running xcopy out of. Also, if you do put a destination in and it doesn't exist or it can't find it, it'll actually prompt you for the destination or a different destination. And if your destination has spaces, you'll definitely want to put quotes around it in the path.

Page 4 of 24

Page 5: Windows Scripting Utilities xcopy...copy don't go because this script just . stops. So what you can do is you . can put the /c, it'll hit an error, it'll do . whatever it needs to,

xcopy Syntax -2

4

xcopy Syntax -2

xcopy with no options• Does not copy directories• Does not copy hidden or system files• Removes read-only attribute• If run a second time will prompt for overwrite

• Copies files in the temp directories and places them in c:\

**004 Okay. So you can run, like I said, xcopy with no options, but there are some limitations when you do this. It will not copy directories if you do it with just the xcopy and the source location. And it won't copy any hidden or system files that are in there. So if you're trying to pull something out of, say, a, like, a system 32 or whatever. It will not pull those hidden, or system files, out. And it does remove the read- only attribute on the files that it pulls. And then, well, similar to many other copying programs, if you run it a second time it'll prompt you if you want to overwrite the files that you just copied. So it's just an example

Page 5 of 24

Page 6: Windows Scripting Utilities xcopy...copy don't go because this script just . stops. So what you can do is you . can put the /c, it'll hit an error, it'll do . whatever it needs to,

of pulling the files out of temp and placing them into c:/ or the xcopy is located.

xcopy Options -1

5

xcopy Options -1

[/?] – Display all options

[/w] – Displays “Press any key to begin copying files(s)”

[/c] – Ignore errors*

[/q] – Suppresses the display of xcopy messages*

[/f] – Displays source and destination file names

[/l] – List only – display files that would be copied

[/y] – Suppresses prompting to confirm overwrite

[/k] – Retain read-only attribute on destination files

[/i] – Assume the destination is a folder

**005 Okay. So lots and lots of switches. So I'm not going to sit here and go a, b, c for you. You can look them up. With the /?, a lot of command-line commands use the //Help or the /H or something like that. This particular xcopy uses the /? to get at it. For some reason it kind of barks at you if you do H and you do Help. So you'll want to do the /?. It'll display all of these, not just these. Just a couple that are worthy of mentioning, pointing out in detail,

Page 6 of 24

Page 7: Windows Scripting Utilities xcopy...copy don't go because this script just . stops. So what you can do is you . can put the /c, it'll hit an error, it'll do . whatever it needs to,

is the /c, ignores errors. This is very helpful if you're running something, say, overnight and you're not going to be there and an error happens on your first or your second file and the other hundred files you're trying to copy don't go because this script just stops. So what you can do is you can put the /c, it'll hit an error, it'll do whatever it needs to, and then it'll jump to the next one, so it'll make its copies. The /q suppresses display of the xcopy messages. So it's kind of nice you don't have to sit there and worry about the output coming out and mucking up whatever you're, if you're saving any output or anything like that. And then also this, the /I. If you're sending it someplace, and you're just going to explicitly tell it, it may not be able to determine based on the attributes of that folder or the name that it's a actual folder or a directory, so you can force it, so to speak, and tell it, "This is a folder," and go ahead and copy it in there. Any questions? Oh, go ahead. Student: Why is it that Microsoft can never get copyright? Instructor: I... Student: Problem is, one of them was on error it always fails. Especially when you have a lot of files to do. Instructor: Yeah.

Page 7 of 24

Page 8: Windows Scripting Utilities xcopy...copy don't go because this script just . stops. So what you can do is you . can put the /c, it'll hit an error, it'll do . whatever it needs to,

Student: Okay. So you ignore the errors. So then by now you are faced with two options, delete everything that you did here and just start the whole thing over again, or tell it to copy again. Well, suppose this time you turned on the ignore error, how would you know which ones caused the error that you can go and fix them later, and also how could you make the copy see that these already succeeded, don't copy them over again, just copy the ones that aren't there? Instructor: So in this case-- Student: So why don't we list for, switch for, copy, what's not there yet? Instructor: Okay. So this is not all- inclusive, by any means actually. Student: Oh, there's more?

Page 8 of 24

Page 9: Windows Scripting Utilities xcopy...copy don't go because this script just . stops. So what you can do is you . can put the /c, it'll hit an error, it'll do . whatever it needs to,

xcopy Options -2

6

xcopy Options -2

[/u] – Copy only files that already exist in destination

[/s] – Copy non-empty folders and sub-folders

[/t] – Copy subdirectory structure only, not files

[/e] – Copy folder and sub-folders, including empty

[/r ] – Copy read-only files

[/h] – Copy hidden and system files

[/d[:mm-dd-yyyy] – Copy file changed on or after the specified date –default copies source date/time is newer then destination

[/exclude:filename1[+[filename2]][+[filename3]]

Specify a file containing a list of strings to exclude

[/z] – Copy in restartable mode for network copies

**006 Instructor: We'll be moving on to some that, see, the u up there, /u, copy only files that already exist. So this is kind of a backup. That would be good for backups if you want to make backups of certain files that already exist in the destination folder. They'll go out and it'll go look for it. So you kind of want the reverse of that if you're looking for it, right? Student: Yeah. Instructor: So you can also expressly tell it to copy non-empty folders and subfolders only, so you don't sit there and go out and make

Page 9 of 24

Page 10: Windows Scripting Utilities xcopy...copy don't go because this script just . stops. So what you can do is you . can put the /c, it'll hit an error, it'll do . whatever it needs to,

copies of directories that don't have anything in them. So if you'd like to make the subdirectory structure only, without files, so if you have a, I don't know, a baseline computer that you'd like to have all the, a specific, like, per person, per basic user, if you want to have a temp file and then a share file and then a whatever that structure is, but you don't need to copy every, all the files that you have in yours, or whatever computer you're copying them from. You can use the /t, and it'll copy just the file, the folder structure, if you will, the directory structure in that way, not the files themselves, so... And if you just want everything to include the empties, you can do the /3, and then there's some special situations where if you really do need hidden or system files you got to explicitly tell it with a /h and read-only files, if you're interested in reaching out and taking just those, then you would do the /r. So this /d is the date or a difference, if you will, option that allows you to tell it--with just the d it'll find files that are more current than the ones that are there right now. If you need it to be a specific date, you just have to do the month month-day day-year thing in there and it'll grab based on that date. So that's the inclusive piece. And then there's the exclusive piece where you can tell it to exclude certain files. And it's interesting, you can actually tell it which strings or words, I guess you could say, if you don't want certain documents that

Page 10 of 24

Page 11: Windows Scripting Utilities xcopy...copy don't go because this script just . stops. So what you can do is you . can put the /c, it'll hit an error, it'll do . whatever it needs to,

are, you know, about whatever, a widget and you want only a certain type of document, you can tell it not to copy ones that have widget inside of it. And then this is helpful when you're doing stuff over networks. Although I'm not sure that it works every single time, from some things I've read on the internet. But if you can save it or copy it using, with a restartable mode when you're copying from the network, and that way if you lose a connection or something happens to your script then it stops, whatever, you have an opportunity to restart that particular file.

Page 11 of 24

Page 12: Windows Scripting Utilities xcopy...copy don't go because this script just . stops. So what you can do is you . can put the /c, it'll hit an error, it'll do . whatever it needs to,

xcopy Strings

7

xcopy Strings

When providing filenames to xcopy wildcard characters ? and * can be used to tailor the files to copy

• Use ? to replace a characterxcopy “c:\temp\password.d??” d:\

– This would copy files named password.doc orpassword.dat to d:\

• Use * to replace any number of charactersxcopy “c:\temp\password*.*” d:\

– This would copy files like passwords.doc, password.txt, passwords.xls

**007 Okay. So xcopy lets you use wildcards when you're copying things explicitly. If you know that, in this case, it uses the question mark and the asterisk symbols as wildcards, the question mark can be used in lieu of a single character. So if you know that usually extensions are two, three, total spaces. So if you know that you want the password.d and then two other spaces, so that way you can get the passtword.dat or the password.doc or a dot or some other iteration difference there. So you can use that for two. And then the asterisk symbol actually is any number of characters after password. So it could be passwords, password

Page 12 of 24

Page 13: Windows Scripting Utilities xcopy...copy don't go because this script just . stops. So what you can do is you . can put the /c, it'll hit an error, it'll do . whatever it needs to,

listing, and then it could be any extension, so you go ahead and use the asterisk there and then you copy it to the destination d root folder. So as you can see, it'll grab the .doc, the txt or the xls or any other

xcopy Exclude

8

xcopy Exclude

/exclude:filename1[+[filename2]][+[filename3]]Each filename contains a list of strings to exclude.

copy “c:\test\*” “d:\” /i /e /exclude:c:\excludelist.txt

• Copy all the files and directories in c:\test, including empty ones to d:\– [/e]

• Assume the destination is a directory – [/i]• Exclude all the files under the directory temp and any files with the

extenstion .bak – [/exclude:]

**008 Alrighty. So here's an example of using the exclude. So the exclude list is the actual what strings or what locations that the xcopy will, which apparently the x got removed out of this particular slide. I did not see that. But so it will not copy from the temp folder and it won't copy anything that has an extension of .bak when you run this. So this

Page 13 of 24

Page 14: Windows Scripting Utilities xcopy...copy don't go because this script just . stops. So what you can do is you . can put the /c, it'll hit an error, it'll do . whatever it needs to,

particular copy line is pulling from c, or I'm sorry, c:test, and anything after. So any subfolders based in there, it will copy it to the d drive at the root level. The /i is assume. It's telling it explicitly, assume this is a folder or a directory. The /e tells you, including empties, please grab all the directories and subdirectories, including empty folders as well, and then but exclude these items that are located here. And that's the /temp and the avoid stuff out of the folder temp and avoid the .baks.

xcopy Examples -1

9

xcopy Examples -1

xcopy “c:\pass*.*” “d:\export” /s /i /c > d:\exportpass.txt

• Copy all the files on the c: drive that start with pass, regardless of extension to d:\export

• Copy directories and sub-directories, excluding empty – [/s]• Assume the destination is a directory – [/i]• Ignore errors – [/c]• Export a list of files copied to a text file named d:\exportpass.txt – [>]

**009 Okay. So this example copies all the files on the c: drive that have,

Page 14 of 24

Page 15: Windows Scripting Utilities xcopy...copy don't go because this script just . stops. So what you can do is you . can put the /c, it'll hit an error, it'll do . whatever it needs to,

start with, pass. And regardless of the extension. And then it dumps it to d:\export. Copies the directories and subdirectories except with a /s. It actually ignores all the empties, so it won't grab that. A /i, once again, says, yes, you can trust that \export is a folder, not a file. And /c is that ever-important, if you do encounter any errors, go ahead and skip over those and we'll deal with that later. And then you export a list. The redirection, way over there that you may not have seen. The redirection puts a list of files, copy it into a text file named exportpass.txe, txt, excuse me. Any questions? So these are pretty straightforward.

Page 15 of 24

Page 16: Windows Scripting Utilities xcopy...copy don't go because this script just . stops. So what you can do is you . can put the /c, it'll hit an error, it'll do . whatever it needs to,

xcopy Examples -2

10

xcopy Examples -2

xcopy “c:\My Files” “\\server\backup” /c /d /e /h /i /q /r /s /y

• Copy all the files and folders from c:\My Files to the network share \\server\backup - [/s]

• Ignore errors – [/c]• Copy newer files then already copied – [/d]• Include empty folders – [/e]• Include hidden files – [/h]• Assume the destination is a directory – [/i]• Suppress output – [/q]• Copy read-only files – [/r]• Do not prompt to overwrite files – [/y]

**010 So this one here gets a little crazy with the number of flags that you use. So it's xcopy. It's taking it out of the My Files on the c: drive and it's sending it to a server, a backup drive. And the /s is the, that's the without empties; is that correct? And then the /c is the ignore errors; /d is the--you're looking for files that are newer than the copy, the files already there. Student: Is that the answer to the question I asked? Instructor: Well--

Page 16 of 24

Page 17: Windows Scripting Utilities xcopy...copy don't go because this script just . stops. So what you can do is you . can put the /c, it'll hit an error, it'll do . whatever it needs to,

Student: Copy whatever's not already there? Instructor: Oh, no, it's just newer. So you could have a john.txt but if it's newer, the one you're going to copy is newer, it will overwrite your current file. So that's not saying that files that are already in there. It's actually going to look for a file that is, the name is matching from where you copy. Student: And it won't copy anything that's already there if the dates match. Instructor: If the dates match. That's correct. Student: Okay. And it'll copy anything that's not there, because... So I think that might be my answer. So with the /e we are including the empty folders and this particular time we're looking to keep the hidden files, excuse me. And then once again we're assuming the destination is a directory, and then we're using the /q to suppress any output. And wow. They're doing all kinds of things. They not only want hidden files but they want the read-only files. And another thing is because we're copying over things, you can use a /y. Normally when you're overwriting, it asks you, "Are you sure you want to copy over this, overwrite this?" You can explicitly say on any time you ask me if it's okay to overwrite this, I want to go ahead and say "yes." So you can do

Page 17 of 24

Page 18: Windows Scripting Utilities xcopy...copy don't go because this script just . stops. So what you can do is you . can put the /c, it'll hit an error, it'll do . whatever it needs to,

the /y to skip over that and that way you don't necessarily have to be there to click "yes" or whatever. Yes, yeah. Student: What's so special about copy, read-only files? Why would that be a special switch? Instructor: I think only--so if you have protected documents like maybe templates or certain documents that you use on a regular basis and you don't want the regular user population to change it and so you make it read-only and you want to copy just those, I think that's about the only time I can think of that you would be needing it, separately, anyway, to copy just, say, read-only marked documents, I think. John? Student: If you're copying a folder, you might want to do it just to sync a couple directories. And if the thing never changed, why bother copying them? So you don't hit that switch and they won't be copied, just the ones that were changed. Instructor: Or... Student: If you have, you're trying to sync two folders, and you have read-only files in there. There's no point in copying them. You want to copy the ones that aren't read-only that could've changed. Read-onlys never change. Instructor: Oh. So well, this is specifically two copy read-onlys.

Page 18 of 24

Page 19: Windows Scripting Utilities xcopy...copy don't go because this script just . stops. So what you can do is you . can put the /c, it'll hit an error, it'll do . whatever it needs to,

Student: And if you don't do the slash you don't get the read-onlys. Instructor: Right. Well, no. He was asking, "Why would you? What purpose would you have to even use this?" is kind of what he was getting at. So I was thinking, well, if you happen to have some documents and in the big list of files that you have in that folder you know there's, like, 10 of them that are read-only, they're marked so people don't adjust them, then maybe you just want to pull those out. And instead of trying to figure out what the names are, you can just go, "I know they're all read- only. I can do a /r and copy them all." So Chris, you have? Instructor 2: You might find very, or this very similar, to rsync on the Linux side. And in fact, robocopy, which replaced xcopy, is even moreso. So if you look at the parameter list between the two programs, I'm willing to bet that you're going to see a significant amount of overlap between the two. So this, not a direct port of rsync but maybe a clone or a, you know, attempted copy of the functionality that rsync provides just on the Windows side. And so that's why I think you see some of the more strange or obtuse command-line parameters here that you may wonder like, "Why is that here?" Well, probably because if you look at the rsync options it's available there. Instructor: Try to match--

Page 19 of 24

Page 20: Windows Scripting Utilities xcopy...copy don't go because this script just . stops. So what you can do is you . can put the /c, it'll hit an error, it'll do . whatever it needs to,

Student: Yeah. Instructor: --capability. Student: When you get into the situation where you need a really odd selection of parameters, you can be very happy that they're there. Student: It's that if the selector isn't there, like, say you have a situation where you have to copy just the read-only files from a directory structure, maybe that directory structure is 50 folders deep. Do you want to have to go through manually and find all the read-only files and then find a way to pull them all and keep the structure? Instructor: No. I would be very happy for xcopy right about then. Student: I do a lot of rsync work, and you'd be surprised at the odd selection of flags I've had to use sometimes. Instructor: Definitely helpful when you need it most, so... Any other questions? Okay.

Page 20 of 24

Page 21: Windows Scripting Utilities xcopy...copy don't go because this script just . stops. So what you can do is you . can put the /c, it'll hit an error, it'll do . whatever it needs to,

xcopy Exit Codes

11

xcopy Exit Codes

Useful for user feedback batch file using if statements

*http://www.microsoft.com/resources/documentation/windows/xp/all/proddocs/en-us/xcopy.mspx?mfr=true

Exit code Description0 Files were copied without error.1 No files were found to copy.2 The user pressed CTRL+C to terminate xcopy.

4 Initialization error occurred. There is not enough memory or disk space, or you entered an invalid drive name or invalid syntax on the command line.

5 Disk write error occurred.

**011 So here are the exit codes or the error codes, if you will, for xcopy, when it runs properly. Gives you a 0. When no files for copy, it's, oh, found, were found to copy, gives you a 1. And then user--this is an interesting one. I guess if it was manually shut down with a CTRL+C input, it'll actually give you a different error code, separately. And then probably the most common one, 4, where it's like initialization issues, not enough memory, disk space. It entered an invalid drive name, invalid syntax on the command line. And then there's a disk overwrite error for a number 5. So it's kind of strange. If I remember right, this one doesn't

Page 21 of 24

Page 22: Windows Scripting Utilities xcopy...copy don't go because this script just . stops. So what you can do is you . can put the /c, it'll hit an error, it'll do . whatever it needs to,

do exactly this when I tried it before, so another instance of, you know, as you get ready to write your scripts and you're trying to take advantage of these exit codes, please make sure you do your trial and see that the error code shows up the way you'd like it to be.

ROBOCOPY

12

ROBOCOPY

Robust COPYAdds numerious features above XCOPY – included starting with VISTA

• More tolerant of network outages and resuming• Copy ALL attributes of files, including NTFS ACLs• “mirror” mode to keep trees in sync• Copy a larger number of files that would usually cause XCOPY

to crash• Progress indicator on the command line

**012 Okay. And as Chris alluded to, since xcopy is technically deprecated, robocopy or robust copy, I guess that's what it's supposed to be short for, added quite a few features to xcopy. And then I guess it started in the Vista timeframe. Little bit more tolerant of network

Page 22 of 24

Page 23: Windows Scripting Utilities xcopy...copy don't go because this script just . stops. So what you can do is you . can put the /c, it'll hit an error, it'll do . whatever it needs to,

outages. So maybe you don't have to explicitly put that /z in. It's probably able to resume. Copy all attributes of files, including NTFS access control list information. Which is interesting that it would go that far, but that's, that's good stuff. And there's a mirror mode to keep trees in sync. So I guess that's directory trees so it can match up, instead of having to do explicit backup operations, you can set up a mirror and it'll automatically copy everything over from one to another. Or maybe one to many. And then it can actually do even more, copy more files than xcopy can. So copy would crash with a few and then xcopy would be a little bit more tolerant to crashing, and then robocopy being robust, can handle even more, so... And then this also gives you a progress indicator which is surprising on a command line, so that's good. Let's you know how far along it's running.

Page 23 of 24

Page 24: Windows Scripting Utilities xcopy...copy don't go because this script just . stops. So what you can do is you . can put the /c, it'll hit an error, it'll do . whatever it needs to,

Notices

6

Notices

© 2015 Carnegie Mellon University

This material is distributed by the Software Engineering Institute (SEI) only to course attendees for their own individual study.

Except for the U.S. government purposes described below, this material SHALL NOT be reproduced or used in any other manner without requesting formal permission from the Software Engineering Institute at [email protected].

This material was created in the performance of Federal Government Contract Number FA8721-05-C-0003 with Carnegie Mellon University for the operation of the Software Engineering Institute, a federally funded research and development center. The U.S. government's rights to use, modify, reproduce, release, perform, display, or disclose this material are restricted by the Rights in Technical Data-Noncommercial Items clauses (DFAR 252-227.7013 and DFAR 252-227.7013 Alternate I) contained in the above identified contract. Any reproduction of this material or portions thereof marked with this legend must also reproduce the disclaimers contained on this slide.

Although the rights granted by contract do not require course attendance to use this material for U.S. government purposes, the SEI recommends attendance to ensure proper understanding.

THE MATERIAL IS PROVIDED ON AN “AS IS” BASIS, AND CARNEGIE MELLON DISCLAIMS ANY AND ALL WARRANTIES, IMPLIED OR OTHERWISE (INCLUDING, BUT NOT LIMITED TO, WARRANTY OF FITNESS FOR A PARTICULAR PURPOSE, RESULTS OBTAINED FROM USE OF THE MATERIAL, MERCHANTABILITY, AND/OR NON-INFRINGEMENT).

CERT ® is a registered mark owned by Carnegie Mellon University.

Page 24 of 24