I am new to Powershell and Robocopy usage.
I am trying to send some files to destination path. But destination path contains some files that is not contained in source. I want that files remain intact after copy operation.
When i try to run Robocopy from a powershell script like this:
Robocopy sourcePath destinationPath /MIR
it syncs two paths. So that destinationPath lonely files are deleted.
Is there any way to prevent this behavior?
Don't use the /MIR flag. That is exactly to keep the source and destination in sync, including deletion of files missing in source.
Use robocopy /E to copy a folder structure including empty subfolders. By default it only copies new and newer files.
Related
i am using a Windows 10 64bit laptop to copy a bunch of folder and files to a SharePoint folder.
My Script is running in PowerShell.
If i try to copy the files and folders with the explorer (strg+c and strg+v) everything except .exe files are copying.
.exe files are blocked from the server.
If Robocopy is trying to copy the same set of folders and files:
First i use: Get-Credential
then: Connect-PnPOnline
and then copying with Robocopy.
If i copy the complete bunch of the folder i use following code:
ROBOCOPY $orgPath $newPath /MIR /dcopy:DAT /copy:DAT /r:0 /w:1
Robocopy is then working with this options:
*.* /S /E /DCOPY:DAT /COPY:DAT /PURGE /MIR /R:0 /W:1
If i just copy single files i use:
ROBOCOPY $orgPath $newPath $fileName /dcopy:DAT /copy:DAT /r:0 /w:1
Robocopy is then working with this options:
/DCOPY:DAT /COPY:DAT /R:0 /W:1
$orgPath = the path of the folder i want to copy (local or network drive)
$newPath = the path of the folder where i want to copy to (SharePoint)
$fileName = the file i want to copy
I just copy if the folder/file is not existing already.
With both versions i get the same result.
This is working fine. Robocopy is copying.
The folders and the most files (.xmlx .txt) are copying
The .exe files get the error 222 because the server is blocking this file. That is normal and i understand why he is doing it.
BUT:
.pdf files do not copy with robocopy. This files gives the error 64 (0x00000040).
So the server is not blocking this file. (i also can copy this file using the explorer) but all the .pdf files getting this error using robocopy.
But of course the connection is still working. robocopy is still copying further the .xmlx and .txt files.
The error is relating to the extension.
But i do not know why and i do not know how to solve the problem.
The Name of the .pdf files do not have any special caracters.
Does anyone can help me to fix the problem?
Thanks in advance
With best regards
Matze
Have a folder structure on local drive like this
and a similar structure on network drive like
C:\Documents\Projects\
\\myServer\Storage\Projects\
For each new project I create locally folders like
\Projects\abc\Subfolder1
\Projects\abc\Subfolder2
I need PowerShell or Batch file. The batch is stored in folder abc and it shall perform
Robocopy C:\Documents\Projects\abc \\myServer\Storage\Projects\abc
(of course some switches are reasonable)
Next project, for example xyz
\Projects\abc\Subfolder1
\Projects\abc\Subfolder2
I expect
Robocopy C:\Documents\Projects\xyz \\myServer\Storage\Projects\xyz
Each time I create new project I copy my sample folder structure that is supposed to contain the batch so I can quickly do copy for the project to network,
I think this can be solved by using variables for local and server base path or by doing som
I don't know which Robocopy parameters you need, but this could be a start. This assumes that you place this script in each \Projects\abc directory and run it from there.
$DestinationShare = '\\myServer\Storage'
$SourcePath = $PSScriptRoot
$DestinationPath = $PSScriptRoot -replace ".*?(?<path>\\Projects\\.*)","$DestinationShare`${path}"
robocopy $SourcePath $DestinationPath
This could be turned into a function that would have less static coding.
It is going to be simpler to just robocopy the entire Projects directory whenever it is required.
In batch you could do something like this:
#echo off
set "src=%~dp0"
set "dst=\\myServer\Storage"
for /d %%d in (%src%.) do set "name=%%~nd"
robocopy %src% %dst%\%name%\ /mir
%~dp0 is the directory in which the script resides. The for loop then extracts the name of that directory from the full path.
Using robocopy is it possible to make a copy of a file in same directory?
Something like this...
robocopy c:\temp\file.txt c:\temp\file_copy.txt
COPY could be used. Use /Y to confirm an overwrite.
COPY /Y "C:\temp\file.txt" "C:\temp\file_copy.txt"
I was pulling my hair out to try and figure this problem out. I finally found my own solution and maybe it will help you too.
I noticed that the syntax used to select the entire directory could be used to select a single file.
ROBOCOPY "*" "Directory source" "Directory Output unc path or non"
The above code will copy everything from the directory source folder to the directory output path.
Let's say you only wanted to copy 1 file from the directory source named "test.txt"
In order to do that use the following code:
ROBOCOPY "*test.txt" "Directory source" "Directory Output unc path or non"
Thats about it. It works really well and will only copy the file name you want.
Alternatively you can use
ROBOCOPY "*.txt" "Directory source" "Directory Output unc path or non"
to copy out all text documents from the directory source.
Similarly this will also work with any .ext
.zip .exe .txt .pdf etc..
I signed up to answer this question with a better method. Let me know if I succeeded.
I have multiple config files that I need to copy to a destination directory. Some of these files have multiple extensions like the following:
web.config.env1
web.config.env2
web.config.something
I also have files that just simply end in ".config". I'd like to use Robocopy to copy all files in a directory plus the ".config" files while excluding any ".config.*" files.
So I tried the following in powershell:
robocopy $SourceDir $DestinationDir "*.*" /L /XF "*.config.*"
However, if I try to exclude anything matching "*.config.*" it also excludes all configuration files that end with only with "*.config". How should I go about excluding the config files with additional extensions while copying all others?
I want to sync folders between two computers, one with XP and one with Vista. I want the two folders mirrored, except for security settings and folder settings. It is my understanding that /MIR switch will delete any 'extra' files in the destination folder, which would include the desktop.ini files. I can avoid copying desktop.ini files with /XA:SH How can I prevent robocopy from deleting the destination desktop.ini files?
If I have to do any extra scripting, I prefer PowerShell. But I hope robocopy can do it on its own.
Thanks.
/XF desktop.ini
Will exclude Desktop.ini (from copy or purge).
Replace the /MIR switch with /E, and don't use the /PURGE parameter.
Explanation: /MIR is the equivalent of using /E /PURGE, so by using /E without /PURGE, you achieve the results you desire.
Create a bat file so you can mirror the source at the destination and later once the mirror process is finish erase the files you desired.
For example:
robocopy SOURCE DEST /E /PURGE or /MIR or /E
command to erase recursively (test the command before running at production):
del /s DRIVe:\DESTINATION\desktop.ini
Hope this helps,
Luis