none
Copying files based on modified date keeping folder structure RRS feed

  • Question

  • Hi,

    Sorry if this has been asked 100 times already, but I can't find the solution I'm looking for. I would like either a vbscript or powershell script to do the following:

    copy all files from d:\data that were only modified after 16/01/2015 18:00 to D:\Tempdata (whilst keeping folder structure)

    copy all files from T:\Data that were modified before 16/01/2015 18:00 to d:\data (overwriting them without a prompt, and logging any failures to a log.txt file).

    this is easy stuff for someone in the know I'm sure, we had some severe corruption over the weekend and we know our backup on the T drive is the most healthy one, we assume that if the file has been modified since 16/01/2015 18:00 then those files are fine. we are also missing files so this will ensure that those files will come back from the T drive.

    really appreciate your help, thanks

    Steve

    Monday, January 19, 2015 2:00 PM

Answers

  • You can call robocopy from within a script.

    i would not recommend scripting what is already available. For one, robocopy will perform significantly better than any script, secondly robocopy is certainly better tested and debugged than any of your own code.

    for example:

    StrMySource ="c:\temp"
    StrMyTarget= "d:\temp"
    
    StrCommand="robcopy.exe " & StrMySource & " " & StrMyTarget & " /S /MaxAge:5"
    intReturn = wsshell.run(StrCommand),1,true)
    if inreturn <> 0 then
       'robocopy returned an error
       wscript.echo "Robocopy returned code " & intreturn
    end if
     


    If you persist scripting this yourself, you will have to have an handle to the source folder and loop through all files. If conditions match, copy the file.

    this is something like listed in http://blogs.technet.com/b/heyscriptingguy/archive/2006/02/21/how-can-i-replace-a-local-file-with-a-newer-version-found-on-a-file-server.aspx

    or in Powershell:

    $StartDate = (get-date -year 2011 -month 6 -day 
    $EndDate = (get-date -year 2011 -month 6 -day 4)
    $StrSource ="c:\temp"
    $StrTarget= "d:\temp"
    Get-ChildItem $StrSource | Where-Object {($_.LastWriteTime.Date -ge $StartDate.Date) -and ($_.LastWriteTime.Date -le $EndDate.Date)} | Copy-Item -Destination $StrTarget


    MCP/MCSA/MCTS/MCITP

    • Marked as answer by Milkientia Tuesday, January 20, 2015 4:45 PM
    Monday, January 19, 2015 2:20 PM
  • This works for me in my limited testing.

    $cutoff = Get-Date '1/16/2015 18:00'
    $source = 'C:\Scripts\PowerShell Scripts'
    $dest = 'C:\t1'
    
    Get-ChildItem $source -File -Recurse | Where { $_.LastWriteTime -ge $cutoff } | ForEach {
    
        $actualSource = Split-Path $_.FullName
        $actualDest = Split-Path $_.FullName.Replace($source,$dest)
    
        robocopy $actualSource $actualDest $_.Name /SEC
    
    }

    I've gone about as far with this as I can, so you'll need to make any further adjustments that are needed based on your environment.


    Don't retire TechNet! - (Don't give up yet - 13,085+ strong and growing)

    Tuesday, January 20, 2015 3:30 PM

All replies

  • Hi,

    Sorry if this has been asked 100 times already, but I can't find the solution I'm looking for. I would like either a vbscript or powershell script to do the following:

    copy all files from d:\data that were only modified after 16/01/2015 18:00 to D:\Tempdata (whilst keeping folder structure)

    copy all files from T:\Data that were modified before 16/01/2015 18:00 to d:\data (overwriting them without a prompt, and logging any failures to a log.txt file).

    this is easy stuff for someone in the know I'm sure, we had some severe corruption over the weekend and we know our backup on the T drive is the most healthy one, we assume that if the file has been modified since 16/01/2015 18:00 then those files are fine. we are also missing files so this will ensure that those files will come back from the T drive.

    really appreciate your help, thanks

    Steve

    Monday, January 19, 2015 1:30 PM
  • That is what RoboCopy is for.

    Post RoboCopy questions in the forum for your OS.


    ¯\_(ツ)_/¯

    Monday, January 19, 2015 1:58 PM
  • can I have it in a VB Script or Powershell please, that is why I asked it in this forum since I may want to add further logic around it.

    Monday, January 19, 2015 2:01 PM
  • can I have it in a VB Script or Powershell please, that is why I asked it in this forum since I may want to add further logic around it.

    You can write your own script or look inthe repository for a script.  We do not write custom scripts.

    Robocopy does everythingyou asked and is built into all systems.

    type:

    robocopy /?


    ¯\_(ツ)_/¯

    Monday, January 19, 2015 2:11 PM
  • You can call robocopy from within a script.

    i would not recommend scripting what is already available. For one, robocopy will perform significantly better than any script, secondly robocopy is certainly better tested and debugged than any of your own code.

    for example:

    StrMySource ="c:\temp"
    StrMyTarget= "d:\temp"
    
    StrCommand="robcopy.exe " & StrMySource & " " & StrMyTarget & " /S /MaxAge:5"
    intReturn = wsshell.run(StrCommand),1,true)
    if inreturn <> 0 then
       'robocopy returned an error
       wscript.echo "Robocopy returned code " & intreturn
    end if
     


    If you persist scripting this yourself, you will have to have an handle to the source folder and loop through all files. If conditions match, copy the file.

    this is something like listed in http://blogs.technet.com/b/heyscriptingguy/archive/2006/02/21/how-can-i-replace-a-local-file-with-a-newer-version-found-on-a-file-server.aspx

    or in Powershell:

    $StartDate = (get-date -year 2011 -month 6 -day 
    $EndDate = (get-date -year 2011 -month 6 -day 4)
    $StrSource ="c:\temp"
    $StrTarget= "d:\temp"
    Get-ChildItem $StrSource | Where-Object {($_.LastWriteTime.Date -ge $StartDate.Date) -and ($_.LastWriteTime.Date -le $EndDate.Date)} | Copy-Item -Destination $StrTarget


    MCP/MCSA/MCTS/MCITP

    • Marked as answer by Milkientia Tuesday, January 20, 2015 4:45 PM
    Monday, January 19, 2015 2:20 PM
  • Ahh I see, ok, I will go with the robocopy route I think, it sounds like it will do what I want. I am not sure on the parameters so ill look into that

    thanks

    Steve

    Monday, January 19, 2015 3:04 PM
  • I am not sure on the parameters so ill look into that

    Here's a good resource:

    http://ss64.com/nt/robocopy.html


    Don't retire TechNet! - (Don't give up yet - 13,085+ strong and growing)

    Monday, January 19, 2015 3:36 PM
  • I thought I was correct the first time I posted this - a script to perform the copying. VB script has the ability to use the FSObject and create folder directories in the same structure as another I am fairly sure of that. yes it might not be tested and yes it might have to iterate through all the folders and be slower, but my original question was very specific to the date and time, those dates and times are there for a reason. it would appear that robocopy can only accept a min/max age in whole days which is not what I am after.

    so back to my original question, can someone help me make a script which does what I am after. SenneVL seems to have shown something that should work in powershell, however I am not as clued up in powershell as i'd like to be, can you explain the script a bit more please.

    I have a few questions:

    is the $StartDate required or can we just copy everything regardless of how old it is, we have files which are over a decade old and we don't really know what is corrupted or not, so we'd rather copy everything we have as far back as we have.

    secondly is it possible to go as focused as minutes or is days the best I can get with powershell?

    finally, will the command you used copy the entire folder structure as it is in the source?

    many thanks

    Steve

    Monday, January 19, 2015 3:53 PM
  • RoboCopy can and will do all of that.  You do not need to use PowerShell or VB to run ROboCopy.

    Robocopy can copy a folder structure with no files.  You can then incremetally choose which files to populate.  Once the initial setupis complete RoboCopy can be scheduled to copy or mirror aas needed. Only changes will be copied.

    RoboCopy has fully detailed logging to almost any level you might need.


    ¯\_(ツ)_/¯

    Monday, January 19, 2015 4:01 PM
  • so for example then, what would the command be to copy files from a source to a destination based on the modified date being 16/01/2015 at 18:00, I want all files after this date AND time to be copied somewhere, if a file was modified at 17:59 it doesn't get copied.

    I cant see what parameter to use for this.

    Monday, January 19, 2015 4:13 PM
  • so for example then, what would the command be to copy files from a source to a destination based on the modified date being 16/01/2015 at 18:00, I want all files after this date AND time to be copied somewhere, if a file was modified at 17:59 it doesn't get copied.

    I cant see what parameter to use for this.

    You need to read the help.  It is all there.

     /MINLAD:n :: MINimum Last Access Date - exclude files used since n.
                  (If n < 1900 then n = n days, else n = YYYYMMDD date).

    All age related values take a number of days or a date.


    ¯\_(ツ)_/¯

    Monday, January 19, 2015 4:17 PM
  • sorry but you're not making any sense here, you said a robocopy command can do what I want but you're just copying a parameter from the help file, the same help file I have already read. what is the command line used to be able to do what I am after, including the full date and time please because i'm not seeing that in the help I can't see any parameter that takes a time.

    thanks

    Steve

    Monday, January 19, 2015 4:31 PM
  • AFAIK, robocopy deals in dates only, not times.

    If you must be that granular, here's something you can start with (note the date format difference, sorry):

    $cutoff = Get-Date '1/16/2015 18:00'
    
    Get-ChildItem .\ -Recurse |
        Where { $_.LastWriteTime -ge $cutoff } |
            Copy-Item ....

    Syntax links:

    http://ss64.com/ps/get-date.html

    http://ss64.com/ps/get-childitem.html

    http://ss64.com/ps/where-object.html

    http://ss64.com/ps/syntax-compare.html

    http://ss64.com/ps/copy-item.html


    Don't retire TechNet! - (Don't give up yet - 13,085+ strong and growing)

    Monday, January 19, 2015 4:42 PM
  • i'm afraid I must be that granular because of the amount of work that has changed between the dates, your code looks like it is getting close to what I need. I will try this out tomorrow, any idea if this will keep the folder structure, and is there a way to copy over the original permissions?

    many thanks

    Steve

    Monday, January 19, 2015 6:18 PM
  • Copy-Item only copiess the fiel.  It will not create the folder structure or copy permissions.

    Robocopy will copy all folders and permissions.  Just set it to coppy only the folder structure.  The files willl have to bemamnaged individually and you will ahve to write an extensive amount of code to grab all permisisons and set them.  If you use ROboCopy it will coreectly copy  all permissions


    ¯\_(ツ)_/¯


    • Edited by jrv Monday, January 19, 2015 6:42 PM
    Monday, January 19, 2015 6:41 PM
  • Another issue with Copy-Itemm is that it willnot copy long fole names (path more than 260 characters).  RoboCopy can.

    Copy-Item is very slow.  RoboCopy is multi-threaded, restartable and very fast.


    ¯\_(ツ)_/¯

    Monday, January 19, 2015 6:46 PM
  • First, I would add that robocopy copies files intelligently - if a target file already exists (based on name and date/time stamp), it won't copy it again.

    Second, this isn't a script request forum. The place to request scripts is here:

    https://gallery.technet.microsoft.com/scriptcenter/site/requests

    Keep in mind that the script request list is perused by volunteers, so there's no guarantee that someone will take the time to write all the code you need for free.


    -- Bill Stewart [Bill_Stewart]

    Monday, January 19, 2015 9:31 PM
    Moderator
  • I understand it's not a script request forum, if someone could give me the key bits of what I need im sure I can work it out from there. I know the suggested answer to use robocopy is not a script question, fine but what's the harm in giving me the command line to use robocopy in the way I want it to? I am already at a time limited stage, not to mention stressed due our data loss experience, and I can't find the appropriate examples to follow for what I am after (I had read about robocopy before posting this question, it didn't appear to do what I wanted so I then thought a script would be my answer). right now, I am still confused over what I need to use, and I still don't have a concrete answer on how to proceed.

    If I dumb this down to this level will it make it any easier to provide a solid answer?:

    I need a mechanism to copy files using their modified date AND time.

    I need to keep the folder structure and permissions in tact.

    I am not sure if I can make the requirements any simpler, but if there is a solid answer to this, then we can at least take the next step in asking HOW? :)

    I know scripting is challenging, and I really do admire the skills involved to do programming - but yet again I have come to a scripting forum because I am in need of HELP which is what this place is supposed to provide, but have instead been fobbed off with half arsed answers (not by you Bill)

    the resource Mike gave me was good, but I cant see what parameters I need to meet the requirements of date and time

    the resource senneVL looked along the right track but if it doesn't copy permissions or folder structure it obviously isn't the answer.

    the answers provided by JRV are not helpful, if you know the answer can you just tell me and we can get on with more important things than this thread, if you don't know the answer stop replying and wasting my time.

    thank you for the help

    Steve

    Tuesday, January 20, 2015 8:02 AM
  • You can use a hybrid approach. Use PowerShell to gather your files based on date/time, and then use robocopy to copy them.

    $cutoff = Get-Date '1/16/2015 18:00'
    
    Get-ChildItem .\ -Recurse | Where { $_.LastWriteTime -ge $cutoff } | ForEach {
    
        $file = $_.Fullname
    
        robocopy ...
    
    }


    Don't retire TechNet! - (Don't give up yet - 13,085+ strong and growing)

    Tuesday, January 20, 2015 1:27 PM
  • Hi Mike,

    your example looks like it will do the job, however my script isn't quite accurate - it seems to be copying all the files and folders regardless of the modified date. this is what I got.

    $cutoff = Get-Date '1/16/2015 18:00'
    $StrSource ="D:\ROBOCOPY\Live"
    $StrTarget= "D:\ROBOCOPY\LiveTemp"
    
    Get-ChildItem $StrSource -Recurse | Where { $_.LastWriteTime -ge $cutoff } | ForEach {
    
        $file = $_.Fullname
    
        robocopy $file $StrTarget /E /B /COPYALL /V /FP /log:"d:\robocopy\copylog.txt"
    
    }

    thanks

    Steve

    Tuesday, January 20, 2015 2:09 PM
  • This may have to do with the date difference between our systems, I'm not sure.

    Try this, are you getting the list of files you expect?

    $cutoff = Get-Date '1/16/2015 18:00'
    
    Get-ChildItem .\ -Recurse | Where { $_.LastWriteTime -ge $cutoff } | ForEach {
    
        $file = $_.Fullname
    
        $file
    
    }


    Don't retire TechNet! - (Don't give up yet - 13,085+ strong and growing)

    Tuesday, January 20, 2015 2:36 PM
  • Robocopy's first two parameters must be directory names, not file names.

    -- Bill Stewart [Bill_Stewart]

    Tuesday, January 20, 2015 2:45 PM
    Moderator
  • Robocopy's first two parameters must be directory names, not file names.

    -- Bill Stewart [Bill_Stewart]

    Good eyes, I didn't even look at the robocopy command that was being used.


    Don't retire TechNet! - (Don't give up yet - 13,085+ strong and growing)

    Tuesday, January 20, 2015 2:50 PM
  • ahh so does this mean I can't use robocopy to copy an actual file it has to be an entire directory? I cant do something like:

    robocopy $strSource +\$file ?

    in response to Mike, the $file is actually getting the correct files, I can see the output is skipping the files which don't meet my date criteria, so nearly there. I also used the -File parameter in get-child because it was picking up an entire directory in each loop which I don't want. its now just a case of getting the source and destination paths correct in robocopy

    thanks

    Steve

    Tuesday, January 20, 2015 2:51 PM
  • ahh so does this mean I can't use robocopy to copy an actual file it has to be an entire directory?

    No. It means you must use a correct robocopy command line. File specifications come after the directory specifications on robocopy's command line, as detailed in the documentation.


    -- Bill Stewart [Bill_Stewart]

    Tuesday, January 20, 2015 3:03 PM
    Moderator
  • As I recommended earlier.  Use RoboCopy to create thetarget folder strucure with no files.

    Use Copy-Item to copy files.  Use Get-Acl/Set-Acl tocopy permissions.

    This is NOT  a tricial request.  It is also not a scriping issue.  You should contact anexperienced and competent consultant to do this for you since it is a critical issus. Trying to badger people in a forum into giving you a solution is just plain stupid.

    You say you want the pieces.  YOu have the pieces yet you still fo not know how to proceed.


    ¯\_(ツ)_/¯

    Tuesday, January 20, 2015 3:20 PM
  • This works for me in my limited testing.

    $cutoff = Get-Date '1/16/2015 18:00'
    $source = 'C:\Scripts\PowerShell Scripts'
    $dest = 'C:\t1'
    
    Get-ChildItem $source -File -Recurse | Where { $_.LastWriteTime -ge $cutoff } | ForEach {
    
        $actualSource = Split-Path $_.FullName
        $actualDest = Split-Path $_.FullName.Replace($source,$dest)
    
        robocopy $actualSource $actualDest $_.Name /SEC
    
    }

    I've gone about as far with this as I can, so you'll need to make any further adjustments that are needed based on your environment.


    Don't retire TechNet! - (Don't give up yet - 13,085+ strong and growing)

    Tuesday, January 20, 2015 3:30 PM
  • Mike - still no folder structure or permissions.

    It needs to be done as I outlined above.  It also needs to be done by someone with more trianing and knowledge of how to use WIndows and the NTFS file system.  THisincremental discovery of a solution can go on for days.


    ¯\_(ツ)_/¯

    Tuesday, January 20, 2015 4:01 PM
  • Mike - still no folder structure or permissions.

    It needs to be done as I outlined above.  It also needs to be done by someone with more trianing and knowledge of how to use WIndows and the NTFS file system.  THisincremental discovery of a solution can go on for days.


    ¯\_(ツ)_/¯

    It created the folder structure I needed (C:\t1 was empty). Also, perhaps I just misunderstand the point of /SEC, but the way I read it security information should be included.


    Don't retire TechNet! - (Don't give up yet - 13,085+ strong and growing)

    Tuesday, January 20, 2015 4:07 PM
  • I am already at a time limited stage, not to mention stressed due our data loss experience, and I can't find the appropriate examples to follow for what I am after...I am still confused over what I need to use, and I still don't have a concrete answer on how to proceed.

    Everyone can sympathize with your predicament, but please keep in mind that the respondents in this forum are volunteers, and the help here is free. Mike L. has done a great deal to give you the basic building blocks for a solution. Unfortunately, we're not really able to drive this to a solution that meets all of your specifications--that part is up to you. If you need for someone to write the code for you, then I would suggest hiring a consultant and paying for what you need.


    -- Bill Stewart [Bill_Stewart]

    Tuesday, January 20, 2015 4:15 PM
    Moderator
  • Mike, sorry I missed the/SEC and I didn't know you had created the empty structure.  Given that your metod should work but willfail on long paths.  RC handles long paths correctly.


    ¯\_(ツ)_/¯

    Tuesday, January 20, 2015 4:20 PM
  • Mike, sorry I missed the/SEC and I didn't know you had created the empty structure.  Given that your metod should work but willfail on long paths.  RC handles long paths correctly.


    ¯\_(ツ)_/¯

    Someday we'll all be free of the long path headaches... That day can't come quickly enough. =]


    Don't retire TechNet! - (Don't give up yet - 13,085+ strong and growing)

    Tuesday, January 20, 2015 4:22 PM
  • Mike, sorry I missed the/SEC and I didn't know you had created the empty structure.  Given that your metod should work but willfail on long paths.  RC handles long paths correctly.


    ¯\_(ツ)_/¯

    Someday we'll all be free of the long path headaches... That day can't come quickly enough. =]


    Don't retire TechNet! - (Don't give up yet - 13,085+ strong and growing)


    Oh.  Is NTFS 5.0 ready to be released?

    ¯\_(ツ)_/¯


    • Edited by jrv Tuesday, January 20, 2015 4:30 PM
    Tuesday, January 20, 2015 4:27 PM
  • Oh.  Is NTFS 5.0 ready tobereleased?

    ¯\_(ツ)_/¯

    I think it's on the 2039 roadmap.

    ...

    Maybe.


    Don't retire TechNet! - (Don't give up yet - 13,085+ strong and growing)

    Tuesday, January 20, 2015 4:29 PM
  • I am not criticizing Mike at all on the contrary, what he has done actually works if you can be bothered to test it properly JRV. Mike has provided me with a solution, the request was a script and mike has provided me with a script without being an ass. JRV has just been critical and useless and has added no value to this thread at all, I don’t know why you are here other than to wind other people up.

    Bill, I know this is free help but in the spirit of community that’s what we’re all here for, maybe JRV should find out a bit more about what community spirit is all about. I have seen in the past massive scripts posted here, what I asked for was very simple and small under a panic situation. I just can’t believe how uselss JRV is, and by the way copying and re-wording my quotes like that is rather childish, how old are you exactly 12?

    Mike, SenneVL and Bill – thank you for your time it’s most appreciated, I am glad there are still reasonable people in the world. And for the record JRV I've been using Windows since 3.1 and this has nothing really to do with NTFS anyway not sure where that comes from.

    Steve

    Tuesday, January 20, 2015 4:43 PM
  • Bill, I know this is free help but in the spirit of community that’s what we’re all here for, maybe JRV should find out a bit more about what community spirit is all about. I have seen in the past massive scripts posted here, what I asked for was very simple and small under a panic situation. I just can’t believe how uselss JRV is, and by the way copying and re-wording my quotes like that is rather childish, how old are you exactly 12?

    To me the phrase "That's what we're all here for" implies a two-way street: A certain amount of giving and a certain amount of taking. In looking at your post history I note that you have been very strong on "taking" during the past few months. There is a long string of farily complex questions you posted while at the same time not really making any contribution of your own. One gets the impression that you use these forums as a way of avoiding to pay a consultant.

    I could, of course, be wrong. Did I overlook a number of your substantial contributions in these forums?

    Tuesday, January 20, 2015 5:03 PM
  • to be honest, I really don't try and get on these forums much because I generally have too much work to do. I do use this place as a source of help yes, nothing wrong with that. I have recently asked a lot because of getting into new areas of expertise as generally I have to do everything in my job and not specialised in any one particular area. the hours are long but the pay is poor, I simply don't have the time to be looking at in depth answers for things, there are many things which others have posted in response to other peoples questions that answer it before me anyway. whatever my reasons for using TechNet forums for help is no reason to have responses like JRV has posted.

    oh and for the record, we pay tens of thousands of £ per year already in consultancy so I doubt that's the reason for coming here. a consultant sits there and takes money and gives no education in return, whereas this place is (usually) more in the spirit of educating others.... and if you think I don't go on training, think again - i'm booked onto three courses this year costing £3-4k each.

    pipe, smoke it.

    Tuesday, January 20, 2015 6:00 PM
  • to be honest, I really don't try and get on these forums much because I generally have too much work to do. I do use this place as a source of help yes, nothing wrong with that. I have recently asked a lot because of getting into new areas of expertise as generally I have to do everything in my job and not specialised in any one particular area. the hours are long but the pay is poor, I simply don't have the time to be looking at in depth answers for things, there are many things which others have posted in response to other peoples questions that answer it before me anyway. whatever my reasons for using TechNet forums for help is no reason to have responses like JRV has posted.

    oh and for the record, we pay tens of thousands of £ per year already in consultancy so I doubt that's the reason for coming here. a consultant sits there and takes money and gives no education in return, whereas this place is (usually) more in the spirit of educating others.... and if you think I don't go on training, think again - i'm booked onto three courses this year costing £3-4k each.

    pipe, smoke it.

    So you mamnaged to badger everyone into doingthis for you and clearly have learnned nothing except how to be abusive.  I post many useful comments but you inststed that someone should do the typing for you.  Mike was kind enough to do so but you are the one who is supposed to bedoing thee work. I amm sure you will tell yourboss that you did it all by yourself.

    Well good luck.


    ¯\_(ツ)_/¯

    Tuesday, January 20, 2015 6:23 PM
  • Cheers, i'm sure ill get a pay rise off the back of your work so I can buy another acre attached to my small one bedroom flat.

    and I have never claimed to be an expert in scripting of any language, it really isn't where my experience is. I don't pretend to know everything either. maybe if you removed your head from a dark place you might see that being helpful to others could get you more friends than the right hand you call Dorris. I have read some of your recent posts and your replies are equally as useless on those as well, there is a big difference between the way Mike helps others and you help others. one of you washes your hands the other gets down and dirty to it. maybe you can take a leaf from someone else and actually be less rude and more well mannered.

    tally ho, good day chap

    Tuesday, January 20, 2015 6:53 PM
  • to get lastwritetime of the file can you do the following
    1
    get-childitem -Path $Source -ErrorAction Stop -Recurse | where-object { $_.LastWriteTime -lt (get-date).AddDays($days).Date }
    
    and to copy files and keep the folder structure can you do something like.
    1
    2
    3
    4
    5
    6
    7
                 $Files = Get-ChildItem -Path $Source -Recurse -ErrorAction Stop
                 if ($Files){
                            $Files | foreach {
                                   $target = $Destination + $_.FullName.Substring($Source.length);
                                   New-Item -ItemType file -Path $target -Force -Verbose;
                                   Copy-Item $_.FullName -Destination $target -Force -Verbose
                                   }
    
    Friday, February 27, 2015 12:39 PM
  • to get lastwritetime of the file can you do the following
    1
    get-childitem -Path $Source -ErrorAction Stop -Recurse | where-object { $_.LastWriteTime -lt (get-date).AddDays($days).Date }
    
    and to copy files and keep the folder structure can you do something like.
    1
    2
    3
    4
    5
    6
    7
                 $Files = Get-ChildItem -Path $Source -Recurse -ErrorAction Stop
                 if ($Files){
                            $Files | foreach {
                                   $target = $Destination + $_.FullName.Substring($Source.length);
                                   New-Item -ItemType file -Path $target -Force -Verbose;
                                   Copy-Item $_.FullName -Destination $target -Force -Verbose
                                   }
    

    You seem to have missed reading the question and the answers.  Go back and read the whole thread and you will see what you missed and why it is important.


    ¯\_(ツ)_/¯

    Friday, February 27, 2015 1:31 PM
  • jrv!

    i answered the first question and maybe my solution may help someone else. 

    What happens after the first thread i dont care :-)

    Friday, February 27, 2015 3:16 PM
  • jrv!

    i answered the first question and maybe my solution may help someone else. 

    What happens after the first thread i dont care :-)

    That is a terrible attitude that will lead to cluttered forums.


    Don't retire TechNet! - (Don't give up yet - 13,225+ strong and growing)

    Friday, February 27, 2015 3:19 PM
  • Actually you didn't even answer the first question.  You answered half of the question which is why I suggested reading a bit further to understand what the discussion was about.

    This is how we learn new things.


    ¯\_(ツ)_/¯

    Friday, February 27, 2015 3:31 PM
  • Yes this script worked , only slight modification required highlighted in bold(end bracket is missing) and that is as follows :

    $StartDate = (get-date -year 2011 -month 6 -day )

    Tuesday, May 22, 2018 11:22 AM