suriyaehnop
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
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
ASKER
Yes
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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)
}
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 :(
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.
$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 $_}}}
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.
ASKER
Thank you,
before posting, I'm a looking for solution similar to "Group".
before posting, I'm a looking for solution similar to "Group".
Thank you, for providing the complete requirements for the solutions :)
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 ?