Solved

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

Posted on 2010-08-13
5
895 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
Comment Utility
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
Comment Utility
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
Comment Utility
Nevermind that what you provided was just fine.
0
 
LVL 13

Expert Comment

by:soostibi
Comment Utility
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
Comment Utility
Thanks soostibi
0

Featured Post

Too many email signature changes to deal with?

Are you constantly being asked to update your organization's email signatures? Do they take up too much of your time? Wouldn't you love to be able to manage all signatures from one central location, easily design them and deploy them quickly to users. Well, you can!

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 …
To efficiently enable the rotation of USB drives for backups, storage pools need to be created. This way no matter which USB drive is installed, the backups will successfully write without any administrative intervention. Multiple USB devices need t…
This tutorial will walk an individual through the steps necessary to install and configure the Windows Server Backup Utility. Directly connect an external storage device such as a USB drive, or CD\DVD burner: If the device is a USB drive, ensure i…

744 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

16 Experts available now in Live!

Get 1:1 Help Now