Move and rename log files

Posted on 2008-11-12
Last Modified: 2013-12-04

I wonder if somebody has a powershell/vb/batch script to move files which are older than 4 hours (create time) to different location and rename those with adding create date & time at end.

Example: c:\*.log move it to d:\LogDir\*.log directory and rename those to *-mm-dd-yy-hh-mm.log

Assistance would be appreciated.
Question by:AmitTank
    LVL 18

    Accepted Solution

    Try this... Change the <folder> and <new location> to the correct values and remove -whatif if it works as you suspect.
    $files = dir <folder> *.log | ?{$_.LastWriteTime -gt (Get-date).addhours(-4)}
    foreach($file in $files)
       $FileName = $ -replace $file.Extension,""
       $NewName = "${FileName}$(Get-date -f "MM-dd-yy-HH-mm").log"
       rename-item -path $file.fullname -newname $NewName -whatif
       Move-Item $NewName -dest <new location> -whatif

    Open in new window

    LVL 2

    Assisted Solution

    try this one - it is vbscript.  rename the file file to .vbs.
    if you do not want to see the output, put a ' in front of the wscript.echo lines.  if you get responses in a pop-up message, change the default script host on your pc to use cscript.exe instead of wscript.exe using this command:  C:\>cscript //h:cscript

    LVL 6

    Author Closing Comment

    LVL 2

    Expert Comment

    thanks for the A.
    LVL 6

    Author Comment

    Thanks for sharing the script... :)

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Find Ransomware Secrets With All-Source Analysis

    Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

    This article is the result of a quest to better understand Task Scheduler 2.0 and all the newer objects available in vbscript in this version over  the limited options we had scripting in Task Scheduler 1.0.  As I started my journey of knowledge I f…
    Set OWA language and time zone in Exchange for individuals, all users or per database.
    This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA.…
    Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…

    759 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    8 Experts available now in Live!

    Get 1:1 Help Now