Solved

Window Powershell

Posted on 2014-02-09
7
315 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
Edgartown IT Case Study

Learn about Edgartown's quest to ensure the safety and security of the entire town's employee and citizen data. Read the case study!

 
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

Problems using Powershell and Active Directory?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

Question has a verified solution.

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

While rebooting windows server 2003 server , it's showing "active directory rebuilding indices please wait" at startup. It took a little while for this process to complete and once we logged on not all the services were started so another reboot is …
In previous parts of this Nano Server deployment series, we learned how to create, deploy and configure Nano Server as a Hyper-V host. In this part, we will look for a clustering option. We will create a Hyper-V cluster of 3 Nano Server host nodes w…
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 to another domain controller. Log onto the new domain controller with a user account t…
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 …

730 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