Solved

Window Powershell

Posted on 2014-02-09
7
308 Views
Last Modified: 2014-02-09
I have log file generated daily. I would like to move the log file from default directory to the folder.

folder named created based on date format when the log generated.

Example, I have multiple log created on 07 Feb 2014/08 Feb 2014 at different times. My idea is to created a folder name with name e.g. 07022014.

Then move the log  created on specific date to the folder.

i have difficulty to come up with the idea of how to use log information to create the folder since there are multiple log generated on 07 Feb 2014
1.jpg
0
Comment
Question by:suriyaehnop
  • 2
  • 2
  • 2
  • +1
7 Comments
 
LVL 28

Expert Comment

by:becraig
Comment Utility
So let me try to get this:
You want to first:
Find all the files by created date (DAY)
Create a folder for that day
Then move/copy all the files with a create date matching that day to the folder ?
0
 
LVL 18

Author Comment

by:suriyaehnop
Comment Utility
Yes
0
 
LVL 40

Accepted Solution

by:
Subsun earned 400 total points
Comment Utility
You can try this code...
$source = "C:\HT01"
$Dest = "C:\New"
GCI $source -Include "*.log" -Recurse | 
	Select *,@{N="Date";E={Get-date $_.LastWriteTime -f ddMMyyy}}  | 
	Group Date | % {
	$Path = "$Dest\$($_.Name)"
	If (!(Test-Path $Path)){New-Item $path -type directory -Force}
	$_.Group | %{Move -Path $_.FullName -Destination "$Path\" -Force}
}

Open in new window

If the log file extension is different from *.log then change it in third line..
0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 34

Expert Comment

by:Dan Craciun
Comment Utility
Get-ChildItem -Path X:\your\path -File | % {
    $path=$_.DirectoryName
    $date = $_.CreationTime.ToString('yyyyMMdd')
    if (!(Test-Path -path "$path\$date")) {
        New-Item -Path "$path\$date" -ItemType directory
    } 
    Move-Item -Path $_.FullName -Destination ("$path\$date\" + $_.Name)
}

Open in new window


This will create folders named yearmonthday for each day and move the logs from that day in the folder.

HTH,
Dan
0
 
LVL 28

Expert Comment

by:becraig
Comment Utility
I think either of the guys already have the solution above, I just had to post mine  :(

$dest = "C:\Dest"; 
$logs = "c:\logfolder";
$dates = @(); 
gci $logs -Include "*.ext" -recurse | select-object  LastWriteTime | % {$dates += ($_.LastWriteTime| get-date -format M-d-yyyy)};  
$folderlist = ($dates | sort-object | gu); 
$folderlist | % {$nudir= "$dest\$_"; if (!(Test-path $nudir)) {mkdir  $nudir}; gci $logs -Include "*.ext" -recurse | % {Move-Item $_.FullName -Destination $nudir " | where {$_.LastWriteTime -like $_}}}

Open in new window


I think the extra overhead in mine, is that it reads twice and I waste time actually parsing for a unique date so that I only create one destination folder.
0
 
LVL 18

Author Closing Comment

by:suriyaehnop
Comment Utility
Thank you,

before posting, I'm a looking for solution similar to "Group".
0
 
LVL 34

Expert Comment

by:Dan Craciun
Comment Utility
Thank you, for providing the complete requirements for the solutions :)
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Set OWA language and time zone in Exchange for individuals, all users or per database.
This article will help you understand what HashTables are and how to use them in PowerShell.
This tutorial will walk an individual through the steps necessary to join and promote the first Windows Server 2012 domain controller into an Active Directory environment running on Windows Server 2008. Determine the location of the FSMO roles by lo…
This tutorial will walk an individual through the process of transferring the five major, necessary Active Directory Roles, commonly referred to as the FSMO roles from a Windows Server 2008 domain controller to a Windows Server 2012 domain controlle…

762 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

6 Experts available now in Live!

Get 1:1 Help Now