3

First time making a bat file. I complied this code and on my pc it make a folder with the date stamp in a backup folder.

Works great on my pc but when i transfer it to another computer I get Syntax error. And no folder get created.

I think it has to do with the mkdir c:\backup\%DATE%

Please see the code below

@echo off
cls
echo Date format = %DATE%
echo dd = %date:~0,2%
echo mm = %date:~3,2%
echo yyyy = %date:~6,4%
echo.

mkdir c:\backup\%DATE%

Please all any help would be greatly appreciated. Regard Kenneth

fthiella
  • 48,073
  • 15
  • 90
  • 106
  • Do you get *any* output from the script? What version of Windows does your machine, and the other machine run? Try removing individual lines from the batch script until the error goes away - then you will know the line that is causing you problems. – RB. Mar 02 '16 at 13:30
  • 2
    Saying *I get a syntax error* is absolutely meaningless unless you also include the specific text of the error message you're seeing. You have it right in front of you on the screen, so there's absolutely no reason you can't include it in your post for us. You're asking for *free help* to solve *your problem*; you should make it as easy as possible for us to provide it by giving us the information you already have. – Ken White Mar 02 '16 at 13:41
  • As you think that the problem is in the line `mkdir c:\backup\%DATE%`, did you tried to echo it `echo mkdir c:\backup\%DATE%`? – jeb Mar 02 '16 at 13:41
  • Thanks for the reply. i have hone that using pause by every command line. I get the Syntax error using mkdir c:\backup\%DATE% and even if i change it to c:\%DATE% still have the Syntax error. And no folder create. – Kenneth O'Donnell Mar 02 '16 at 13:44
  • thanks will try it out now – Kenneth O'Donnell Mar 02 '16 at 13:45
  • @KennethO'Donnell If you've been through it using `PAUSE` then you must know what the value of `%DATE%` is! Please let us know (e.g. what printed for the line `echo Date format = %DATE%`) - we are not psychic! – RB. Mar 02 '16 at 13:47
  • may be it will be useful for you to check [this](http://stackoverflow.com/a/19799236/388389) – npocmaka Mar 02 '16 at 14:11

1 Answers1

3

Your date format has slashes in it, which are not allowed in directory names. On my system, for instance, echo %DATE% produces 03/02/2016, which is not a legal directory name on Windows.

Use something like this instead:

SET Today=%Date:~6,4%%Date:~0,2%%Date:~3,2%
echo %Today%              

The above produces 20160302 with my time format settings.

You can then use mkdir c:\backup\%Today%, which will create the c:\backup\20160302 folder if run now with my date format settings.

Adjust the values around the ~ as needed to match the date format on your system.

Ken White
  • 123,280
  • 14
  • 225
  • 444