none
copy file LastWriteTime/rename RRS feed

  • Question

  • I tell you what I'm trying to do and as I could help.
    1.- I need to traverse a folder and copy the files created/modified during the last execution of the script.
    2.- Rename those files to the date-time of modification.

    I have this script.

    3.- I think that I have to use the LastWriteTime, auqnue sometimes gives me a little "strange" results.
    4.- How can I rename them?

    ********************************

    $source = "C:\test01"
    $destination = "C:\test02"
    $since = (Get-date).AddHours(-1)
    $logpath = "C:\test03" 
    $date = Get-Date -format yyyyMMdd 
        

        $items = get-childitem $source -recurse  |  where-object {$_.LastWriteTime –gt $since}
        foreach ($item in $items)
        {
                   $dir = $item.DirectoryName.Replace($source,$destination)
                   $target = $item.FullName.Replace($source,$destination)

                    if (!(test-path($dir))) { mkdir $dir }

            if (!(test-path($target)))
            {
                copy-item -path $item.FullName -destination $target -recurse -force
                
            }
            "Successfully copied $items to $target" | add-content $logpath\log-$date.txt 
        }

    ********************************

    Thanks!!

    Sunday, December 20, 2015 6:48 PM

Answers

  • Here are a couole of more ways to name a file:

    $file=get-item .\test.csv
    
    # ---
    Rename-Item $file -NewName $file.LastWriteTime.ToFileTime() -WhatIf
    
    # ---
    Rename-Item $file -NewName $file.LastWriteTime.ToString('yyyy-MM-dd-HH-mm-ss') -WhatIf
    


    \_(ツ)_/

    • Marked as answer by SysAdm.MAC Friday, January 8, 2016 7:23 PM
    Monday, December 21, 2015 9:50 AM

All replies

  • It is not possible to understand what you have posted.  Ou have many words that are not used correctly.

    Where did you copy the script from?  perhaps you should ask the author of the script for help.


    \_(ツ)_/

    Sunday, December 20, 2015 10:21 PM
  • I've changed the script. The only thing I need to know to help me to know how Ishould rename the files with the date-time of creation
    Monday, December 21, 2015 8:40 AM
  • [datetime]::Today.ToString('MMddyyyy')

    This can be applied to any date object.

    Here is a list of articles onhow to work with dates.: https://www.google.com/?gws_rd=ssl#newwindow=1&q=powershell+and+dates


    \_(ツ)_/

    Monday, December 21, 2015 8:51 AM
  • Thank you. I will check it
    Monday, December 21, 2015 9:24 AM
  • Here are a couole of more ways to name a file:

    $file=get-item .\test.csv
    
    # ---
    Rename-Item $file -NewName $file.LastWriteTime.ToFileTime() -WhatIf
    
    # ---
    Rename-Item $file -NewName $file.LastWriteTime.ToString('yyyy-MM-dd-HH-mm-ss') -WhatIf
    


    \_(ツ)_/

    • Marked as answer by SysAdm.MAC Friday, January 8, 2016 7:23 PM
    Monday, December 21, 2015 9:50 AM