Solved

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

Posted on 2010-08-13
5
907 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 96

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

Technology Partners: 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

Background Information Recently I have fixed file server permission issues for one of my client. The client has 1800 users and one Windows Server 2008 R2 domain joined file server with 12 TB of data, 250+ shared folders and the folder structure i…
A quick Powershell script I wrote to find old program installations and check versions of a specific file across the network.
This tutorial will show how to push an installation of Backup Exec to an additional server in both 2012 and 2014 versions of the software. Click on the Backup Exec button in the upper left corner. From here, select Installation and Licensing, then I…
This tutorial will show how to configure a single USB drive with a separate folder for each day of the week. This will allow each of the backups to be kept separate preventing the previous day’s backup from being overwritten. The USB drive must be s…

705 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