Jump to content











Photo
- - - - -

Batch script to back up folders using Windows XP backup utility


  • Please log in to reply
18 replies to this topic

#1 Holmes.Sherlock

Holmes.Sherlock

    Gold Member

  • Team Reboot
  • 1444 posts
  • Location:Santa Barbara, California
  •  
    United States

Posted 15 June 2011 - 04:47 PM

I wrote a batch script to back up three folders residing on my desktop's D:\ drive. I tried to make my script this much "intelligent" so that it can determine the presence/absence of previous backups of the same folders. If a previous FULL backup is there, it should perform an INCREMENTAL backup, otherwise should go for a FULL/NORMAL backup at the specified location. The script is as follows



@echo off

cls

if (%1)==() goto noDriveSpecified



echo --------------------------------

echo Backing up Programs n Resources

echo --------------------------------

IF EXIST "%1:\Programs & Resources.bkf" (

ntbackup backup "D:\Programs & Resources.bkf" /J "Programs & Resources" /M incremental /F "%1:\Programs & Resources.bkf" /L:s

) ELSE (

ntbackup backup "D:\Programs & Resources" /J "Programs & Resources" /M normal /F "%1:\Programs & Resources.bkf" /L:s

)



echo ---------------------

echo Backing up Essentials

echo ---------------------

IF EXIST "%1:\Essentials.bkf" (

ntbackup backup "D:\Essentials" /J "Essentials" /M incremental /F "%1:\Essentials.bkf" /L:s

) ELSE (

ntbackup backup "D:\Essentials" /J "Essentials" /M normal /F "%1:\Essentials.bkf" /L:s

)





echo ------------------------------

echo Backing up Non Technical Books

echo ------------------------------

IF EXIST "%1:\Non Technical Books.bkf" (

ntbackup backup "D:\Non-Technical Books" /J "Non-Technical Books" /M incremental /F "%1:\Non Technical Books.bkf" /L:s

) ELSE (

ntbackup backup "D:\Non-Technical Books" /J "Non-Technical Books" /M normal /F "%1:\Non Technical Books.bkf" /L:s

)



goto end



:noDriveSpecified

echo FORMAT: backup <driveletter>



:end



The problem with this are
  • For the first time, the FULL backup is performed correctly. For second & subsequent times, the backup archive is reduced to a small sized file.
  • If no parameter is passed, I expected the script to display the format of the command. But, instead it shows "The syntax of the command is incorrect".


#2 Wonko the Sane

Wonko the Sane

    The Finder

  • Advanced user
  • 16066 posts
  • Location:The Outside of the Asylum (gate is closed)
  •  
    Italy

Posted 15 June 2011 - 04:54 PM

Just for the record (Holmes.Sherlock already has this via PM):
@echo off

SETLOCAL ENABLEEXTENSIONS



FOR %%A IN (C D E F G H I J K L M N O P Q R S T U V W X Y Z) DO IF /I %1.==%%A. SET TargetDrive=%1:\

if %TargetDrive%.==. goto :noDriveSpecified



SET SourceDrive=D:\



FOR  %%A IN (

"Programs n Resources"

"Essentials"

"Non Technical Books"

) DO CALL :do_backup %%A

GOTO :EOF



:do_backup

SET SourceName=%~1

SET SourcePath="%SourceDrive%%SourceName%"

SET TargetFile="%TargetDrive%%SourceName%.bkf"

echo --------------------------------

echo Backing up %SourceName%

echo --------------------------------

SET Param=normal

IF EXIST %TargetFile% SET Param=incremental

ECHO ntbackup backup %SourcePath% /J "SourceName" /M %param% /F %TargetFile% /L:s

GOTO :EOF





:noDriveSpecified

echo FORMAT: backup ^<driveletter^>

No actual tests made, up to Holmes.Sherlock to adapt if needed, test and report.

2.If no parameter is passed, I expected the script to display the format of the command. But, instead it shows "The syntax of the command is incorrect".

Sure :cheers:, < and > are REDIRECTION characters in batch and need to be escaped (see last line of the proposed modified batch).

:cheers:
Wonko

#3 Holmes.Sherlock

Holmes.Sherlock

    Gold Member

  • Team Reboot
  • 1444 posts
  • Location:Santa Barbara, California
  •  
    United States

Posted 15 June 2011 - 04:59 PM

Actual tests are being performed. It'll take time as the combined data is voluminous.

Problem #2, as stated in my original post, is gone. Now please explain what magic is there in

SETLOCAL ENABLEEXTENSIONS



#4 Wonko the Sane

Wonko the Sane

    The Finder

  • Advanced user
  • 16066 posts
  • Location:The Outside of the Asylum (gate is closed)
  •  
    Italy

Posted 15 June 2011 - 05:07 PM

Actual tests are being performed. It'll take time as the combined data is voluminous.

Problem #2, as stated in my original post, is gone. Now please explain what magic is there in


SETLOCAL ENABLEEXTENSIONS

NO "magic" :).
Simply a way to:
  • have batch variables LOCAL to the actual running batch (as opposed to being local to the CMD session)
  • making sure that CALL works as it should and that %~1 expands correctly (through EXTENSIONS)

See:
http://www.robvander....com/ntcall.php

BTW, your original batch also had a TYPO:

IF EXIST "%1:\Programs & Resources.bkf" (
ntbackup backup "D:\Programs & Resources.bkf" /J "Programs & Resources" /M incremental /F "%1:\Programs & Resources.bkf" /L:s
) ELSE (
ntbackup backup "D:\Programs & Resources" /J "Programs & Resources" /M normal /F "%1:\Programs & Resources.bkf" /L:s
)


The use of variables and FOR loops helps in avoiding them, as you only type "once" the names.... ;)

:cheers:
Wonko

#5 Holmes.Sherlock

Holmes.Sherlock

    Gold Member

  • Team Reboot
  • 1444 posts
  • Location:Santa Barbara, California
  •  
    United States

Posted 15 June 2011 - 05:20 PM

Before I could digest "that" ENABLEEXTENSIONS, one more awkward problem has sprung up. My Problem #1, is PARTIALLY solved. What happened previously is, the INCREMENTAL backup was replacing the original FULL backup, thereby reducing the backup archive size drastically. I modified them as follows

ntbackup backup "D:\Essentials" /J "Essentials" /M incremental[b] /A [/b]/F "%1:\Essentials.bkf" /L:s

to append the INCREMENTAL backup to previous backups. Now the problem is, it works for 2nd & 3rd folders but not for the 1st one. Even I tried to run the command

ntbackup backup "D:\Programs & Resources.bkf" /J "Programs & Resources" /M incremental /A /F "N:\Programs & Resources.bkf" /L:s

from command line. A backup windows flashes up goes away instantly, leaving no error message. But astonishingly, NORMAL backup of the SAME folder is working fine with the following command from both batch & command line

ntbackup backup "D:\Programs & Resources" /J "Programs & Resources" /M normal /F "N:\Programs & Resources.bkf" /L:s

Any ideas?

#6 Wonko the Sane

Wonko the Sane

    The Finder

  • Advanced user
  • 16066 posts
  • Location:The Outside of the Asylum (gate is closed)
  •  
    Italy

Posted 15 June 2011 - 05:48 PM

Any ideas?

READ.

You have a TYPO in that line, the SOURCE is NOT a .bkf file, it is a FOLDER!

As told you before via PM DO NOT use characters that are part of the batch language in names of files or folders, "&" is one of the NO, NOes:
http://www.addletter...oopid filenames


;)
Wonko

#7 Holmes.Sherlock

Holmes.Sherlock

    Gold Member

  • Team Reboot
  • 1444 posts
  • Location:Santa Barbara, California
  •  
    United States

Posted 15 June 2011 - 06:07 PM

You have a TYPO in that line, the SOURCE is NOT a .bkf file, it is a FOLDER!

Ohh, I forgot to mention that I've already corrected the typo in the actual script. The result stated above is after the correction.

As told you before via PM DO NOT use characters that are part of the batch language in names of files or folders, "&" is one of the NO, NOes:
http://www.addletter...oopid+filenames


The result is same when run from command line also. Again, it works fine for NORMAL backup. Only the INCREMENTAL is problematic.

#8 Wonko the Sane

Wonko the Sane

    The Finder

  • Advanced user
  • 16066 posts
  • Location:The Outside of the Asylum (gate is closed)
  •  
    Italy

Posted 15 June 2011 - 06:18 PM

The result is same when run from command line also. Again, it works fine for NORMAL backup. Only the INCREMENTAL is problematic.

Sure, command line (actually cmd.exe) USES THE SAME SYNTAX and OPERATORS batch language uses, actually batch is a way to repeat commands given to cmd.exe.

RENAME that folder WITHOUT using the "&" and try again! :cheers:
As posted in my proposed batch (which BTW should be what you are testing INSTEAD of your one :)), I changed:
"Programs & Resources"
to
"Programs n Resources"
there are REASONS (that I told you, TWICE by now :ranting2: ) for that renaming.
(or try in command line ESCAPing the "&") ;)

:cheers:
Wonko

#9 Holmes.Sherlock

Holmes.Sherlock

    Gold Member

  • Team Reboot
  • 1444 posts
  • Location:Santa Barbara, California
  •  
    United States

Posted 16 June 2011 - 05:22 PM

there are REASONS (that I told you, TWICE by now :smiling9: ) for that renaming.

My Italian uncle is getting peevish these days. Don't know whether it is because of Italian climate Posted Image. Sherlock is too stubborn to obey an instruction unless repeated few times.Posted Image


The result is astonishing. INCREMENTAL backup of both "Programs n Resources" & "Programs & Resources" are failing on my desktop but succeeds on my laptop!!!

#10 Holmes.Sherlock

Holmes.Sherlock

    Gold Member

  • Team Reboot
  • 1444 posts
  • Location:Santa Barbara, California
  •  
    United States

Posted 18 June 2011 - 01:22 PM

@Wonko,
I resolved the problem. I had one more typo in my original script. The problem was not with 'n' and '&'

BTW, can you please try 'escaping' the '&' in your script instead of using 'n'?

#11 Holmes.Sherlock

Holmes.Sherlock

    Gold Member

  • Team Reboot
  • 1444 posts
  • Location:Santa Barbara, California
  •  
    United States

Posted 18 June 2011 - 07:02 PM

Can this page be of any help in modifying Wonko's batch to treat ampersands properly?

#12 Wonko the Sane

Wonko the Sane

    The Finder

  • Advanced user
  • 16066 posts
  • Location:The Outside of the Asylum (gate is closed)
  •  
    Italy

Posted 19 June 2011 - 05:07 PM

You are not getting the message. :(

It is foolish to use ampersand or ANY other character that is also used as a "special" character in command line or batch.

This does not mean that there is NO solution to it, it means that it is foolish to create a potential problem only for the sake of using a stupid "&" in a file/directory name, as:
  • if you are going to write all your batches, it will make your life uneededly more complex
  • if you are going to use "third party" batches, you will probably need to modify ALL of them to comply with your foolish fancy of using "&" in file/directory names
  • sooner or later you will forget about this and create havoc on your filesystem/drives

Is it worth it? :cheers:

If it is (according to you, and BTW wrongly :rofl:) you can well add the provision to that and all future batches you will ever write :worship: (but asking help to me for doing that? :w00t: )

Sure that page can help :cheers: , it simply states how anything within double quotes is not parsed, and that you can use variable character sustitution, how difficult is it to change:
SET SourceName=%~1
to:
SET SourceName=%1

SET SourceName=%Sourcename:&=^&%

SET SourceName=%Sourcename:"=%

:unsure:

:cheers:
Wonko

#13 Holmes.Sherlock

Holmes.Sherlock

    Gold Member

  • Team Reboot
  • 1444 posts
  • Location:Santa Barbara, California
  •  
    United States

Posted 19 June 2011 - 05:18 PM

Is it worth it? :cheers:

YES, it's worth it. Because, if I rename the folder, you have to provide me a way to do the same with the folders written on last 12 DVD-R discs. Posted Image

........but asking help to me for doing that? :rofl:

Who else can I ask to do that?

Sure that page can help :cheers: , it simply states how anything within double quotes is not parsed, and that you can use variable character sustitution, how difficult is it to change:


Does it mean that my google-fu is increasing? Posted Image

#14 Wonko the Sane

Wonko the Sane

    The Finder

  • Advanced user
  • 16066 posts
  • Location:The Outside of the Asylum (gate is closed)
  •  
    Italy

Posted 19 June 2011 - 06:01 PM

Who else can I ask to do that?

Are you familiar with a nice human artifact called "looking glass"? :cheers:

The general concept is about "independence", you create yourself a problem, you solve it.... without asking anyone else.

Does it mean that my google-fu is increasing? Posted Image

Sure :cheers:,

Posted Image

BUT:
Spoiler


:rofl:
Wonko

#15 Holmes.Sherlock

Holmes.Sherlock

    Gold Member

  • Team Reboot
  • 1444 posts
  • Location:Santa Barbara, California
  •  
    United States

Posted 19 June 2011 - 06:02 PM

I think your original batch had a typo which I guess that I've corrected in red letter. Is it your modified batch with ampersand support?

@echo off
SETLOCAL ENABLEEXTENSIONS
REM cls
if %1.==. goto noDriveSpecified

FOR %%A IN (C D E F G H I J K L M N O P Q R S T U V W X Y Z) DO IF /I %1.==%%A. SET TargetDrive=%1:\


SET SourceDrive=D:\

FOR %%A IN (
"Programs & Resources"
"Essentials"
"Non Technical Books"
) DO CALL :do_backup %%A
GOTO :EOF

:do_backup
SET SourceName=%1
SET SourceName=%Sourcename:&=^&%
SET SourceName=%Sourcename:"=%
SET SourcePath="%SourceDrive%%SourceName%"
SET TargetFile="%TargetDrive%%SourceName%.bkf"
echo --------------------------------
echo Backing up %SourceName%
echo --------------------------------
SET Param=normal
IF EXIST %TargetFile% SET Param=incremental
echo ntbackup backup %SourcePath% /J "%SourceName%" /M %param% /F %TargetFile% /L:s
GOTO :EOF


:noDriveSpecified
echo FORMAT: backup ^<driveletter^>


Unfortunately, it's not working.
The output it's producing as follows



--------------------------------

Backing up Programs

&#39;Resources&#39; is not recognized as an internal or external command,

operable program or batch file.

--------------------------------

ntbackup backup "C:\Programs & Resources" /J "Programs & Resources" /M normal /F

 "M:\Programs & Resources.bkf" /L:s

--------------------------------

Backing up Essentials

--------------------------------

ntbackup backup "C:\Essentials" /J "Essentials" /M normal /F "M:\Essentials.bkf"

 /L:s

--------------------------------

Backing up Non Technical Books

--------------------------------

ntbackup backup "C:\Non Technical Books" /J "Non Technical Books" /M normal /F "

M:\Non Technical Books.bkf" /L:s



#16 Wonko the Sane

Wonko the Sane

    The Finder

  • Advanced user
  • 16066 posts
  • Location:The Outside of the Asylum (gate is closed)
  •  
    Italy

Posted 19 June 2011 - 07:04 PM

Actually it is working:
ntbackup backup "C:\Programs & Resources" /J "Programs & Resources" /M normal /F

 "M:\Programs & Resources.bkf" /L:s
:cheers:

Only it displays an error message when it reaches this line:
echo Backing up %SourceName%

Try changing it to :cheers: (if you strip quotes you need to re-add them):
echo Backing up "%SourceName%"

You just got +5 points for finding where to add the "%"'s and -135 for not looking into the above properly.

Total points amount to -125,145 (and 3/4) :rofl:

:worship:
Wonko

#17 Holmes.Sherlock

Holmes.Sherlock

    Gold Member

  • Team Reboot
  • 1444 posts
  • Location:Santa Barbara, California
  •  
    United States

Posted 19 June 2011 - 11:51 PM

Actually it is working:

Yeah!!!! It's working. Thank youuuuuuuuuuu a lottttttt Posted Image

You just got +5 points for finding where to add the "%"'s and -135 for not looking into the above properly.

Hmmmm,probably it's the time to change my spectacles.

#18 Holmes.Sherlock

Holmes.Sherlock

    Gold Member

  • Team Reboot
  • 1444 posts
  • Location:Santa Barbara, California
  •  
    United States

Posted 22 June 2011 - 06:37 PM

"Programs n Resources"
"Essentials"
"Non-Technical Books"
) DO CALL :do_backup %%A

One more typo here.Seems that Wonko's mamory is under tremendous challenge.Posted Image

#19 Wonko the Sane

Wonko the Sane

    The Finder

  • Advanced user
  • 16066 posts
  • Location:The Outside of the Asylum (gate is closed)
  •  
    Italy

Posted 22 June 2011 - 07:41 PM

One more typo here.Seems that Wonko's mamory is under tremendous challenge.Posted Image


I know quite a few good opticians, if you can't find one good enough for your (needed :cheers: ) new pair of spectacles:

No actual tests made, up to Holmes.Sherlock to adapt if needed, test and report.


:thumbup:
Wonko




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users