Solved

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

Posted on 2010-08-13
5
905 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
[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
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

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!

Question has a verified solution.

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

A safe way to clean winsxs folder from your windows server 2008 R2 editions
This article explains how to install and use the NTBackup utility that comes with Windows Server.
This tutorial will walk an individual through the steps necessary to join and promote the first Windows Server 2012 domain controller into an Active Directory environment running on Windows Server 2008. Determine the location of the FSMO roles by lo…
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …

733 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