Powershell Remove-Item IF file already exists after Copy-item -


I need to add security net to my script. I am trying to get a copy job provided by a TCT file Based on the list of users. Copy that user from the home directory to a new location. Once the files are copied, check whether the file exists in the new location. If so, remove-item

Can anyone help me? I do not know if "if file exists" to apply the logic.

  $ username = get-content ' Users.txt 'foreign currency ($ UN $ UN) {$ dest = "\\ server \ homedirs \ $ un \ redirectedfolders" $ source = "\\ server \ homedirs \ $ un" new item-type type directory - path $ Dest \ document, $ dest \ Desktop Get-ChildItem $ source \ documents -Recurse -Exclude '* .msg' | Copy-item-destination $ waste \ document gate-childItem $ source \ desktop-reservation-allow '* .msg' | Copy-item- Destination $ Dest \ Desktop Get-ChildItem $ source \ mydocuments, $ source \ desktop -Recurse -Exclude '* .msg' |  per << code>   

To answer your question per , You can do it like this:

  go- child item $ source \ mydocuments, $ source \ desktop -Recurse -Exclude '*. msg '| % {If (test-path ($ _--^ "^ $ ([regex] :: escape ($ source))", "$ dest") {Remove items $ _-revised}}   $ incorrect   $ true    $ right  If the file exists on the given path, otherwise  $ false  
  • $ _ -replace "^ $ ([regex] :: escape ($ source))", "$ dest" changes the path of each source item With the corresponding destination path by changing $ source in the beginning of the path with the $ dest you are enumerating.
  • The original regex -install operator for the first argument is ^ $ source (meaning "source $ Strong> match the value of "). However, you need [regex] :: escape to use any regex special characters in $ source , which is actually with too There are likely windows paths because they are backslash, for example, in the value you have for $ source here in \ s , which is a " Any white space character "means $ ([regex] :: escape ($ source)) the value of $ source with any regex special charactersAg will be, so you're obviously match what value.

    She said, if your purpose is to copy each item to a new location, and to remove the original only when the copy of the new location is successful, it seems that the wheel changes . Why not use move-item instead of copy-items ?


    is not directly related to the question, but rather than replacing it, you can use the foreach loop command for each subdirectory: < To copy or order items, "$ source \ $ subdir" and "To change items", "$ source \ $ subdir" $ Path \ $ subdir "is ending the path)

  • Comments

    Popular posts from this blog

    Verilog Error: output or inout port "Q" must be connected to a structural net expression -

    jasper reports - How to center align barcode using jasperreports and barcode4j -

    c# - ASP.NET MVC - Attaching an entity of type 'MODELNAME' failed because another entity of the same type already has the same primary key value -