Trying to use Powershell to backup some large dir -newbie
I can't make this line to work
"C:\Robocopy\RoboCopy.exe" $source $destination "/E /R:10 /W:5 /V /ETA"
Robocopy at best (depending on the " i put here or there..) is executed but it's its GUI that is launched (and nothing more is done).
There's no issue with the $dest and $source (I manage to log into a txt file and his is working)
Thank you
Use this:
& "C:\Robocopy\RoboCopy.exe" $source $destination /E /R:10 /W:5 /V /ETA
The & (call) operator is required if you want PowerShell to run a quoted string as a command.
In this specific case, the quotes are not needed because the executable's path and filename don't contain spaces, so you can just write this instead:
C:\Robocopy\RoboCopy.exe $source $destination /E /R:10 /W:5 /V /ETA
But is robocopy.exe really sitting in C:\Robocopy? Do you have that directory name? Robocopy.exe is a tool that comes with the OS and should already be in the path. Why not just this?
robocopy $source $destination /E /R:10 /W:5 /V /ETA
Related
I am trying to delete a folder using robocopy mirroring like this:
Start-Process -FilePath "robocopy.exe" -ArgumentList "$emptyDir $sourcePath /mir /e /np /ns /nc /njs /njh /nfl /ndl" -Wait -PassThru -NoNewWindow but still get a line of output for every deleted file
I tried adding >nul 2>&1 as explained in another answer here Start-Process -FilePath "robocopy.exe" -ArgumentList "$emptyDir $sourcePath /mir /e /np /ns /nc /njs /njh /nfl /ndl >nul 2>&1" -Wait -PassThru -NoNewWindow but still get the same output.
Since you're running robocopy in the current console window (-NoNewWindow), synchronously (-Wait), there is no reason to use Start-Process at all - just invoke robocopy directly, which also allows you to use > redirections effectively:
robocopy.exe $emptyDir $sourcePath /mir /e /np /ns /nc /njs /njh /nfl /ndl *>$null
Note:
Direct execution makes a program's stdout and stderr output directly available to PowerShell, via its success and error output streams.
*>$null is a convenient PowerShell shortcut for silencing all output streams - see about_Redirection.
Another benefit of direct invocation is that the external program's process exit code is reported in PowerShell's automatic $LASTEXITCODE variable.
See also:
This answer provides background information.
GitHub docs issue #6239 provides guidance on when use of Start-Process is and isn't appropriate.
As for what you tried:
You fundamentally cannot suppress output from a process launched with Start-Process -NoNewWindow on the PowerShell side.
Trying to silence command output at the source, i.e. as part of the target process' command line with >nul 2>&1, would only work if cmd.exe were the -FilePath argument and you passed a robocopy command to it. > redirections are a shell feature, and robocopy itself isn't a shell.
You can try to pass arguments via splatting, and then use the object pipeline to parse line by line.
In the example below, I'm going to split the arguments into two groups, in case you wanted to change out the options programmatically.
$roboFileArgs = #(
<#
If you're sure your argument is already a string or
a primitive type, there's no need to quote it.
#>
$emptyDir
$sourcePath
)
$roboFlags = "/mir","/e","/np","/ns","/nc","/njs","/njh","/nfl","/ndl"
# We can use splatting to pass both lists of arguments
robocopy.exe #roboFileArgs #roboFlags |
Foreach-Object {
<#
process output line by line and turn it into objects
or pipe to Out-Null if you truly don't care.
#>
}
The following code deletes everything in Desktop, Download and Documents including the folders themselves. Is there any way to delete all the folders inside, but not the folder itself?
echo cmd delete all files in folder
del C:\Users\shmuel_admin\Downloads\*.*" /s /f /q
del C:\Users\shmuel_admin\Desktop\*.*" /s /f /q
del C:\Users\shmuel_admin\Documents\*.*" /s /f /q
rd /s /q "C:\Users\shmuel_admin\Desktop"
rd /s /q "C:\Users\shmuel_admin\Downloads"
rd /s /q "C:\Users\shmuel_admin\Documents"
PowerShell.exe -NoProfile -Command Clear-RecycleBin -Confirm:$false
echo Done!
You're using PowerShell to empty out the Recycle Bin; why not use PowerShell for the entire job?
Write-Host "Deleting all files and subfolders..."
Get-ChildItem -Path C:\Users\shmuel_admin\Downloads -Recurse | Remove-Item
Get-ChildItem -Path C:\Users\shmuel_admin\Desktop -Recurse | Remove-Item
Get-ChildItem -Path C:\Users\shmuel_admin\Documents -Recurse | Remove-Item
Clear-RecycleBin -Confirm:false
Write-Host "Done!"
...and that's it!
I have folder c:\tocopy that I would like to copy the content to c:\dest1, c:\dest2 and overriding the existing files
I am not sure what my script would need to look like to be able to do this.
(using win2k12)
Thank you
would use robocopy for this purpose.
http://www.windows-commandline.com/robocopy-switches-syntax-examples/
http://social.technet.microsoft.com/wiki/contents/articles/1073.robocopy-and-a-few-examples.aspx
http://improve.dk/simple-file-synchronization-using-robocopy/
In this particular case:
robocopy c:\tocopy c:\dest1 /MIR /Z
robocopy c:\tocopy c:\dest2 /MIR /Z
/MIR makes dest mirror tocopy
/Z makes it so that the copy is restartable (depending on the size of the things you are copying you may not need this, but you probably want it when transferring files over the network or doing large copies
To use powershell, try the copy-item cmdlet:
Copy-Item c:\tocopy -Destination c:\dest1 -Recurse -Force
Copy-Item c:\tocopy -Destination c:\dest2 -Recurse -Force
I'm trying to copy a specific folder w/ files from a network drive using Robocopy.
The catch is, that the files I want to copy are updated often, and placed in folders with version-numbers. Would it be possible to use Robocopy to grab files from whatever folder has the highest number?
Ex: The source path looks like this:
K:\program\versions\6.7.0.144\
with '144' being the number that is changed often.
The path K:\Program\versions\ contains all versions, each in their own folder, like so:
http://i.stack.imgur.com/zDL16.png
So, each time I run the script, I want it to get files from the latest version/highest number.
So far, my script looks like this:
robocopy \\K:\program\versions\6.7.0.*\bin\config C:\Target /e /z /A-:R
Robocopy does not accept the * in the source-path. So, is this possible with Robocopy, or do I have to use a different approach?
You cannot with robocopy alone. You have to script it a bit.
Assuming you first versions are zeroed (like 6.7.001), then it is easy to get the highest version number you requested.
I provide below snippets for batch & powershell.
Batch:
set SRCPATH=K:\program\versions
for /f %%f in ('dir /b /ad /o-n %SRCPATH%') do set SRCVER=%%f & goto NEXT
:NEXT
echo # Version %SRCVER% will be used
robocopy %SRCPATH%\%SRCVER%\bin\config C:\Target /E /Z /A-:R /LOG:C:\backup.log
goto NEXT is to break for loop after first element, since we sorted by name, descending
Powershell:
$SRCPATH = "K:\program\versions"
$SRCPATH = "D:\temp"
$SRCVER = (Get-ChildItem $SRCPATH | Where-Object { $_.PsISContainer } | Sort-Object -Property Name -Descending | Select-Object -First 1).FullName
$SRCFULL= $SRCVER + '\bin\config'
echo "# Version $SRCVER will be used"
& robocopy $SRCFULL C:\Target /E /Z /A-:R /LOG:C:\backup.log
HTH
The only thing I can suggest vis using Robocopy only is the /MAXAGE: flag.
Otherwise I'd wrap Robocopy in a Powershell Script to do the directory selection for me.
$dirlist = "k:\program\version\6.7.0.1","k:\program\version\6.7.0.144","k:\program\version\6.7.0.77"
$pattern = [regex]'.*6\.7\.0\.(\d*)'
$maxvers = 0
foreach ($dirname in $dirlist) {
$vers = $pattern.match( $dirname ).Groups[1].Value
if($vers -gt $maxvers) { $maxvers = $vers }
}
$robodir = "k:\program\version\6.7.0.$maxvers\bin\config"
robocopy $robodir c:\Target /e /z /A-:R
My purpose was to copy files with long paths (+255 char) and spaces in the folder path from a computer to a server with an analogus folder already in place (needed to maintain folder structure). Below is an example of the script:
robocopy "c:\long path\with spaces" "\\servername\long path\with spaces" filename.html /MIR /R:5 /LOG+:\\server\logfolder /v /NP
The result was the below log and a complete deletion of the source machine. Any thoughts?
Source : c:\long path\with spaces
Dest : \
Files : filename.html
Options : /V /S /E /PURGE /MIR /NP /R:5 /W:30
I've been researching and have found no other situation. I'd absolutely love to avoid this issue in the future.
unfortunately, robocopy is going to see the "\\" as an escape sequence, and you end up with a path that's just \. you need to use /"\servername\long path\with spaces\/" for quoted UNC paths.