Windows Scripting Utilities Xcopy
Total Page:16
File Type:pdf, Size:1020Kb
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 xcopy 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 2 **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 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 xcopy Syntax -1 xcopy source [destination] [options] source – defines the files or top level folder to copy from • The only required parameter destination – 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 3 **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 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 xcopy Syntax -2 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:\ 4 **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 of pulling the files out of temp and placing them into c:/ or the xcopy is located. xcopy Options -1 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 5 **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 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 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 xcopy Options -2 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 6 **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 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..