Solved

Powershell script to create folder with time stamp.

Posted on 2011-09-30
6
1,225 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 68

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
Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

 
LVL 68

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 68

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

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

How to sign a powershell script so you can prevent tampering, and only allow users to run authorised Powershell scripts
This article explains how to prepare an HTML email signature template file containing dynamic placeholders for users' Azure AD data. Furthermore, it explains how to use this file to remotely set up a department-wide email signature policy in Office …
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…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

743 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now