Page 1 of 1

Win 8.1 messing up dates!

Posted: 12 May 2017 20:07
by TipMark
The following is a main part of my .bat file:

Code: Select all

:: 2. Create new backup set
set bkuphour=%time:~0,2%
if "%bkuphour:~0,1%"==" " set bkuphour=0%time:~1,1%
set bkupfldr=%date:~10,4%-%date:~4,2%-%date:~7,2% %bkuphour%-%time:~3,2%

echo+

echo CREATING FOLDER %BackupTarget2%%bkupfldr%\
if not exist "%BackupTarget2%%bkupfldr%\" mkdir "%BackupTarget2%%bkupfldr%\"


echo+
echo BACKING UP FILES...

%BackupCmd2% "%BackupSource2%" "%BackupTarget2%%bkupfldr%\"

echo+
echo BACKUP COMPLETED!
echo %date% %time%

pause
exit

:noback
echo Please insert your USB key and run this once more
pause
exit


What is happening is this.
On my desktop which has WinXP the dates come out for the
folders like this:

2016-12-16 21-04

That is what I want, but when the .bat is run on Win 8.1 or Win 10.
The folders come out like this:

_ay_17_20_55 (this is supposed to be May 2017 20:55)

Any suggestions how I can fix the .bat when run on Win 8.1/10
so the dates look like:
2016-12-16 21-04 ?

Re: Win 8.1 messing up dates!

Posted: 13 May 2017 02:23
by penpen
Please post the output of the following batch file ("test.bat"):

Code: Select all

@echo off
echo "%date%"
echo "%time%"
pause


penpen

Re: Win 8.1 messing up dates!

Posted: 13 May 2017 04:21
by Compo
As long as you're not using a Home Edition of XP, you should have full support of the WMI Command line, (WMIC.exe).

You could then replace the top half of your script with this:

Code: Select all

Rem 2. Create new backup set
For /F "EOL=L" %%A In ('WMIC OS GET LocalDateTime') Do For %%B In (%%~nA
) Do Set "DTS=%%B"
Set "bkupfldr=%DTS:~,4%-%DTS:~4,2%-%DTS:~6,2% %DTS:~8,2%-%DTS:~10,2%"
If Not Exist "%BackupTarget2%%bkupfldr%\" (Echo=
   Echo=CREATING FOLDER %BackupTarget2%%bkupfldr%
   MD "%BackupTarget2%%bkupfldr%")

The first time you invoke it, it may take a short time to initialise.

The script should then work on any of your PC's without having to change anything.

Re: Win 8.1 messing up dates!

Posted: 13 May 2017 13:08
by TipMark
penpen wrote:Please post the output of the following batch file ("test.bat"):

Code: Select all

@echo off
echo "%date%"
echo "%time%"
pause


penpen


Thanks for helping!

WinXP:

"Sat 05/13/2017"
"14:59:47.06"
------------------

Win 8.1:

"05/13/17"
"15:03:52.76"

Re: Win 8.1 messing up dates!

Posted: 13 May 2017 13:53
by TipMark
Compo wrote:As long as you're not using a Home Edition of XP, you should have full support of the WMI Command line, (WMIC.exe).

You could then replace the top half of your script with this:

Code: Select all

Rem 2. Create new backup set
For /F "EOL=L" %%A In ('WMIC OS GET LocalDateTime') Do For %%B In (%%~nA
) Do Set "DTS=%%B"
Set "bkupfldr=%DTS:~,4%-%DTS:~4,2%-%DTS:~6,2% %DTS:~8,2%-%DTS:~10,2%"
If Not Exist "%BackupTarget2%%bkupfldr%\" (Echo=
   Echo=CREATING FOLDER %BackupTarget2%%bkupfldr%
   MD "%BackupTarget2%%bkupfldr%")

The first time you invoke it, it may take a short time to initialise.

The script should then work on any of your PC's without having to change anything.



Thanks very much Compo!
It works like a charm. :)

Re: Win 8.1 messing up dates!

Posted: 13 May 2017 18:54
by penpen
TipMark wrote:WinXP:

"Sat 05/13/2017"
"14:59:47.06"
------------------

Win 8.1:

"05/13/17"
"15:03:52.76"
I assume i'm too late, but just for info, you could norm the date (remove anything up to the first space (if exists), and then add "20" in front of the year if needed), before using it:

Code: Select all

set "d=%date:* =%"
if "%d:~-3,1%" == ":" set "d=%d:~0,-2%20%d:~-2%"
echo(%d:~-4%-%d:~-,2%-%d:~3,2%
(Or you could use for/f to split the string, or ... .)

I'm unsure, but could it be that your Win10 PC has a third date format (your opening post contains "_ay" which could be derived from "May", so it may be that you need additional steps to also handle that date).


penpen

Re: Win 8.1 messing up dates!

Posted: 14 May 2017 15:18
by TipMark
Thanks guys!

Something a little strange is happening now with the .bat file.

The folder name created is fine:

2017-05-14 17-09

but the date of this folder shown to the right of the folder name is:

04/30/2017 19:14

and this date is persistent!

So if I run the batch file a few time, new folders are created with the correct folder name
but always with the exact same date of 04/30/2017 19:14

I notice (using Total Commander) that these new folders are set to 'a' for their attribute.
Whereas other folders from a couple of days ago have the correct date and no 'a' attribute.

Re: Win 8.1 messing up dates!

Posted: 15 May 2017 12:20
by penpen
Without seeing the source code it is hard to tell, if your batch file is causing such symptoms, but i doubt that.
If you are are accessing a network drive, then maybe the date of that pc is wrong,
else you should create a minimized (and anonymized) script where this happens,
so we could check if the batch script is causing this.

penpen

Re: Win 8.1 messing up dates!

Posted: 15 May 2017 15:44
by TipMark
penpen wrote:Without seeing the source code it is hard to tell, if your batch file is causing such symptoms, but i doubt that.
If you are are accessing a network drive, then maybe the date of that pc is wrong,
else you should create a minimized (and anonymized) script where this happens,
so we could check if the batch script is causing this.

penpen


No network drive here.

Here's the code I'm using:

Code: Select all

@echo off

:: Start Variables

set NumberToKeep=2

set BackupCmd2=xcopy.exe
set BackupSource2=d:\Mom\Excel
set BackupTarget2=d:\Mom\Excel-Bak\

:: End Variables

echo+
echo STARTING BACKUP
echo %date% %time%

:: 1. Delete older backup set(s) beyond the NumberToKeep

for /F "tokens=* skip=%NumberToKeep%" %%I In ('dir "%BackupTarget2%" /AD /B /O-D /TW') do (
   echo+
   echo DELETING OLD BACKUP SET %BackupTarget2%%%~I
   rd /s /q "%BackupTarget2%%%~I"
)


Rem 2. Create new backup set
For /F "EOL=L" %%A In ('WMIC OS GET LocalDateTime') Do For %%B In (%%~nA
) Do Set "DTS=%%B"
Set "bkupfldr=%DTS:~,4%-%DTS:~4,2%-%DTS:~6,2% %DTS:~8,2%-%DTS:~10,2%"
If Not Exist "%BackupTarget2%%bkupfldr%\" (Echo=
   Echo=CREATING FOLDER %BackupTarget2%%bkupfldr%
   MD "%BackupTarget2%%bkupfldr%")

echo+
echo BACKING UP FILES...

%BackupCmd2% "%BackupSource2%" "%BackupTarget2%%bkupfldr%\"

exit

Re: Win 8.1 messing up dates!

Posted: 16 May 2017 12:15
by penpen
I don't get this result on newly created directories.
But (odd enough) i get this result if i overwrite an existing backup directory -
by executing the script twice within the same minute (my "test.bat"):

Code: Select all

setlocal enableExtensions disableDelayedExpansion

set "BackupCmd2=xcopy.exe"
set "BackupSource2=Z:\Neuer Ordner"
set "BackupTarget2=Z:\xml test-Bak\"
set "bkupfldr=12017-05-16 19-39"

rd /s /q "%BackupTarget2%%bkupfldr%\"
%BackupCmd2% "%BackupSource2%" "%BackupTarget2%%bkupfldr%\"
pause
echo a | %BackupCmd2% "%BackupSource2%" "%BackupTarget2%%bkupfldr%\"

endlocal
The timestamp in my tests is that of "%BackupSource2%".

In this case you could just have to remove the existing directory, or create a new one with another name
(maybe also use seconds and/or milliseconds, then it should be more unlikely to happen).

Sidenotes:
I really hope you only haven't forgotten to mention that this happens only when overwriting (i only recognized it by accident - whch took some time), else i have no idea how to avoid the older date.
It's not what i would call a "minimized example", but it is short enough.



penpen

Re: Win 8.1 messing up dates!

Posted: 16 May 2017 14:26
by TipMark
penpen wrote:I don't get this result on newly created directories.
But (odd enough) i get this result if i overwrite an existing backup directory -
by executing the script twice within the same minute (my "test.bat"):


...The timestamp in my tests is that of "%BackupSource2%".

In this case you could just have to remove the existing directory, or create a new one with another name
(maybe also use seconds and/or milliseconds, then it should be more unlikely to happen).

Sidenotes:
I really hope you only haven't forgotten to mention that this happens only when overwriting (i only recognized it by accident - whch took some time), else i have no idea how to avoid the older date.
It's not what i would call a "minimized example", but it is short enough.

penpen


Thanks again for helping out!
Much of the .bat code is beyond me. I only know the basics.

No, this doesn't just happen when overwriting.
It happens all the time.
If I wait a minute or more, a new folder is created with the correct new folder name (with the correct date and time).

But as I mentioned, if I look to the right, I see an actual date for the folder of 04/30/2017 19:14 a
I don't know where the .bat file is getting this information?

And for an experiment, I just now created a NEW 'BackupTarget2' folder (Mom\Excel-Bak2).
But it didn't make a difference! The new folder created still had a date of 04/30/2017 19:14 a

It's not too important though, because I can ignore this date and just look at the date of the folder name. :)