Solved

Powershell script to create folder with time stamp.

Posted on 2011-09-30
6
1,245 Views
Last Modified: 2012-05-12
Hi,

I need to write a powershell script that will create a folder with today’s time stamp and move all the files in the directory to the folder that has just been created with the time stamp.

Thanks

Luke  
0
Comment
Question by:luketr
  • 3
  • 2
6 Comments
 
LVL 27

Expert Comment

by:KenMcF
ID: 36891011
To create the folder you can use this

New-Item "C:\temp\$(get-date -f yyyyMMdd)" -type directory

To move the file
$1Day = (get-date).adddays(-1)
((get-date $1day -f yyyyMMdd))

Get-ChildItem c:\temp | %{
    Where {(get-date $_.lastwritetime -f yyyyMMdd) -eq (get-date $1day -f yyyyMMdd)} | 
        Move-Item "C:\temp\$(get-date -f yyyyMMdd)"

}

Open in new window

0
 
LVL 69

Expert Comment

by:Qlemo
ID: 36891132
Line 2 above is for debugging only, and can be removed. Besides that, the code is not working that way, the foreach-object (%) is wrong here, but should work if  %{  and the closing } are removed.
For my taste there is too much of get-date above - that might not perform well with a lot of files.
$src = C:\Temp
$target = C:\Temp\(get-date).ToString("yyyyMMdd")
$yesterday = (get-date).adddays(-1).ToString("yyyyMMdd")
gci $src | ? {$_.lastwritetime.ToString("yyyyMMdd") -eq $yesterday} | move-item $target

Open in new window

0
 

Author Comment

by:luketr
ID: 36909742
When i try and run the script i get the following error.


PS C:\Documents and Settings\dipadmin> $src = C:\Temp
The term 'C:\Temp' is not recognized as a cmdlet, function, operable program, or script file. Verify the term and try a
gain.
At line:1 char:14
+ $src = C:\Temp <<<<
PS C:\Documents and Settings\dipadmin> $target = C:\Temp\(get-date).ToString("yyyyMMdd")
The term 'C:\Temp\' is not recognized as a cmdlet, function, operable program, or script file. Verify the term and try
again.
At line:1 char:19
+ $target = C:\Temp\( <<<< get-date).ToString("yyyyMMdd")
PS C:\Documents and Settings\dipadmin> $yesterday = (get-date).adddays(-1).ToString("yyyyMMdd")
PS C:\Documents and Settings\dipadmin> gci $src | ? {$_.lastwritetime.ToString("yyyyMMdd") -eq $yesterday} | move-item $
target
0
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 
LVL 69

Expert Comment

by:Qlemo
ID: 36909994
Sorry, the first line needs to sound:
$src = "C:\Temp"
0
 

Author Comment

by:luketr
ID: 36910211
it's erroring on the 2nd line

$target = "C:\Temp\"(get-date).ToString("yyyyMMdd")

The term 'C:\Temp\' is not recognized as a cmdlet, function, operable program, or script file. Verify the term and try
again.
At line:1 char:19
+ $target = C:\Temp\( <<<< get-date).ToString("yyyyMMdd")
0
 
LVL 69

Accepted Solution

by:
Qlemo earned 500 total points
ID: 36910269
Strange. How did that get there??? I'm certain I have tested the script. Has to be cut & waste error.
$src = "C:\Temp"
$target = "C:\Temp\" + (get-date).ToString("yyyyMMdd")
$yesterday = (get-date).adddays(-1).ToString("yyyyMMdd")
gci $src | ? {$_.lastwritetime.ToString("yyyyMMdd") -eq $yesterday} | move-item $target

Open in new window

0

Featured Post

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

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

I thought I'd write this up for anyone who has a request to create an anonymous whistle-blower-type submission form created using SharePoint 2010 (this would probably work the same for 2013). It's not 100% fool-proof but it's as close as you can get…
"Migrate" an SMTP relay receive connector to a new server using info from an old server.
Learn the basics of while and for loops in Python.  while loops are used for testing while, or until, a condition is met: The structure of a while loop is as follows:     while <condition>:         do something         repeate: The break statement m…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…

825 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