TechTalkz.com Logo Ask the Experts!

Go Back   TechTalkz.com Technology & Computer Troubleshooting Forums > Tech Support Archives > Microsoft > Microsoft Windows Powershell

Getting Day of Week

Microsoft Windows Powershell

 
 
Thread Tools Display Modes
Unread 26-06-2008, 09:47 AM   #1
dm3281
Guest
 
Posts: n/a
Getting Day of Week

Hello --

I have about 100 directories labeled:

G100BC\Mon
G100BC\Tue
G100BC\Wed
G100BC\Thu
G100BC\Fri
G105BC\Mon
G105BC\Tue
G105BC\Wed
G105BC\Thu
G105BC\Fri
....
The list goes on...
....

I need a powershell script that can use a wildcard of C:\...\...\G??BC and
delete any folders that are not today or yesterday--ignoring weekends.

I.E, if today is Friday and I run the script on Friday morning, it will only
delete Mon,Tue,Wed. If I run the script on Mon, it
will only delete Tue,Wed,Thu

Is there an easy way to accomplish this in Powershell?

How can I retrieve the DOW?

I would like to schedule the script to run each morning Mon-Fri. Holidays
may be the only thing that will ***** me up, but I think I can live with
that.

Any help would be appreciated.




 
Unread 26-06-2008, 09:47 AM   #2
dm3281
Guest
 
Posts: n/a
Re: Getting Day of Week

Hi all -- I was trying to do something like this, then realized I needed to
pass in the "G???BC wildcard for each of the MON, TUE, WED, THU, FRI
subdirectories. I don't see an easy way to do this???

Also, I was trying to check that the direcotry exists before I do anything
to avoid errors... but that, too, seems kind of difficult with what I'm
doing???


switch ((get-date).dayofweek.toString())
{
Sunday { $dow="SUN"; break }
Monday { $dow="MON"; break }
Tuesday { $dow="TUE"; break }
Wednesday { $dow="WED"; break }
Thursday { $dow="THU"; break }
Friday { $dow="FRI"; break }
}

write-host "Deleting $dow files..."

switch ($dow)
{
MON { ; this was my sample to check directory first, but then I realized
I need to use wildcards for C:\TEMP\XMLBACUPS\G???BC folder structure....
arrghhh
if(Test-Path -path "C:\TEMP\XMLBACUPS\TUE") { get-childitem
"C:\TEMP\XMLBACKUPS\TUE\*.*" | remove-item what-if }
if(Test-Path -path "C:\TEMP\XMLBACKUPS\WED") { get-childitem
"C:\TEMP\XMLBACKUPS\WED\*.*" | remove-item what-if }
if(Test-Path -path "C:\TEMP\XMLBACKUPS\THU") { get-childitem
"C:\TEMP\XMLBACKUPS\THU\*.*" | remove-item what-if }
}
TUE {
get-childitem "C:\TEMP\XMLBACKUPS\WED\*.*" | remove-item what-if
get-childitem "C:\TEMP\XMLBACKUPS\THU\*.*" | remove-item what-if
get-childitem "C:\TEMP\XMLBACKUPS\FRI\*.*" | remove-item what-if
}
WED {
get-childitem "C:\TEMP\XMLBACKUPS\THU\*.*" | remove-item what-if
get-childitem "C:\TEMP\XMLBACKUPS\FRI\*.*" | remove-item what-if
get-childitem "C:\TEMP\XMLBACKUPS\MON\*.*" | remove-item what-if
}
THU {
get-childitem "C:\TEMP\XMLBACKUPS\FRI\*.*" | remove-item what-if
get-childitem "C:\TEMP\XMLBACKUPS\MON\*.*" | remove-item what-if
get-childitem "C:\TEMP\XMLBACKUPS\TUE\*.*" | remove-item what-if
}
FRI {
get-childitem "C:\TEMP\XMLBACKUPS\MON\*.*" | remove-item what-if
get-childitem "C:\TEMP\XMLBACKUPS\TUE\*.*" | remove-item what-if
get-childitem "C:\TEMP\XMLBACKUPS\WED\*.*" | remove-item what-if
}
}








"dm3281" <dm3281@nospam.net> wrote in message
news:032F4A19-AEBB-4095-8606-FC0E715A76CC@microsoft.com...
> Hello --
>
> I have about 100 directories labeled:
>
> G100BC\Mon
> G100BC\Tue
> G100BC\Wed
> G100BC\Thu
> G100BC\Fri
> G105BC\Mon
> G105BC\Tue
> G105BC\Wed
> G105BC\Thu
> G105BC\Fri
> ...
> The list goes on...
> ...
>
> I need a powershell script that can use a wildcard of C:\...\...\G??BC and
> delete any folders that are not today or yesterday--ignoring weekends.
>
> I.E, if today is Friday and I run the script on Friday morning, it will
> only delete Mon,Tue,Wed. If I run the script on Mon, it
> will only delete Tue,Wed,Thu
>
> Is there an easy way to accomplish this in Powershell?
>
> How can I retrieve the DOW?
>
> I would like to schedule the script to run each morning Mon-Fri. Holidays
> may be the only thing that will ***** me up, but I think I can live with
> that.
>
> Any help would be appreciated.
>
>
>
>


 
Unread 26-06-2008, 09:47 AM   #3
dm3281
Guest
 
Posts: n/a
Re: Getting Day of Week

This is my final of what I ended up with. It appears to work.

I feel like I could refactor this somehow to reduce the number of lines???

I'm also not sure if my formatting of the braces is in the spirit of
Powershell...???

My next plan is -- how do I automate this via Windows 2003 Task Scheduler?




switch ((get-date).dayofweek.toString())
{
Sunday { $dow="SUN"; break }
Monday { $dow="MON"; break }
Tuesday { $dow="TUE"; break }
Wednesday { $dow="WED"; break }
Thursday { $dow="THU"; break }
Friday { $dow="FRI"; break }
}

write-host "Deleting $dow files..."

switch ($dow)
{
MON {
if(Test-Path -path "\\10.246.16.86\D$\XMLBACKUPS\G???BC\TUE") {
get-childitem "\\10.246.16.86\D$\XMLBACKUPS\G???BC\TUE\*.*" |
remove-item -whatif }
if(Test-Path -path "\\10.246.16.86\D$\XMLBACKUPS\G???BC\WED") {
get-childitem "\\10.246.16.86\D$\XMLBACKUPS\G???BC\WED\*.*" |
remove-item -whatif }
if(Test-Path -path "\\10.246.16.86\D$\XMLBACKUPS\G???BC\THU") {
get-childitem "\\10.246.16.86\D$\XMLBACKUPS\G???BC\THU\*.*" |
remove-item -whatif }
}
TUE {
if(Test-Path -path "\\10.246.16.86\D$\XMLBACKUPS\G???BC\WED") {
get-childitem "\\10.246.16.86\D$\XMLBACKUPS\G???BC\WED\*.*" |
remove-item -whatif }
if(Test-Path -path "\\10.246.16.86\D$\XMLBACKUPS\G???BC\THU") {
get-childitem "\\10.246.16.86\D$\XMLBACKUPS\G???BC\THU\*.*" |
remove-item -whatif }
if(Test-Path -path "\\10.246.16.86\D$\XMLBACKUPS\G???BC\FRI") {
get-childitem "\\10.246.16.86\D$\XMLBACKUPS\G???BC\FRI\*.*" |
remove-item -whatif }
}
WED {
if(Test-Path -path "\\10.246.16.86\D$\XMLBACKUPS\G???BC\THU") {
get-childitem "\\10.246.16.86\D$\XMLBACKUPS\G???BC\THU\*.*" |
remove-item -whatif }
if(Test-Path -path "\\10.246.16.86\D$\XMLBACKUPS\G???BC\FRI") {
get-childitem "\\10.246.16.86\D$\XMLBACKUPS\G???BC\FRI\*.*" |
remove-item -whatif }
if(Test-Path -path "\\10.246.16.86\D$\XMLBACKUPS\G???BC\MON") {
get-childitem "\\10.246.16.86\D$\XMLBACKUPS\G???BC\MON\*.*" |
remove-item -whatif }
}
THU {
if(Test-Path -path "\\10.246.16.86\D$\XMLBACKUPS\G???BC\FRI") {
get-childitem "\\10.246.16.86\D$\XMLBACKUPS\G???BC\FRI\*.*" |
remove-item -whatif }
if(Test-Path -path "\\10.246.16.86\D$\XMLBACKUPS\G???BC\MON") {
get-childitem "\\10.246.16.86\D$\XMLBACKUPS\G???BC\MON\*.*" |
remove-item -whatif }
if(Test-Path -path "\\10.246.16.86\D$\XMLBACKUPS\G???BC\TUE") {
get-childitem "\\10.246.16.86\D$\XMLBACKUPS\G???BC\TUE\*.*" |
remove-item -whatif }
}
FRI {
if(Test-Path -path "\\10.246.16.86\D$\XMLBACKUPS\G???BC\MON") {
get-childitem "\\10.246.16.86\D$\XMLBACKUPS\G???BC\MON\*.*" |
remove-item -whatif }
if(Test-Path -path "\\10.246.16.86\D$\XMLBACKUPS\G???BC\TUE") {
get-childitem "\\10.246.16.86\D$\XMLBACKUPS\G???BC\TUE\*.*" |
remove-item -whatif }
if(Test-Path -path "\\10.246.16.86\D$\XMLBACKUPS\G???BC\WED") {
get-childitem "\\10.246.16.86\D$\XMLBACKUPS\G???BC\WED\*.*" |
remove-item -whatif }

}
}

 
Unread 26-06-2008, 09:48 AM   #4
dm3281
Guest
 
Posts: n/a
Re: Getting Day of Week

Sorry -- this is the final code.




switch ((get-date).dayofweek.toString())
{
Sunday { $dow="SUN"; break }
Monday { $dow="MON"; break }
Tuesday { $dow="TUE"; break }
Wednesday { $dow="WED"; break }
Thursday { $dow="THU"; break }
Friday { $dow="FRI"; break }
}

write-host "Deleting $dow files..."

switch ($dow)
{
MON {
if(Test-Path -path "\\10.246.16.86\D$\XMLBACKUPS\G???BC\TUE") {
get-childitem "\\10.246.16.86\D$\XMLBACKUPS\G???BC\TUE\*.*" |
remove-item -whatif }
if(Test-Path -path "\\10.246.16.86\D$\XMLBACKUPS\G???BC\WED") {
get-childitem "\\10.246.16.86\D$\XMLBACKUPS\G???BC\WED\*.*" |
remove-item -whatif }
if(Test-Path -path "\\10.246.16.86\D$\XMLBACKUPS\G???BC\THU") {
get-childitem "\\10.246.16.86\D$\XMLBACKUPS\G???BC\THU\*.*" |
remove-item -whatif }
}
TUE {
if(Test-Path -path "\\10.246.16.86\D$\XMLBACKUPS\G???BC\WED") {
get-childitem "\\10.246.16.86\D$\XMLBACKUPS\G???BC\WED\*.*" |
remove-item -whatif }
if(Test-Path -path "\\10.246.16.86\D$\XMLBACKUPS\G???BC\THU") {
get-childitem "\\10.246.16.86\D$\XMLBACKUPS\G???BC\THU\*.*" |
remove-item -whatif }
if(Test-Path -path "\\10.246.16.86\D$\XMLBACKUPS\G???BC\FRI") {
get-childitem "\\10.246.16.86\D$\XMLBACKUPS\G???BC\FRI\*.*" |
remove-item -whatif }
}
WED {
if(Test-Path -path "\\10.246.16.86\D$\XMLBACKUPS\G???BC\THU") {
get-childitem "\\10.246.16.86\D$\XMLBACKUPS\G???BC\THU\*.*" |
remove-item -whatif }
if(Test-Path -path "\\10.246.16.86\D$\XMLBACKUPS\G???BC\FRI") {
get-childitem "\\10.246.16.86\D$\XMLBACKUPS\G???BC\FRI\*.*" |
remove-item -whatif }
if(Test-Path -path "\\10.246.16.86\D$\XMLBACKUPS\G???BC\MON") {
get-childitem "\\10.246.16.86\D$\XMLBACKUPS\G???BC\MON\*.*" |
remove-item -whatif }
}
THU {
if(Test-Path -path "\\10.246.16.86\D$\XMLBACKUPS\G???BC\FRI") {
get-childitem "\\10.246.16.86\D$\XMLBACKUPS\G???BC\FRI\*.*" |
remove-item -whatif }
if(Test-Path -path "\\10.246.16.86\D$\XMLBACKUPS\G???BC\MON") {
get-childitem "\\10.246.16.86\D$\XMLBACKUPS\G???BC\MON\*.*" |
remove-item -whatif }
if(Test-Path -path "\\10.246.16.86\D$\XMLBACKUPS\G???BC\TUE") {
get-childitem "\\10.246.16.86\D$\XMLBACKUPS\G???BC\TUE\*.*" |
remove-item -whatif }
}
FRI {
if(Test-Path -path "\\10.246.16.86\D$\XMLBACKUPS\G???BC\MON") {
get-childitem "\\10.246.16.86\D$\XMLBACKUPS\G???BC\MON\*.*" |
remove-item -whatif }
if(Test-Path -path "\\10.246.16.86\D$\XMLBACKUPS\G???BC\TUE") {
get-childitem "\\10.246.16.86\D$\XMLBACKUPS\G???BC\TUE\*.*" |
remove-item -whatif }
if(Test-Path -path "\\10.246.16.86\D$\XMLBACKUPS\G???BC\WED") {
get-childitem "\\10.246.16.86\D$\XMLBACKUPS\G???BC\WED\*.*" |
remove-item -whatif }

}
}

 
Unread 26-06-2008, 09:48 AM   #5
Leo Tohill
Guest
 
Posts: n/a
RE: Getting Day of Week

This should get you pretty close:

#make an arraylist
$dows = new System.Collections.Arraylist(7)
#initialize it with the days of the week (the % operator is an alias for
"foreach"
"Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"|% {$dows.Add($_)}
# Remove today
$dows.Remove([DateTime]::Now.DayOfWeek.ToString().SubString(0,3))
# Remove Yesterday
$dows.Remove([DateTime]::Now.AddDays(-1).DayOfWeek.ToString().SubString(0,3))
# get FileInfo on all on the children of someplace
$items = Get-ChildItem c:\someplace
# filter to select the folders named by the remaining dow values, and remove
them
$d=items|where-object {$_.PsIsContainer -and
$dows.Contains($_.Name)}|remove-item

the $dows.Contains() will be a case-sensitive comparison: be careful of that.

the remove-item might need a -f (force) switch to make it work for you.

- Leo

"dm3281" wrote:

> Hello --
>
> I have about 100 directories labeled:
>
> G100BC\Mon
> G100BC\Tue
> G100BC\Wed
> G100BC\Thu
> G100BC\Fri
> G105BC\Mon
> G105BC\Tue
> G105BC\Wed
> G105BC\Thu
> G105BC\Fri
> ....
> The list goes on...
> ....
>
> I need a powershell script that can use a wildcard of C:\...\...\G??BC and
> delete any folders that are not today or yesterday--ignoring weekends.
>
> I.E, if today is Friday and I run the script on Friday morning, it will only
> delete Mon,Tue,Wed. If I run the script on Mon, it
> will only delete Tue,Wed,Thu
>
> Is there an easy way to accomplish this in Powershell?
>
> How can I retrieve the DOW?
>
> I would like to schedule the script to run each morning Mon-Fri. Holidays
> may be the only thing that will ***** me up, but I think I can live with
> that.
>
> Any help would be appreciated.
>
>
>
>
>

 
Unread 26-06-2008, 09:48 AM   #6
Shay Levi
Guest
 
Posts: n/a
Re: Getting Day of Week



dir G???BC\* | where {$_.name -ne (get-date -f ddd)} | remove-item -force
-recurse -whatIf


---
Shay Levi
$cript Fanatic
http://scriptolog.blogspot.com

> Hello --
>
> I have about 100 directories labeled:
>
> G100BC\Mon
> G100BC\Tue
> G100BC\Wed
> G100BC\Thu
> G100BC\Fri
> G105BC\Mon
> G105BC\Tue
> G105BC\Wed
> G105BC\Thu
> G105BC\Fri
> ...
> The list goes on...
> ...
> I need a powershell script that can use a wildcard of C:\...\...\G??BC
> and delete any folders that are not today or yesterday--ignoring
> weekends.
>
> I.E, if today is Friday and I run the script on Friday morning, it
> will only
> delete Mon,Tue,Wed. If I run the script on Mon, it
> will only delete Tue,Wed,Thu
> Is there an easy way to accomplish this in Powershell?
>
> How can I retrieve the DOW?
>
> I would like to schedule the script to run each morning Mon-Fri.
> Holidays may be the only thing that will ***** me up, but I think I
> can live with that.
>
> Any help would be appreciated.
>



 
Unread 26-06-2008, 09:49 AM   #7
Leo Tohill
Guest
 
Posts: n/a
RE: Getting Day of Week

This incorporates the weekend requirement, and deals with case sensitivity.

$dow = [DateTime]::Now.DayOfWeek
if (5,6 -contains $dow) {write-host "Don't run this on weekends!";return}
$dows = new System.Collections.Arraylist(5)
"mon", "tue", "wed", "thu", "fri" |% {$dows.Add($_)}
if ($dow -eq 1) {$dows.Remove("thu");$dows.Remove("fri")}
elseif ($dow -eq 2) {$dows.Remove("fri");$dows.Remove("mon")}
else
{
$dows.Remove([DateTime]::Now.DayOfWeek.ToString().SubString(0,3).ToLower( ))
$dows.Remove([DateTime]::Now.AddDays(-1).DayOfWeek.ToString().SubString(0,3).ToLower())
}
Get-ChildItem G???BC\*|where-object {$_.PsIsContainer -and
$dows.Contains($_.Name.ToLower())}|remove-item -recurse -force -whatif


 
Unread 26-06-2008, 09:49 AM   #8
Kiron
Guest
 
Posts: n/a
Re: Getting Day of Week

# -< script.ps1 >-
switch ((date).dayOfWeek.value__) {
1 {$pat = 'TUE|WED|THU'; break}
2 {$pat = 'WED|THU|FRI'; break}
3 {$pat = 'THU|FRI|MON'; break}
4 {$pat = 'FRI|MON|TUE'; break}
5 {$pat = 'MON|TUE|WED'; break}
default {exit}
}
ls \\10.246.16.86\D$\XMLBACKUPS\G???BC -r -fo -ea 0 |
? {$_.psIsContainer -and $_ -match $pat} |
ri -r -fo -ea 0 -vb
# -< script.ps1 >-

--
Kiron
 
Unread 26-06-2008, 09:50 AM   #9
Leo Tohill
Guest
 
Posts: n/a
RE: Getting Day of Week

As they say, "give me enough time, I'll write a shorter program".

It occured to me that your requirement for deletion could probably be
restated as "delete the folders that are more than two days old, not counting
weekends". Phrased that way, i can reduce the script to this:

if (5,6 -contains [DateTime]::Now.DayOfWeek){$minAge = 4} else {$minAge = 2}
Get-ChildItem G???BC\*|where-object {$_.PsIsContainer -and "Mon", "Tue",
"Wed", "Thu", "Fri" -contains $_.Name -and ((get-date) - $_.CreationTime) -gt
$minAge} |remove-item -recurse -force -whatif



"Leo Tohill" wrote:

> This incorporates the weekend requirement, and deals with case sensitivity.
>
> $dow = [DateTime]::Now.DayOfWeek
> if (5,6 -contains $dow) {write-host "Don't run this on weekends!";return}
> $dows = new System.Collections.Arraylist(5)
> "mon", "tue", "wed", "thu", "fri" |% {$dows.Add($_)}
> if ($dow -eq 1) {$dows.Remove("thu");$dows.Remove("fri")}
> elseif ($dow -eq 2) {$dows.Remove("fri");$dows.Remove("mon")}
> else
> {
> $dows.Remove([DateTime]::Now.DayOfWeek.ToString().SubString(0,3).ToLower( ))
> $dows.Remove([DateTime]::Now.AddDays(-1).DayOfWeek.ToString().SubString(0,3).ToLower())
> }
> Get-ChildItem G???BC\*|where-object {$_.PsIsContainer -and
> $dows.Contains($_.Name.ToLower())}|remove-item -recurse -force -whatif
>
>

 
Unread 26-06-2008, 09:50 AM   #10
Leo Tohill
Guest
 
Posts: n/a
RE: Getting Day of Week

Correction
if (5,6 -contains [DateTime]::Now.DayOfWeek){ ...
should be
if (1,2 -contains [DateTime]::Now.DayOfWeek){ ...




"Leo Tohill" wrote:

> As they say, "give me enough time, I'll write a shorter program".
>
> It occured to me that your requirement for deletion could probably be
> restated as "delete the folders that are more than two days old, not counting
> weekends". Phrased that way, i can reduce the script to this:
>
> if (5,6 -contains [DateTime]::Now.DayOfWeek){$minAge = 4} else {$minAge = 2}
> Get-ChildItem G???BC\*|where-object {$_.PsIsContainer -and "Mon", "Tue",
> "Wed", "Thu", "Fri" -contains $_.Name -and ((get-date) - $_.CreationTime) -gt
> $minAge} |remove-item -recurse -force -whatif
>
>
>
> "Leo Tohill" wrote:
>
> > This incorporates the weekend requirement, and deals with case sensitivity.
> >
> > $dow = [DateTime]::Now.DayOfWeek
> > if (5,6 -contains $dow) {write-host "Don't run this on weekends!";return}
> > $dows = new System.Collections.Arraylist(5)
> > "mon", "tue", "wed", "thu", "fri" |% {$dows.Add($_)}
> > if ($dow -eq 1) {$dows.Remove("thu");$dows.Remove("fri")}
> > elseif ($dow -eq 2) {$dows.Remove("fri");$dows.Remove("mon")}
> > else
> > {
> > $dows.Remove([DateTime]::Now.DayOfWeek.ToString().SubString(0,3).ToLower( ))
> > $dows.Remove([DateTime]::Now.AddDays(-1).DayOfWeek.ToString().SubString(0,3).ToLower())
> > }
> > Get-ChildItem G???BC\*|where-object {$_.PsIsContainer -and
> > $dows.Contains($_.Name.ToLower())}|remove-item -recurse -force -whatif
> >
> >

 
 

Thread Tools
Display Modes



< Home - Windows Help - MS Office Help - Hardware Support >


New To Site? Need Help?

All times are GMT. The time now is 10:07 PM.


vBulletin, Copyright ©2000 - 2014, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO
Copyright © 2005-2013, TechTalkz.com. All Rights Reserved - Privacy Policy
Valid XHTML 1.0 Transitional