Shintaro Takechi
Shintaro Takechi

Reputation: 1365

Robocopy everything in subdirectory excluding the root files

How do I use robocopy so the root contents are not copied? I already have root files stored elsewhere and I just would like to copy the sub directories and their contents while the source folder still containing root directory contents.

Upvotes: 1

Views: 2534

Answers (2)

SSN669
SSN669

Reputation: 1

I don't have a reputation but the answer Mr. Hinkle gave solved 2 days of effort and searching. My challenge was moving the files that were > 1hour of age. This combination of powershell and robocopy appears to work. Below is my final code.

# Clear screen
cls
# Disconnect the drive if it exist - don't know where it is pointing to
If (Test-path p:) {
net use p: /delete
}
#Map the destination
net use p: \\server\dir1\dir2\dir3 password /USER:domain\user /P:no

get-childitem -path 'D:\dir1\dir2\' |
    where-object {$_.LastWriteTime -lt (get-date).Addhours(-1)} |
    ?{$_.PsIsContainer} |
    %{robocopy $_.FullName p:\$($_.Name) /S /MOVE /r:3 /W:1}

net use p: /delete

Upvotes: 0

Tony Hinkle
Tony Hinkle

Reputation: 4742

This is not possible with native robocopy switches as far as I can tell. You will need to use a script to enumerate the subdirectories and run robocopy against them.

Here is a sample PowerShell command that will accomplish what you want, copying everything from C:\temp\source\ to c:\temp\target\, excluding the files that are in c:\temp\source:

get-childitem c:\temp\source\* |?{$_.PsIsContainer} | %{robocopy $_.FullName c:\temp\target\$($_.Name) /S}

Credit to powershell ignore files in root but robocopy folders and their contents for the basics of this.

Upvotes: 1

Related Questions