?
Solved

Move and rename log files

Posted on 2008-11-12
5
Medium Priority
?
2,051 Views
Last Modified: 2013-12-04
Hi,

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.
0
Comment
Question by:AmitTank
  • 2
  • 2
5 Comments
 
LVL 18

Accepted Solution

by:
BSonPosh earned 1200 total points
ID: 22943194
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 = $file.name -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

0
 
LVL 2

Assisted Solution

by:Briguy_572
Briguy_572 earned 800 total points
ID: 22943924
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

move-rename-older-log-files-rena.txt
0
 
LVL 6

Author Closing Comment

by:AmitTank
ID: 31516067
Thanks...
0
 
LVL 2

Expert Comment

by:Briguy_572
ID: 22949618
thanks for the A.
0
 
LVL 6

Author Comment

by:AmitTank
ID: 22949879
Thanks for sharing the script... :)
0

Featured Post

Has Powershell sent you back into the Stone Age?

If managing Active Directory using Windows Powershell® is making you feel like you stepped back in time, you are not alone.  For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Previously, on our Nano Server Deployment series, we've created a new nano server image and deployed it on a physical server in part 2. Now we will go through configuration.
In this post, I will showcase the steps for how to create groups in Office 365. Office 365 groups allow for ease of flexibility and collaboration between staff members.
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. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an anti-spam), the admin…

839 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