Copying source files and retaining folder structures including parent folders (but not files) - powershell

Say I have the following source and target.
source: c:\temp\folder1\folder2
target: e:\backup
I want to have an output as shown below. As you can see, it should only copy the files and its subdirectories from the above source path but it should also copy/create the parent folders (no files) leading to the source path. How can I accomplish this with Powershell or robocopy? If Powershell doesnt handle long path names then I prefer using Robocopy.
source:
c:\temp
\folder1
\file1.txt
\folder2
\file2.txt
\folder3
\file3.txt
output:
e:\backup
\temp
\folder1
\folder2
\file2.txt
\folder3
\file3.txt

If I have understood you right you want to create any missing folders in the output on the fly. In powershell this would be s.t. like this:
Copy-Item c:\temp e:\backup\temp\ -recurse

Related

Powershell - Copy files and rename if same name found

I am a bit confused on how to achieve this. Any input would be great.
I have a source dir and a target directory. During copy, if the file exists with same last modified stamp, then don't bother doing anything to save CPU resources.
If the source dir has a file that exists in target but the source version has a newer modified date, then the existing file in target directory should be renamed to file_yyyymmddhhmmss (the old modified date)
This way the new file can get in.
I got the basic copy here but struggling how can i amend this?
#Set source directory
$sourcedir="D:\Temp\2023\Source"
#Set target directory
$destinationdir="D:\Temp\2023\Destination"
#Set logfile - Add YYMMDDHHMMSS to log
$logfile="D:\Temp\2023\Log.txt"
robocopy $sourcedir $destinationdir /e /xo /log:$Logfile
If you think this can be achieved in Batch script, then fine. Otherwise Powershell is first preference.

How to exclude file types in specific subfolders with robocopy while mirroring?

I want to mirror one folder to another. However, the source folder will have many *.pdb file, most of which I want to exclude.
Root/Subfolder1/a.pdb
Root/Subfolder2/b.pdb
Root/Subfolder2/Subsubfolder/c.pdb
I want to copy only .pdb files in Subfolder1 Note that there are thousands of .pdb files and thousands folders at various sublevels - I don't want any .pdb files which are within Subfolder2, directly or in sub(subsubsub)folders.
My original attempt is:
robocopy C\:Projects\Root D:\SomewhereElse\ /MIR /XF "C:\Projects\Root\Subfolder2\*.pdb"
This, however, does not work.
If I change it for this:
robocopy C\:Projects\Root D:\SomewhereElse\ /MIR /XF "C:\Projects\Root\Subfolder2\.pdb"
it works, but doesn't do what I want it to do.
After some experimenting, I figured out that the /XF won't work if there are both a slash () and an asterix (*) within the same path.
Why would anyone do this? How can I achieve what I want? Why is this so stupid?

Get-ChildItem path without specifying the full path

So i am trying to get-childItem of a specific path.
my command look like this
(Get-ChildItem -Path fes\.policy -Recurse -Directory -Exclude ".*")
and the fullpath is something like this:
path 'C:\Users\(name)\(folder)\(folder)\(folder)\(folder)\governance\fes\.policy
and it works fine when I stand in the \governance\ folder and run the command. But when i am some where else it just add the parameter \fes.policy and says that the path not exsist
Get-ChildItem: Cannot find path C:\Users\(name)\(folder)\(folder)\(folder)\(folder)\governance\src\Tests\fes\.policy' because it does not exist.
which makes sense, but it just dont know how I get it to go back in directories and then put the \fes.policy on the path.
The start of the path is not always going to be the same, so I cant just put the whole path in.
You need to provide either the Fully-qualified path (the path starting from the drive root starting with C:) or the relative path (the path from the current working directory). From what it sounds like, you are trying to use a relative path, which is useful when the folder structure is only partially guaranteed (e.g. FolderA in the example below will always exist, but it's placement under C: may differ from system to system).
.. is a reserved directory "name" meaning the parent folder, similar to how . is used to represent the current folder. You will need to provide the relative path to the file using .. if you are in a different branch of the directory structure than where fes/.policy is located.
Since I don't know your full directory structure and only what you've provided let's take the following example:
Current directory: C:\Users\username\FolderA\FolderB\FolderC
Target file: C:\Users\username\FolderA\governance\src\Tests\fes\.policy
From FolderC, you should be able to locate the file with Get-ChildItem using the following path:
Note: If any of the path has spaces you will want to wrap them in single or double quotes (the latter if you want to expand variables within the path string), or else escape the spaces with `, though quoting is preferred.
Get-ChildItem ../../governance/src/Tests/fes/.policy
Since .. represents the parent folder of the current directory, ../../ represents two parents up, and would put you in FolderA. From there you know governance/ exists and you can craft the rest of the path as you need.

How to copy a file from source to a destination and recreate the folder structure

I have some files which needs to be copied through deployment process often to a destination. This is my folder folder structure:
SOURCE:
c:\
folder1
sub1
subsub1
file1
file2
I need something where i can tell my "script" something like this
mycopy c:\folder1\sub1\subsub1\file1 h:\
That means that i dont want to
check if folder structure exist
provide on bot sides the complete structure for each file on destination side
I want to
provide the full path and filename on source side
create folder structure if not exist
overwrite file if exist
How can i achieve this?
See the robocopy help page on MS Technet
https://technet.microsoft.com/en-us/library/cc733145%28v=ws.11%29.aspx
In your case you would do something like
robocopy c:\folder1\sub1\subsub1 h:\ file1 <options ...>
Notice that the first two parameters are the source and dest paths only, with no filename at the end
Read the linked help page and test on your desktop ...
Ok it works like this:
You can use Robocopy for this task. Thanks you Rick716 for this direction. I am not marking it as answer cause it is only a direction not the solution.
Lets Assume that we have the following source folder structure:
N:\source\a1\b1\c1\d1\e1\f1
and we want to recreate the hole structure under n:\source within h:\destination. Then we have to use Robocopy in this way:
robocopy N:\source h:\destination /e
The option /e will create the folder structure even create empty folders. Additionally you can append the files which should be copied. For example
file.txt
*.jpg
*.bkp
etc. But these files will be even copied when they exist somewhere within the folder strcuture! For example you have the file n:\source\a1\file.txt and the file n:\source\a1\b1\c1\file.txt then both will be copied by using
robocopy N:\source h:\destination file.txt /e

How to exclude subdirectories in the destination while using /mir /xd switch in robocopy

There is a script running which mirrors a bunch of folders from one volume to another. The problem is that now there are going to be subdirectories within those folders at the destination which are not part of the original mirroring script. They are standalone subdirectories and I don't want them purged once the mirroring kicks in. Is there a way for me to use the /xd switch in robocopy wherein I'd be able to exclude the destination subdirectories.
Example:
robocopy "\\hq04t2fis202\archive\dr" "\\hq04t3fis202\archive\dr" /mir /xd "\\hq04t3fis202\archive\dr\*\hq04s2dba301"
In the above example, there are several directories under \\hq04t2fis202\archive\dr that are being mirrored. And at the destination, once these directories are mirrored from the source, there is another script which dumps separate subdirectories within each of those directories. So what I want to do is somehow use the /xd switch to avoid purging those subdirectories at the destination.
Also, in the above example, in the /xd switch, I'm using the "*" wildcard to mean that I want to include all the directories that fall under the "\\hq04t3fis202\archive\dr" root folder at the destination.
Need help and suggestions as to whether this is possible, and if it is then how can I do it. So far I've tried and tested many other switches like /xo, /xx, but none of them solve my purpose.
Also, /xx would work except that now it won't delete any folders at the destination at all and it wouldn't be mirroring.
Hopefully I'm not overly confusing everyone here. Let me know if you have any questions.
When i tried the solution with /XD i found, that the path to exclude should be the source path - not the destination.
e.g. this Works
robocopy c:\test\a c:\test\b /MIR /XD c:\test\a\leavethisdiralone\
Rather than creating empty directories in source to exclude, you can supply the full destination path to the /XD switch to have the destination directories untouched
robocopy "%SOURCE_PATH%" "%DEST_PATH%" /MIR /XD "%DEST_PATH%"\hq04s2dba301
The issue is that even though we add a folder to skip list it will be deleted if it does not exist.
The solution is to add both the destination and the source folder with full path.
I will try to explain the different scenarios and what happens below, based on my experience.
Starting folder structure:
d:\Temp\source\1.txt
d:\Temp\source\2\2.txt
Command:
robocopy D:\Temp\source D:\Temp\dest /MIR
This will copy over all the files and folders that are missing and deletes all the files and folders that cannot be found in the source
Let's add a new folder and then add it to the command to skip it.
New structure:
d:\Temp\source\1.txt
d:\Temp\source\2\2.txt
d:\Temp\source\3\3.txt
Command:
robocopy D:\Temp\source D:\Temp\dest /MIR /XD "D:\Temp\source\3"
If I add /XD with the source folder and run the command it all seems good the command it wont copy it over.
Now add a folder to the destination to get this setup:
d:\Temp\source\1.txt
d:\Temp\source\2\2.txt
d:\Temp\source\3\3.txt
d:\Temp\dest\1.txt
d:\Temp\dest\2\2.txt
d:\Temp\dest\3\4.txt
If I run the command it is still fine, 4.txt stays there 3.txt is not copied over. All is fine.
But, if I delete the source folder "d:\Temp\source\3" then the destination folder and the file are deleted even though it is on the skip list
1 D:\Temp\source\
*EXTRA Dir -1 D:\Temp\dest\3\
*EXTRA File 4 4.txt
1 D:\Temp\source\2\
If I change the command to skip the destination folder instead then the folder is not deleted, when the folder is missing from the source.
robocopy D:\Temp\source D:\Temp\dest /MIR /XD "D:\Temp\dest\3"
On the other hand if the folder exists and there are files it will copy them over and delete them:
1 D:\Temp\source\3\
*EXTRA File 4 4.txt
100% New File 4 3.txt
To make sure the folder is always skipped and no files are copied over even if the source or destination folder is missing we have to add both to the skip list:
robocopy D:\Temp\source D:\Temp\dest /MIR /XD "d:\Temp\source\3" "D:\Temp\dest\3"
After this no matters if the source folder is missing or the destination folder is missing, robocopy will leave it as it is.
The way you can exclude a destination directory while using the /mir is by making sure the destination directory also exists on the source. I went into my source drive and created blank directories with the same name as on the destination, and then added that directory name to the /xd. It successfully mirrored everything while excluding the directory on the source, thereby leaving the directory on the destination intact.
The argument order seems to matter... to exclude subdirectories, I used
robocopy \\source\folder C:\destinationfolder /XD * /MIR
...and that works for me (Windows 10 copy from Windows Server 2016)
Try my way :
robocopy.exe "Desktop\Test folder 1" "Desktop\Test folder 2" /XD "C:\Users\Steve\Desktop\Test folder 2\XXX dont touch" /MIR
Had to put /XD before /MIR while including the full Destination Source directly after /XD.