Solved

Windows Server 2008: Cmd or Powershell to create directory with todays date and copy directory?

Posted on 2010-08-13
5
898 Views
Last Modified: 2012-05-10
I used to have a script like this in Windows Server 2003 to create a directory with todays date:
mkdir c:\Backups\html_sites\html_sites_%date:~3,10%

I can't seem to get it to work in Windows Server 2008. Can anyone give me a working script to achieve the same in 2008? I don't mind whether it's a powershell script or a command line script. However if it's a powershell script, I'd appreciate if you could also tip on how to copy directories using powershell. Basically this is what I'm after:

mkdir c:\Backups\html_sites\html_sites_%date:~3,10%
xcopy /e /q /y C:\html_sites C:\Backups\html_sites\html_sites_%date:~3,10%\
0
Comment
Question by:Synocus
  • 3
5 Comments
 
LVL 95

Accepted Solution

by:
Lee W, MVP earned 250 total points
ID: 33427465
the / character is reserved - no idea how you had that working in 2003 - it shouldn't work there either.

I would use %date:~-4%-%date:~4,2%-%date:~7,2%
 That would result in a YYY-MM-DD output
0
 

Author Comment

by:Synocus
ID: 33427493
Well, the environment is Finnish, so it's d.M.yyyy over here.

Problem is, this in script (mkdir c:\Backups\html_sites\html_sites_%date:~3,10%) produces this:

mkdir c:\Backups\html_sites\html_sites_ 13.08.201

First problem is the extra space. Also the date format is slightly odd, how can I format it to YYYY.MM.DD?
0
 

Author Comment

by:Synocus
ID: 33427509
Nevermind that what you provided was just fine.
0
 
LVL 13

Expert Comment

by:soostibi
ID: 33427648
PowerShell solution, just for your interest:

New-Item -ItemType directory -Name "html_sites_$(get-date -format 'yyyy.MM.dd')" -Path c:\Backups\html_sites
Get-ChildItem C:\html_sites | Copy-Item -Recurse -Destination "c:\Backups\html_sites\html_sites_$(get-date -format 'yyyy.MM.dd')"
0
 

Author Comment

by:Synocus
ID: 33428213
Thanks soostibi
0

Featured Post

Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

Question has a verified solution.

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

Sometimes drives fill up and we don't know why.  If you don't understand the best way to use the tools available, you may end up being stumped as to why your drive says it's not full when you have no space left!  Here's how you can find out...
"Migrate" an SMTP relay receive connector to a new server using info from an old server.
This tutorial will walk an individual through locating and launching the BEUtility application and how to execute it on the appropriate database. Log onto the server running the Backup Exec database. In a larger environment, this would generally be …
This tutorial will walk an individual through the process of transferring the five major, necessary Active Directory Roles, commonly referred to as the FSMO roles to another domain controller. Log onto the new domain controller with a user account t…

912 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

19 Experts available now in Live!

Get 1:1 Help Now