Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Window Powershell

Posted on 2014-02-09
7
Medium Priority
?
319 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 1600 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
Visualize your virtual and backup environments

Create well-organized and polished visualizations of your virtual and backup environments when planning VMware vSphere, Microsoft Hyper-V or Veeam deployments. It helps you to gain better visibility and valuable business insights.

 
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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

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 process allows computer passwords to be managed and secured without using LAPS. This is an improvement on an existing process, enhanced to store password encrypted, instead of clear-text files within SQL
This tutorial will walk an individual through the steps necessary to join and promote the first Windows Server 2012 domain controller into an Active Directory environment running on Windows Server 2008. Determine the location of the FSMO roles by lo…
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.
Suggested Courses

670 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