question

Tim avatar image
Tim asked

Moving folders

A project I am working on is going to require me to move folders based on a certain date from one folder to another. The first six characters of the folder name contain the date. What I am going to need to do is specify that any folders with a date less than today move to another location. An example of a folder name is 071311080901_35P1CTN I know in SSIS there is a FOR EACH LOOP container but I don't have any experience with using it. I know there is a file enumerator but there isn't one for folders. Before I spend countless hours or possibly days trying to engineer this process I thought I would seek out advice and guidance from the community on how best to approach this. A bit more about the project and why this is even my responsibility as the DBA has to do with the fact that the movement of the folders has to occur and a data load. The folders can only move once a dataset is built from our outsourced processor. I decided to write a blog about this since I was so excited to finally adopt PowerShell and to write my first script. [Here is the link for "My First PowerShell Script to Move Folders"]( http://timradney.com/2011/07/15/my-first-powershell-script-to-move-folders/)
sql-server-2008ssis
3 comments
10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

Blackhawk-17 avatar image Blackhawk-17 commented ·
I think you may prefer VBscript or PowerShell to do the heavy lifting.
1 Like 1 ·
Tim avatar image Tim commented ·
Ok, so doing a little digging in PowerShell I have found the majority of what I need. Once I change directory into the source folder, I can load the folders into an "array". Good starting point if I need it. The real power is in the Move-Item cmdlet. Just using 'Move-Item c:\\\Posh\\\Branch\\\071411* C:\\\Posh\\\Moved' moves all the folders that begin with '071411' and works great. What I have to look up now is how to get getdate()-1 in PoSH and in the format I need. I can then pass that into the 'Move-Item' Cmdlet and my problem is solved. (at least I am hoping it is solved)
1 Like 1 ·
Tim avatar image Tim commented ·
I know both of those can accomplish the task however I don't know VBscript or PowerShell that well. I have plenty of contacts in PowerShell and can get them to help out but was really hoping there is a solution that myself and my team can grasp and support. Personally this could be that project that forces me to learn PowerShell.
0 Likes 0 ·
Tim avatar image
Tim answered
So here is my solution. $yest = (get-date).AddDays(-1).ToString('MMddyy') Move-Item c:\\\Posh\\\Branch\\\$yest* c:\\\Posh\\\Moved I am truly amazed that Posh can handle all of this in two freaking lines of code. I do believe we have a new Posh addict. Aaron Nelson will be proud.
2 comments
10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

Blackhawk-17 avatar image Blackhawk-17 commented ·
Once you start digging you will be even more amazed. Enjoy! There are actually contests to perform a set of tasks in the fewest lines/characters possible.
0 Likes 0 ·
Shawn_Melton avatar image Shawn_Melton commented ·
I subscribe to PowerShell.com Tip of the Day and you get little snippets every so often that get you thinking more about what you can do with PowerShell.
0 Likes 0 ·
Blackhawk-17 avatar image
Blackhawk-17 answered
One hint :) get-date -uformat "%m%d%y" and new-TimeSpan
4 comments
10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

Tim avatar image Tim commented ·
Got it. (get-date).AddDays(-1).ToString('MMddyy')
1 Like 1 ·
Blackhawk-17 avatar image Blackhawk-17 commented ·
Now - wasn't that more fun than having it handed to you? ;) PS v1.0 wouldn't work with that though.
1 Like 1 ·
Tim avatar image Tim commented ·
Right now I have $yest = (get-date).AddDays(-1) $yeststring = $yest.ToString("MMddyy") Then I call Move-Item c:\\\Posh\\\Branch\\\$yeststring* C:\\\Posh\\\Moved This works for me but I am looking to see if I can put the date into a variable in the MMddyy format in a single call rather than two lines of code like I have above. This has been fun building my first Posh Script.
0 Likes 0 ·
Tim avatar image Tim commented ·
Absolutely. The best way to learn is to figure it out on your own. I made a task list of the individual items that would need to be done in order to accomplish the task then sought out the powershell logic to make it happen. In the end I didn't need the steps that I thought I would. Now it is time to start building on the wee bit of knowledge I picked up today. :)
0 Likes 0 ·

Write an Answer

Hint: Notify or tag a user in this post by typing @username.

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.