Batch Backup file for Vista Help

Microsoft Windows vista home premium w/s...
January 6, 2010 at 08:11:54
Specs: Windows Vista
Hi, I while ago, someone wrote me a code in order to back up a certain directory into time and date stamped folders that would delete folders over a certain number of days old. Basically, it worked brilliantly in xp, but now on vista, it just flashes a black command prompt like window and does nothing else. I was hoping someone would be able to help me get it working. Thank you.

Here it is:

@Echo off
Setlocal EnableDelayedExpansion
:: variables
Call :GetDate
Call :GetTime
Set _yy=%_fDate:~,4%
Set _mm=%_fDate:~4,2%
Set _dd=%_fDate:~6,2%
Set _hh=%_hhmm:~0,2%
Set _min=%_hhmm:~2,2%
Set _minYear=2008
:: Set this to the number of days of backups you want to keep
Set _DaysKept=14
:: This variable is set to the backup folder. Dated folders will be created under it
Set _backupto=C:\Documents and Settings\Alex Hall\My Documents\~ Back Up ~\SanDisk Cruzer
Set _drive=%_backupto%\%_yy%-%_mm%-%_dd% %_hh%-%_min%
If NOT EXIST "%_drive%" Md "%_drive%"
::
:: Specify the needed switches for Xcopy here
Set _backupcmd=xcopy /cehiryq
:: This variable is set to the parent folder of the folder you wish to backup
:: Example, to backup My Documents\SchoolWork, set this to %userprofile%\My Documents\
Set _backupfrom=G:\
Set _Source=~ School ~
Echo ### Backing up %_backupfrom%%_Source% to %_drive%\%_Source%
%_backupcmd% "%_backupfrom%%_Source%" "%_drive%\%_Source%"
:: use below syntax to backup other directories..., change _backupfrom if needed
:: Set _Source="folder to backup"
:: %_backupcmd% "%_backupfrom%%_Source%" ""%_drive%\%_Source%"
::
:: Now remove old folders.
:_Prompt
Set _s=s
If %_DaysKept%==1 set _s=
Set /p _resp=Do you wish to delete backup folders more than %_DaysKept% day%_s% old (Y/N)? 
If /I "%_resp:~0,1%"=="N" Goto _Done
If /I NOT "%_resp:~0,1%"=="Y" (Echo Y/N only please)&Goto _Prompt
Echo Please wait, searching for folders more than %_DaysKept% day%_s% old
If Exist "%temp%\tf}1{" Del "%temp%\tf}1{"
:: Convert todays date to Julian
Call :JDate %_yy% %_mm% %_dd%
Set _JToday=%_JDate%
Set /a _DelDate=_JToday-%_DaysKept%-1
:: This section will remove all folders that are "%_DaysKept%" days old and older
For /F "tokens=1-5 delims=- " %%I In ('dir /ad /b /on "%_backupto%"') Do (
 Set _Valid=1
 Echo %%I%%J%%K%%L%%M|Findstr /R "[^0-9]">Nul
 If !Errorlevel!==0 Set _Valid=0
 If 1%%I GTR 19999 Set _Valid=0
 If %%I LSS %_minYear% Set _Valid=0
 If %%I GTR 9999 Set _Valid=0
 If %%J LSS 1 Set _Valid=0
 If %%J GTR 12 Set _Valid=0
 If 1%%J GTR 112 Set _Valid=0
 If %%K LSS 1 Set _Valid=0
 If %%K GTR 31 Set _Valid=0
 If 1%%K GTR 132 Set _Valid=0
 If %%L LSS 0 Set _Valid=0
 If %%L GTR 23 Set _Valid=0
 If 1%%L GTR 123 Set _Valid=0
 If %%M LSS 0 Set _Valid=0
 If %%M GTR 59 Set _Valid=0
 If 1%%M GTR 159 Set _Valid=0
 If !_Valid!==1 Call :JDate %%I %%J %%K
 If !_Valid!==1 If !_JDate! LEQ %_DelDate% If Exist "%_backupto%\%%I-%%J-%%K %%L-%%M" (
    If Exist "%temp%\tf}1{" (
     Echo %_backupto%\%%I-%%J-%%K %%L-%%M>>"%temp%\tf}1{"
     ) Else (
     Echo.>"%temp%\tf}1{"
     Echo Do you wish to delete the following folders?>>"%temp%\tf}1{"
     Echo %_backupto%\%%I-%%J-%%K %%L-%%M>>"%temp%\tf}1{"
    ))
 )
If Not Exist "%temp%\tf}1{" Echo No folders Found&Goto _Done
Type "%temp%\tf}1{" | More
Set _rdflag= /q
:_Prompt1
Set /P _resp=Delete All, None, or Prompt for each (A/N/P)?
If /I "%_resp:~0,1%"=="N" Goto _Done
If /I "%_resp:~0,1%"=="A" Goto _Removeold
If /I NOT "%_resp:~0,1%"=="P" (Echo (A/N/P only please)&Goto _Prompt1
Set _rdflag=
:_Removeold
For /F "tokens=* skip=2" %%I In ('type "%temp%\tf}1{"') Do (
 If "%_rdflag%"=="" Echo Deleting
 rd /s%_rdflag% "%%I")
:_Done
Echo Backup Complete!
::@pause
:: Cleanup
If Exist "%temp%\tf}1{" Del "%temp%\tf}1{"
Goto:EOF
::===================================::
::                                         ::
::   -   S u b r o u t i n e s   -   ::
::                                         ::
::===================================::
:JDate
:: Convert date to Julian
:: Arguments : YYYY MM DD
:: Returns   : Julian date in variable _JDate
:: Usage
::Call :JDate %__GYear% %_GMonth% %_GDay%
:: First strip leading zeroes; a logical error in this
:: routine was corrected with help from Alexander Shapiro
::Code taken from datediff.bat written by Rob van der Woude
::http://www.robvanderwoude.com
Set _JMM=%2
Set _JDD=%3
IF 1%_JMM% LSS 110 Set _JMM=%_JMM:~1%
IF 1%_JDD% LSS 110 Set _JDD=%_JDD:~1%
::
:: Algorithm based on Fliegel-Van Flandern
:: algorithm from the Astronomical Almanac,
:: provided by Doctor Fenton on the Math Forum
:: (http://mathforum.org/library/drmath/view/51907.html),
:: and converted to batch code by Ron Bakowski.
Set /A _JMonth1 = ( %_JMM% - 14 ) / 12
Set /A _JYear1  = %1 + 4800
Set /A _JDate  = 1461 * ( %_JYear1% + %_JMonth1% ) / 4 + 367 * ( %_JMM% - 2 -12 * %_JMonth1% ) / 12 - ( 3 * ( ( %_JYear1% + %_JMonth1% + 100 ) / 100 ) ) / 4 + %_JDD% - 32075
For %%A In (_JMonth1 _JYear1) Do Set %%A=
Goto:EOF
:GDate
:: Convert Julian date back to "normal" Gregorian date
:: Argument : Julian date
:: Returns  : YYYY-MM-DD in _GDate
::
:: Call :Gdate %var%
:: Algorithm based on Fliegel-Van Flandern
:: algorithm from the Astronomical Almanac,
:: provided by Doctor Fenton on the Math Forum
:: (http://mathforum.org/library/drmath/view/51907.html),
:: and converted to batch code by Ron Bakowski.
::
Set /A P      = %1 + 68569
Set /A Q      = 4 * %P% / 146097
Set /A R      = %P% - ( 146097 * %Q% +3 ) / 4
Set /A S      = 4000 * ( %R% + 1 ) / 1461001
Set /A T      = %R% - 1461 * %S% / 4 + 31
Set /A U      = 80 * %T% / 2447
Set /A V      = %U% / 11
Set /A __GYear  = 100 * ( %Q% - 49 ) + %S% + %V%
Set /A _GMonth = %U% + 2 - 12 * %V%
Set /A _GDay   = %T% - 2447 * %U% / 80
:: Clean up the mess
For %%A In (P Q R S T U V) Do Set %%A=
:: Add leading zeroes
IF 1%_GMonth% LSS 20 Set _GMonth=0%_GMonth%
IF 1%_GDay%   LSS 20 Set _GDay=0%_GDay%
:: Return value
Set _GDate=%__GYear%-%_GMonth%-%_GDay%
Goto:EOF
:GetDate
:: This subroutine will always display the same results,
:: for the date independent of "International" settings.
:: This batch file uses REG.EXE from the NT Resource Kit
:: (already installed with WinXP and Vista)
:: to read the "International" settings from the registry.
:: Date is returned as yyyymmdd in variable _fdate
:: Modified from SortDate Written by Rob van der Woude
:: <a href="http://www.robvanderwoude.com" target="_blank">http://www.robvanderwoude.com</a>
::
If "%date%A" LSS "A" (Set _NumTok=1-3) Else (Set _NumTok=2-4)
:: Delims= is a TAB followed by a space in the next two lines
:: If you copy this code, you must edit these two lines
For /F "SKIP=3 TOKENS=2* DELIMS=	 " %%A In ('REG QUERY "HKCU\Control Panel\International" /v iDate') Do Set _iDate=%%B
For /F "SKIP=3 TOKENS=2* DELIMS=	 " %%A In ('REG QUERY "HKCU\Control Panel\International" /v sDate') Do Set _sDate=%%B
IF %_iDate%==0 For /F "TOKENS=%_NumTok% DELIMS=%_sDate% " %%B In ("%date%") Do Set _fdate=%%D%%B%%C
IF %_iDate%==1 For /F "TOKENS=%_NumTok% DELIMS=%_sDate% " %%B In ("%date%") Do Set _fdate=%%D%%C%%B
IF %_iDate%==2 For /F "TOKENS=%_NumTok% DELIMS=%_sDate% " %%B In ("%date%") Do Set _fdate=%%B%%C%%D
Goto:EOF
:GetTime
:: This batch file will always display the same results,
:: independent of "International" settings.
:: This batch file uses REG.EXE from the NT Resource Kit
:: (already installed with WinXP and Vista)
:: to read the "International" settings from the registry.
:: Time is returned in 24 hour format as hhmm in variable _hhmm
:: Modified from SortTime Written by Rob van der Woude
:: <a href="http://www.robvanderwoude.com" target="_blank">http://www.robvanderwoude.com</a>
::
For /F "TOKENS=*" %%A In ('TIME/T') Do Set _Time=%%A
:: Delims= is a TAB followed by a space in the next two lines
:: If you copy this code, you must edit these two lines
For /F "TOKENS=2* DELIMS=	 " %%A In ('REG QUERY "HKCU\Control Panel\International" /v iTime') Do Set _iTime=%%B
For /F "TOKENS=2* DELIMS=	 " %%A In ('REG QUERY "HKCU\Control Panel\International" /v sTime') Do Set _sTime=%%B
IF %_iTime%==1 Goto _in24format
For /F "TOKENS=1* DELIMS=%_sTime% " %%A In ('Echo %_Time%') Do (
	Set _Hour=%%A
	Set _Minutes=%%B
)
Set _AMPM=
Echo.%_Minutes%| FIND /I "A" >NUL && Set _AMPM=A
Echo.%_Minutes%| FIND /I "P" >NUL && Set _AMPM=P
IF [%_AMPM%]==[] Set _AMPM=A
Set _Minutes=%_Minutes:~0,2%
If %_Hour:~0,1%==0 Set _Hour=%_Hour:~-1%
IF %_Hour% LSS 12 IF /I %_AMPM%==P Set /A _Hour=%_Hour%+12
IF %_Hour% LSS 10 IF /I %_AMPM%==A Set _Hour=0%_Hour%
IF %_Hour% EQU 12 IF /I %_AMPM%==A Set _Hour=00
Set _Time=%_Hour%%_sTime%%_Minutes%
:_in24format
For /F "TOKENS=1,2* DELIMS=%_sTime% " %%A In ('Echo %_Time%') Do Set _hhmm=%%A%%B
Goto:EOF


See More: Batch Backup file for Vista Help

Report •


#1
January 6, 2010 at 09:25:36
Like I said about 4011 times before, there **IS NOT** general date/time substring solution.

This is exactly why.


=====================================
Helping others achieve escape felicity

M2


Report •

#2
January 6, 2010 at 17:28:01
Set _backupto=C:\Documents and Settings\Alex Hall\My Documents\~ Back Up ~\SanDisk Cruzer
Seeing as Vista no longer uses "Documents and Settings" nor "My Documents" (preferring "Users" and "Documents", respectively), I'd start with that line.

Report •

#3
January 6, 2010 at 17:39:23
Gee... they almost got that directory to 8.3 ; who'da thought.


=====================================
Helping others achieve escape felicity

M2


Report •

Related Solutions

#4
January 6, 2010 at 23:16:29
it sure seemed like a lot of code to do date stuff (not knocking op! general comment only ...) lieutenant general comment: (chant, like in the '60s:) EIGHT DOT THREE, FINE WITH ME! ok, i will never ever be comfortable with spaces in the directory, I can deal with long names, but i deplore spaces and system-reserved/critical char.s in file/dir names(but alas, those, too seem to be multiplificating mulltiplifiplifiplificating... and wreaking havoc where-elst they spawn! see: all the posts that contain /i "why" and "not found"/"syntax error" / "not exist")

Report •

#5
January 8, 2010 at 07:23:39
Thanks Razor, I changed the file names / directories so that they're in keeping with vista, yet still it just opens and closes straight away...

Is there anything in it that won't work / is different on vista than xp?

And Mechanix2Go, what do you mean when you say 'there **IS NOT** general date/time substring solution' ?


Report •

#6
January 8, 2010 at 09:12:30
Set _backupcmd=xcopy /cehiryq
Then I'd assume is no longer valid. But without at least an error message, I'm not going to bother with anything beyond casual glances at the script.

Report •

#7
January 8, 2010 at 09:29:57
'And Mechanix2Go, what do you mean when you say 'there **IS NOT** general date/time substring solution' ?'

The DATE variable built into NT/XP/vista varies according to:

language
regional settings
date layout
... and other things

Consequently there's no point in guessing what the DATE layout will be in any particular case.

Of course if you're clarvoiyant you can probably nail it every time.

The debug script I posted and nbrane worked over uses system calls, notably INT21, to get the date from BIOS. Completely isolated from windows settings, version etc.


=====================================
Helping others achieve escape felicity

M2


Report •

#8
January 9, 2010 at 09:11:01
> Gee... they almost got that directory to 8.3 ; who'da
> thought.

I'm sure they have a nice explanation for that, totally not mentioning the 8.3 system at all ...

Apart from correcting their mistake, they have the additional problem that the names change ...

again.


Report •

#9
January 11, 2010 at 09:51:13
Would it be possible for someone to write it so that it should
work? It seems there's quite a bit that needs changing...

Report •

#10
January 11, 2010 at 13:18:14
insert some echos and some pauses into the script at strategic locations to try and see 1) critical variable values and 2) system error messages. you could also redirect the output of the script to a "logfile" but i prefer the interactive approach myself. you can probably discern what points best to put the pauses and what values to echo out (mostly path & filenames, and the date stuff). If you do this, post the results if you still need assist.

Report •

#11
January 11, 2010 at 14:37:27
So you mean put things like
echo 01
and so on so I can see how far through it gets before
encountering a problem?

Report •

#12
January 11, 2010 at 15:05:33
yeah, like this part:
Echo ### Backing up %_backupfrom%%_Source% to %_drive%\%_Source%

but insert a pause after it:
pause

to suspend the output from going away before you can see it.
(i should have mentioned to back up your script before you work on it, if you do make any changes. copy it to .BAK so you have a safety, especially since the darn thing is so gargantuan). put a pause after any "echo" that is not going to a file, and maybe before and after calling subroutines.

and here, just take out the colons:
Echo Backup Complete!
@pause


Report •


Ask Question