how to emulate xcopy /D?

Ask your PowerShell-related questions, including questions on cmdlet development!
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.
This topic is 16 years and 1 month old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked
User avatar
anthill
Posts: 2
Last visit: Sun Mar 23, 2008 5:10 am

how to emulate xcopy /D?

Post by anthill »

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).
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

how to emulate xcopy /D?

Post by jvierra »

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.

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.
User avatar
donj
Posts: 416
Last visit: Thu May 29, 2008 5:08 am

how to emulate xcopy /D?

Post by donj »

One way to do "Xcopy /D" in PowerShell would be to simply run:

XCOPY /D

:)
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

how to emulate xcopy /D?

Post by jvierra »

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.
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

how to emulate xcopy /D?

Post by jvierra »

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.

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
This topic is 16 years and 1 month old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked