Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

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

Posted on 2010-08-13
5
Medium Priority
?
914 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 1000 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

Tech or Treat!

Submit an article about your scariest tech experience—and the solution—and you’ll be automatically entered to win one of 4 fantastic tech gadgets.

Question has a verified solution.

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

In previous parts of this Nano Server deployment series, we learned how to create, deploy and configure Nano Server as a Hyper-V host. In this part, we will look for a clustering option. We will create a Hyper-V cluster of 3 Nano Server host nodes w…
I was prompted to write this article after the recent World-Wide Ransomware outbreak. For years now, System Administrators around the world have used the excuse of "Waiting a Bit" before applying Security Patch Updates. This type of reasoning to me …
This tutorial will give a an overview on how to deploy remote agents in Backup Exec 2012 to new servers. Click on the Backup Exec button in the upper left corner. From here, are global settings for the application such as connecting to a remote Back…
This tutorial will walk an individual through setting the global and backup job media overwrite and protection periods in Backup Exec 2012. Log onto the Backup Exec Central Administration Server. Examine the services. If all or most of them are stop…

618 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