sm86
sm86

Reputation: 87

Create new folder with current date bat file

I am trying to create a new folder on my 'C' drive but every time I run it, the folder is created on my desktop. Am I putting the "C:\SQL\test" in the wrong spot or do I need to add another command? Thanks

Creates folder with todays date on Desktop:

mkdir %date:~-4,4%%date:~-10,2%%date:~7,2%

Location I'm trying to create a folder with todays date:

mkdir C:\SQL\test  %date:~-4,4%%date:~-10,2%%date:~7,2% 

Upvotes: 0

Views: 959

Answers (2)

user7818749
user7818749

Reputation:

Your problem is whitespace in the path. each space is seen as a separator to cmd between commands and arguments. You therefore need to enclose it in double quotes. i.e mkdir "C:\path with spaces"

for almost the same reason, if you wanted the below instead:

"C:\SQL\test\yyyymmdd"

then the below commands should be mkdir "c:\SQL\test\%%~i"

The %date% environment variable is not the best option to use. Running on a different PC with different regional settings will result in the incorrect output. Incorporate some `powershell.

From cmd

@for /f "tokens=1* delims=-" %i in ('PowerShell -Command "& {Get-Date -format "yyyyMMdd"}"') do mkdir "C:\SQL\test %i~"

within batch-file

@echo off
for /f "tokens=1* delims=-" %%i in ('PowerShell -Command "& {Get-Date -format "yyyyMMdd"}"') do mkdir "C:\SQL\test %%~i"

PS!! you can do this in standalone powershell alone in a .ps1 file:

$var = Get-Date -format "yyyyMMdd"
md "c:\SQL\test\$var"

Upvotes: 1

sm86
sm86

Reputation: 87

This worked:

mkdir "C:\SQL\test\%date:~-4,4%%date:~-10,2%%date:~7,2%"

Upvotes: 0

Related Questions