![]() |
![]() | ![]() | ![]() | ![]() | ![]() | ![]() | ![]() |
| 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. |
| |||||||
![]() |
| |
| | #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 Specs![]() |
| | #2 (permalink) |
| | 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 Specs![]() |
| | #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 Specs![]() |
| | #4 (permalink) |
| | 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 Specs![]() |
| | #5 (permalink) |
| vista 64 | Re: need help, powershell thank you guys so much! |
My System Specs![]() |
![]() |
| Thread Tools | |
| |