Windows Vista Forums
Vista Forums Home Join Vista Forums Windows 7 Forum Vista Tutorials Tags
Welcome to Windows Vista Forums. Our forum is dedicated to helping you find solutions with any problems, errors or issues you are experiencing with Windows Vista. The Vista forum also covers news and updates and has an extensive Windows Vista tutorial section that covers a wide range of tips and tricks.

Go Back   Vista Forums > Misc Newsgroups > PowerShell

Vista - need help, powershell

Reply
 
Old 10-25-2008   #1 (permalink)


vista 64
 
 

need help, powershell

Hi guys,
I need some urgent help to automate a task using powershell. I have tens of thousands of files that need to be re-arranged on daily basis. They structured:

- folder "top"
-----file 1
-----file 2
-----file 3
...
-----sub-folder "2008-10-21"
-----sub-folder "2008-10-22"
-----sub-folder "2008-10-23"
...

I need to
a). check if sub-folder named by today's date (in the format YYYY-MM-DD) exists, if not - create it.
b). move all files from the folder "top" that were *created today* into the new sub-folder.

I know programming a bit but this is too advance for me!
Help is appreciated and karma points are awaiting to be sent.

TIA
Alisa

My System SpecsSystem Spec
Old 10-26-2008   #2 (permalink)
Shay Levy [MVP]


 
 

Re: need help, powershell


$rootFolder = "d:\top"
$date = Get-Date
$newDirName = Join-Path $rootFolder ($date -f yyyy-MM-dd)
$null = new-item -path $newDirName -type container -force

dir $rootFolder | where {!$_.PSIsContainer -AND ($_.creationTime.date -eq
$date.date) } | move-item -dest $newDirName



---
Shay Levy
Windows PowerShell MVP
http://blogs.microsoft.co.il/blogs/ScriptFanatic
PowerShell Toolbar: http://tinyurl.com/PSToolbar


l> Hi guys,
l> I need some urgent help to automate a task using powershell. I have
l> tens of thousands of files that need to be re-arranged on daily
l> basis.
l> They structured:
l> - folder "top"
l> -----file 1
l> -----file 2
l> -----file 3
l> ..
l> -----sub-folder "2008-10-21"
l> -----sub-folder "2008-10-22"
l> -----sub-folder "2008-10-23"
l> ..
l> I need to
l> a). check if sub-folder named by today's date (in the format
l> YYYY-MM-DD) exists, if not - create it.
l> b). move all files from the folder "top" that were *created today*
l> into the new sub-folder.
l> I know programming a bit but this is too advance for me! Help is
l> appreciated and karma points are awaiting to be sent.
l>
l> TIA
l> Alisa


My System SpecsSystem Spec
Old 10-26-2008   #3 (permalink)


Vista Ultimate x64
 
 

Re: need help, powershell

How about this:

param($topfolder = ".")

$today = ([datetime]::now)
$dirname = $today.ToString("yyyy-MM-dd")

function isToday ([datetime]$date)
{
$today.Date -eq $date.Date
}


if (! (Test-Path (Join-Path $topfolder $dirname)) )
{
New-item -type Directory (Join-Path $topfolder $dirname) | Out-Null
}

dir $topfolder -exclude $dirname | Where-Object { isToday($_.creationtime) } | move-Item -Destination (Join-Path $topfolder $dirname)
My System SpecsSystem Spec
Old 10-26-2008   #4 (permalink)
Tao Ma


 
 

RE: need help, powershell

Hi lizardino,

My codes will create directory if necessary:

$dir = 'd:\top'
$files = dir -path $dir | ?{ !$_.PSIsContainer }
$files | %{ [void] (New-Item -ItemType Directory `
$_.CreationTime.ToString("yyyy-MM-dd") -ErrorAction SilentlyContinue);
$_ }`
| %{ Move-Item $_.PSPath -dest $_.CreationTime.ToString("yyyy-MM-dd") }

It will collect all of files under the top directory. Create the directory
and move the file to the correctly directory one by one.

Best regards,
Tao Ma
My System SpecsSystem Spec
Old 10-30-2008   #5 (permalink)


vista 64
 
 

Re: need help, powershell

thank you guys so much!
My System SpecsSystem Spec
Reply

Thread Tools


Similar Threads
Thread Forum
Installing PowerShell dependent features on W2K8 with PowerShell CTP PowerShell
when run powershell script as windows service ,powershell fail PowerShell
Powershell Plus - Free for non commercial Use and Powershell Analyzer1.0 released PowerShell
Automatic PowerShell Error Parsing in PowerShell Analyzer and PowerShellPlus PowerShell
PowerShell Leaders Join Forces and offer a pre-release version of PowerShell for 50% off the retail value PowerShell


Vista Forums is an independent web site and has not been authorized,
sponsored, or otherwise approved by Microsoft Corporation.
"Windows Vista", the Start Orb, and related materials are trademarks of Microsoft Corp.
© Designer Media Ltd

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46