Link to home
Start Free TrialLog in
Avatar of suriyaehnop
suriyaehnopFlag for Malaysia

asked on

Window Powershell

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
Avatar of becraig
becraig
Flag of United States of America image

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 ?
Avatar of suriyaehnop

ASKER

Yes
ASKER CERTIFIED SOLUTION
Avatar of SubSun
SubSun
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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
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.
Thank you,

before posting, I'm a looking for solution similar to "Group".
Thank you, for providing the complete requirements for the solutions :)