Solved

Powershell script to create folder with time stamp.

Posted on 2011-09-30
6
1,264 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

A brief introduction to what I consider to be the best editor for PowerShell.
In threads here at EE, each comment has a unique Identifier (ID). It is easy to get the full path for an ID via the right-click context menu. However, we often want to post a short link within a thread rather than the full link. This article shows a…
The viewer will learn how to count occurrences of each item in an array.
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…

756 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