Search code examples
batch-filewindows-7backupdatestamp

Automatic date stamp backup bat file changes stamp by itself after second loop


So I've been trying to make a automatic backup and date stamp bat program for a folder and its contents. The first time it loops it does exactly what i want it to. But the second time the loop runs, it changes the folder by removing the first 3 numbers and the 0 in 2014. It looks like this.

First loop C:\users\username\desktop\05.26.2014\17.11\contents(This is right)

Second loop C:\user\username\desktop\6.2.14\17\contents

Third loop C:\users\username\desktop\2.1\no time folder\contents

There is a time sub folder in the date folder it is also affected by this until it does not generate anymore. Can anyone tell what is causing this, here is what i have in the bat file

@echo off
set /a x=0

:loop1



timeout /t 600

set day="%date:~-10,2%"
set month="%date:~-7,2%"
set year="%date:~-4,4%"
set hour="%time:~-11,2%"
set minute="%time:~-8,2%"

set time="%hour%.%minute%"

set date="%day%.%month%.%year%"

echo d | XCOPY Z:\copydirectory "G:\pastdirectory" /e

echo Loop number -^>%x%

set /a x=%x%+1

if %x% NEQ 10000 goto loop1

pause

Thanks to anyone who answers.

Edit: changed

variable time to T and variable date to D

That seems to have fixed it.


Solution

  • You should not use " in your set statements. This will put the double-quotes into the actual result. Assuming that your first values were parsed correctly, when you next construct date, the result will be:

    ""26"."05"."2014""
    

    Then, next time "%date:~-4,4%" will give you "14""".

    Remove all the quotes from set statements and try again. If you still have issues, you may need to look into delayed variable expansion. Check out the setlocal and endlocal commands.