Solved

Powershell script to create folder with time stamp.

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

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
Edgartown IT Case Study

Learn about Edgartown's quest to ensure the safety and security of the entire town's employee and citizen data. Read the case study!

 
LVL 70

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 70

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

DevOps Toolchain Recommendations

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

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.
A recent project that involved parsing Tableau Desktop and Server log files to extract reusable user queries for use in other systems. I chose to use PowerShell to gather the data, and SharePoint to present it...
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…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

707 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