添加链接
link之家
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接
相关文章推荐
焦虑的皮带  ·  MySQL:UNION、UNION ...·  6 月前    · 
行走的楼房  ·  GenericXmlApplicationC ...·  1 年前    · 

I need to copy all of my c:\inetpub directory to a new location but exclude the following folders and their subfolders:

c:\inetpub\custerr
c:\inetpub\history
c:\inetpub\logs
c:\inetpub\temp
c:\inetpub\wwwroot

So far I am doing this:

# Directory name is created with a format string
$dirName = "\\servername\folder1 _ {0}\inetpub" -f (get-date).ToString("yyyy-MM-dd-hh-mm-ss")
$dirName # Check the output
# Create dir if needed
if(-not (test-path $dirName)) {
    md $dirName | out-null
} else {
    write-host "$dirName already exists!"
#Copy Backup File to Dir
Copy-Item "\\servername\c$\inetpub\*" $dirName -recurse
                Much better!  I despise posting on this site it's always given me problems with formatting!
                    – MJJM
                Aug 19 '15 at 14:59

This is a simple example of something you could do. Build an array of the parent folders that you want to exclude. Since you are accessing them via UNC paths we cannot really use the c:\ path (We can get around this but what I am about to show should be good enough.).

Then use Get-ChildItem to get all the folders in the inetpub directory. Filter out the exclusions using -notin and pass the rest to Copy-Item

$excludes = "custerr","history","logs","temp","wwwroot"
Get-ChildItem "c:\temp\test" -Directory | 
    Where-Object{$_.Name -notin $excludes} | 
    Copy-Item -Destination $dirName -Recurse -Force

You need at least PowerShell 3.0 for this to work.

I've got an error, my PS major version is 3. copy-Item : The input object cannot be bound to any parameters for the command either because the command does not take pipeline input or the input and its properties do not match any of the parameters that take pipeline input. At line:15 char:93 + ... n $excludes} | Copy-Item $dirName -recurse + ~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : InvalidArgument: (Webservices:PSObject) [Copy-Item], ParameterBindingException + FullyQualifiedErrorId : InputObjectNotBound,Microsoft.PowerShell.Commands.CopyItemCommand – MJJM Aug 20 '15 at 7:19 Not sure why I didn't fix this before. I had ommitted the parameter name -Destination. Without it the binding was messed up. It is fix now if it matters. @MJJM – Matt Sep 13 '16 at 16:45 Warning: If $dirName does not yet exist, it will be created, but the first subdirectory from the source won't be copied. Instead, its contents will be copied directly to $dirName, eliminating one level of depth. As this creates $dirName, the second subdirectory will be copied as intended. – MSalters Apr 5 at 15:00
Copy-Item -Path (Get-Item -Path "$path\*" -Exclude ('Folder1', 'File.cmd', 'File.exe', 'Folder2')).FullName -Destination $destination -Recurse -Force

Replace:

$path by your source folder

('Folder1', 'File.cmd', 'File.exe', 'Folder2') by your specific files/folder to exclude

$destination by your destination folder

This looks great! Clean and simple. However, the excludes didnt seem to apply to the subfolders. – MikeD Aug 30 at 18:52

after you get a hold of all your folders to filter them down.

This example would exclude anything containing "old" in the name. You can do this for directory you wish to exclude.

A full example:

C:\Example*" -include "*.txt -Recurse |
  ?{$_.fullname -notmatch '\\old\\'}|
    % {Copy-Item $_.fullname "C:\Destination\"}

For multiple excludes you can use -And :

C:\Example*" -include "*.txt -Recurse |
  ?{$_.fullname -notmatch '\\old\\' -And $_.fullname -notmatch '\\old2\\'}|
    % {Copy-Item $_.fullname "C:\Destination\"}
                What if one of the wanted sub folders contained that name? Also you should show how you would account for the multiple folders the OP is looking for.
                    – Matt
                Aug 19 '15 at 14:54
                I am starting to wonder if it's worth just running the copy and then a separate delete command after to delete \\server\foldername\logs as it seems quite a task to do something that I thought would be relatively easy
                    – MJJM
                Aug 19 '15 at 15:04
        

Thanks for contributing an answer to Stack Overflow!

  • Please be sure to answer the question. Provide details and share your research!

But avoid

  • Asking for help, clarification, or responding to other answers.
  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.

site design / logo © 2019 Stack Exchange Inc; user contributions licensed under cc by-sa 4.0 with attribution required. rev 2019.9.17.34929