Solved

Window Powershell

Posted on 2014-02-09
7
317 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 19

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
Are You Headed to Black Hat USA 2017?

Getting ready for Black Hat next week? Kick things off with the WatchGuard Badge Challenge and test your puzzle and cipher skills. Do you have what it takes to earn our limited edition Firebox Badge? Get started today - https://crimsonthorn.net

 
LVL 35

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 19

Author Closing Comment

by:suriyaehnop
ID: 39845919
Thank you,

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

Expert Comment

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

Featured Post

Office 365 Training for IT Pros

Learn how to provision tenants, synchronize on-premise Active Directory, implement Single Sign-On, customize Office deployment, and protect your organization with eDiscovery and DLP policies.  Only from Platform Scholar.

Question has a verified solution.

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

Active Directory security has been a hot topic of late, and for good reason. With 90% of the world’s organization using this system to manage access to all parts of their IT infrastructure, knowing how to protect against threats and keep vulnerabil…
Auditing domain password hashes is a commonly overlooked but critical requirement to ensuring secure passwords practices are followed. Methods exist to extract hashes directly for a live domain however this article describes a process to extract u…
This tutorial will walk an individual through the process of configuring their Windows Server 2012 domain controller to synchronize its time with a trusted, external resource. Use Google, Bing, or other preferred search engine to locate trusted NTP …
Attackers love to prey on accounts that have privileges. Reducing privileged accounts and protecting privileged accounts therefore is paramount. Users, groups, and service accounts need to be protected to help protect the entire Active Directory …

617 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