Solved

Window Powershell

Posted on 2014-02-09
7
313 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 29

Expert Comment

by:becraig
ID: 39845665
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
ID: 39845712
Yes
0
 
LVL 40

Accepted Solution

by:
Subsun earned 400 total points
ID: 39845739
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
Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

 
LVL 34

Expert Comment

by:Dan Craciun
ID: 39845743
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 29

Expert Comment

by:becraig
ID: 39845827
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
ID: 39845919
Thank you,

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

Expert Comment

by:Dan Craciun
ID: 39846019
Thank you, for providing the complete requirements for the solutions :)
0

Featured Post

Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

Question has a verified solution.

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

Create and license users in Office 365 in bulk based on a CSV file. A step-by-step guide with PowerShell script examples.
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.

832 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