Question : Move and rename log files

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.

Answer : Move and rename log files

Try this... Change the and to the correct values and remove -whatif if it works as you suspect.
1:
2:
3:
4:
5:
6:
7:
8:
$files = dir  *.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  -whatif
}
Open in New Window Select All
Random Solutions  
 
programming4us programming4us