How do I perform "xcopy /D" in PowerShell?
I want to copy only the source files that are newer than the destination versions of the files (ie: synchronize two folders).
how to emulate xcopy /D?
Forum rules
Do not post any licensing information in this forum.
Any code longer than three lines should be added as code using the 'Select Code' dropdown menu or attached as a file.
Do not post any licensing information in this forum.
Any code longer than three lines should be added as code using the 'Select Code' dropdown menu or attached as a file.
how to emulate xcopy /D?
Here is a script that moves files based on a source with wild cards, a target folder and a date filter. To make it do a copy you only need to change oneline.
I updated this because it looked like an interesting problem for WMI. Many of teh other useful features on XCopy would take a significant amount of script to emulate. Shelling to XCopy is much easier and more powerful. Robocopy adds more to this including scripting capabilities. I recommend using Robocopy inplace of script or in place of XCopy for anything that is more than trivial.
Code: Select all
Dim objWMIService
Dim objItem
Dim SWBemlocator
Dim colItems
Dim sTargetFolder : sTargetFolder = "d:Systempto"
' look for files modified after this Date
Dim path : path ="systempfrom"
Dim drive : drive = "e:"
Dim fileMask : fileMask = "%"
Dim fileExt : fileExt = "vbs"
Dim myDate : myDate = "04/01/2005"
Set wd = CreateObject("WbemScripting.SWbemDateTime")
wd.SetVarDate CDate(myDate)
Set SWBemlocator = CreateObject("WbemScripting.SWbemLocator")
Set objWMIService = SWBemlocator.ConnectServer(".","rootCIMV2")
Dim sQuery : sQuery = "Select * from Cim_Datafile " & _
"WHERE " & _
"Drive='D:' " & _
"AND Path='" & path & "' " & _
"AND FileName LIKE '" & fileMask & "' " & _
"AND Extension LIKE '" & fileExt & "'" _
Set colItems = objWMIService.ExecQuery( sQuery,,48)
WScript.Echo sQuery
For Each objItem In colItems
If objItem.LastModified > wd Then
' here we would call the 'MOVE(ReName' or 'COPY' method to move/copy the file
' The 'ReName' method of the CIM_Datafile class can only renam in teh same drive.
' The 'ReName' method takes a folder of fully qualified name.
WScript.Echo "RENAMING: " & objItem.Caption
sNewName = sTargetFolder & objItem.FileName & "." & objItem.Extension
WScript.Echo vbTab & "TO:" & Replace(sNewName,"","")
objItem.Rename sNewName
End If
Next
I updated this because it looked like an interesting problem for WMI. Many of teh other useful features on XCopy would take a significant amount of script to emulate. Shelling to XCopy is much easier and more powerful. Robocopy adds more to this including scripting capabilities. I recommend using Robocopy inplace of script or in place of XCopy for anything that is more than trivial.
how to emulate xcopy /D?
Well I seem to hav emissed it again. OI was trying to point anthill to the missing "vault" so (s)he could poke around for more information. The request was kinda thin.
In keeping with "best procatices", Don, th efollowing is a better answer:
$log=xcopy /d:01-01-2008 c:windows*.log d:temptestr /L
The files the WOULD be copied or moved by date will be listed in teh $log variable for later use.
With WMI in PowerShell you can just use this as a query:
$query = "Select * from Cim_Datafile " + "WHERE " + "Drive='D:' " + "AND Path='" + $path + "' " + "AND FileName LIKE '" + $fileMask + "' " + "AND Extension LIKE '" + $fileExt + "'"
Of course you need to provide all of teh variables.
In keeping with "best procatices", Don, th efollowing is a better answer:
$log=xcopy /d:01-01-2008 c:windows*.log d:temptestr /L
The files the WOULD be copied or moved by date will be listed in teh $log variable for later use.
With WMI in PowerShell you can just use this as a query:
$query = "Select * from Cim_Datafile " + "WHERE " + "Drive='D:' " + "AND Path='" + $path + "' " + "AND FileName LIKE '" + $fileMask + "' " + "AND Extension LIKE '" + $fileExt + "'"
Of course you need to provide all of teh variables.
how to emulate xcopy /D?
I thought I should fix my broken example in case someone wants to use it.
Her is one that executes with variable replacement and handles line continuation and other issues correctly.
Be sure not to lose the line continuation "ticks" at the end of the select string lines or you will get an array of lines that won't execute. It is also necessary to escape - using those damn little "ticks" - the single quotes around the variables or the vars won't get replaced in the string except on the first assignment. ( wow! - run on sentence misery)
Notice also that WMI requires double "" in path names and requires the ending "" on the path.
jvierra2008-03-03 12:25:53
Her is one that executes with variable replacement and handles line continuation and other issues correctly.
Code: Select all
$drive = "C:"
$path="`Windows"
$fileMask="?"
$fileExt="log"
$query = "Select * from Cim_Datafile `
WHERE Drive=`'$drive`' `
AND Path=`'$path`' `
AND Extension LIKE `'$fileExt`'"
gwmi -query $query
Be sure not to lose the line continuation "ticks" at the end of the select string lines or you will get an array of lines that won't execute. It is also necessary to escape - using those damn little "ticks" - the single quotes around the variables or the vars won't get replaced in the string except on the first assignment. ( wow! - run on sentence misery)
Notice also that WMI requires double "" in path names and requires the ending "" on the path.
jvierra2008-03-03 12:25:53