0

I am trying to copy only the folders that are within a directory, the script provided in the answer to this question copies all content within the directory: How can i only copy folders with robocopy in powershell?

The code from this answer is provided below:

Get-ChildItem 'C:\temp\test' |
ForEach-Object {
    $newname = ($_.BaseName -Replace '[^\x20-\x5D,\x60-\x7E]+', '-')
    if (($_.GetType()).Name -eq "DirectoryInfo"){
        write-host "folder"
    }
    write-host $_.BaseName
    write-host $newname
    robocopy.exe "C:\temp\test\$($_.BaseName)" "C:\temp\test\copy\$newname"
}
3
  • robocopy /CREATE, or xcopy /T? Commented Sep 11, 2019 at 13:35
  • so I can replace robocopy.exe with one of these you mean? Commented Sep 11, 2019 at 13:38
  • I meant that you take a look at the /CREATE option of robocopy (which is likely unsuitable since it also creates zero-length files), or at the xcopy command and its /T option (maybe together with /E), which is intended to create the destination directory tree without copying any files... Commented Sep 11, 2019 at 15:14

2 Answers 2

3

You are looking to use the parameters: /E = copy directories even if empty /XF . = Exclude, in this case all files, ie anything with "." in the filename, eg. all files

Example of use:

    $Source = "C:\temp\test"
    $Destination = "C:\temp\test2"
    $robocopyOptions = "/E /XF *.*"
    # filelist is required but we will ignore it anyway with parameters we are passing
    $fileList = ''
    robocopy.exe $Source $Destination $fileList $robocopyOptions.split(' ')
Sign up to request clarification or add additional context in comments.

Comments

1

As an alternative to robocopy you might also use the xcopy command, which features an option /T to create the destination directory tree without copying any files. Here is an excerpt of the help text when you enter xcopy /? into a command prompt window:

  /T           Creates directory structure, but does not copy files. Does not
               include empty directories or subdirectories. /T /E includes
               empty directories and subdirectories.

So you could use the following command line:

xcopy.exe /T /E /I "C:\temp\test\folder" "C:\temp\test\copy"

1 Comment

yeah great they both work fine but robocopy.exe is slightly faster

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.