Backup Creator Problem & Unrestricted variable maximum

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
john924xps
Posts: 65
Joined: 08 Jun 2012 07:48

Backup Creator Problem & Unrestricted variable maximum

#1 Post by john924xps » 30 Nov 2012 23:56

Hi! I was trying to create a program that created backups; and to save time, I created a SAVE function that allowed you to save directories to backup into a text document. When you enter the save function, you are asked how many lines you would like to load. Then it loads that much lines (from 1-var). THAT's where the problem begins. And I really need help fixing it.
The code: http://tinypaste.net/jUQSi5jj

The second problem is: I've created a simple decimal to binary converter. But if I type in a ridiculously large number to convert, it gives me this error: Invalid number. Numbers are limited to 32-bits of precision. Is there ANY way to bypass this limit?

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Backup Creator Problem & Unrestricted variable maximum

#2 Post by foxidrive » 01 Dec 2012 03:49

john924xps wrote:The second problem is: I've created a simple decimal to binary converter. But if I type in a ridiculously large number to convert, it gives me this error: Invalid number. Numbers are limited to 32-bits of precision. Is there ANY way to bypass this limit?


You can use VB with WSH.

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Backup Creator Problem & Unrestricted variable maximum

#3 Post by foxidrive » 01 Dec 2012 13:18

john924xps wrote:When you enter the save function, you are asked how many lines you would like to load. Then it loads that much lines (from 1-var). THAT's where the problem begins.


What we'd have to know is A) what the problem is and B) what the code does - a flowchart.

Without B) then some bunny has to read broken code and try and figure out what you meant to do.

Squashman
Expert
Posts: 4486
Joined: 23 Dec 2011 13:59

Re: Backup Creator Problem & Unrestricted variable maximum

#4 Post by Squashman » 01 Dec 2012 14:12

I prefer that the code be posted here on the forum inside code tags.

Aacini
Expert
Posts: 1914
Joined: 06 Dec 2011 22:15
Location: México City, México
Contact:

Re: Backup Creator Problem & Unrestricted variable maximum

#5 Post by Aacini » 01 Dec 2012 21:42

john924xps wrote:The second problem is: I've created a simple decimal to binary converter. But if I type in a ridiculously large number to convert, it gives me this error: Invalid number. Numbers are limited to 32-bits of precision. Is there ANY way to bypass this limit?

Yes. The way to do that is by splitting the large number in groups of digits that can be managed in 32-bits wide operations, that means, in groups of 4 digits, because 5 digits (99999*99999) exceed the maximum number, but 9999*9999 does not. This way, each group of 4 digits can be operated and the operation of the whole number can be completed along all the groups propagating a "carry" value from the partial result of one group to the next.

EDIT: However, in this particular case the divisor is the number 2 and the carry is just 0 or 1, so we can use larger groups of digits and still manage the operations in 32-bits integers. I modified the Batch file below to split the large number in groups of 9 digits; this modification speed up the program because it need less of half the number of iterations to process a very large number than with 4-digits groups.

The Batch file below use this method to convert an unlimited size decimal number to binary.

Code: Select all

@echo off

rem Decimal to binary conversion of an unlimited size decimal number
rem Antonio Perez Ayala

setlocal EnableDelayedExpansion

rem Get number of decimal digits
set decimal=X%1
set digits=0
for /L %%a in (12,-1,0) do (
   set /A "digits|=1<<%%a"
   for %%b in (!digits!) do if "!decimal:~%%b,1!" == "" set /A "digits&=~1<<%%a"
)

rem Get number of 9-digits groups and missing left zeros in first group
set /A groups=digits/9, zeros=9-digits%%9
set fill=
if %zeros% neq 9 (
   set /A groups+=1
   for /L %%a in (1,1,%zeros%) do set fill=!fill!0
)
set decimal=%fill%%1

rem Create the array of 9-digits groups eliminating left zeros in each group
for /L %%a in (%groups%,-1,1) do (
   set group[%%a]=!decimal:~0,9!
   for /L %%b in (1,1,8) do if "!group[%%a]:~0,1!" equ "0" set group[%%a]=!group[%%a]:~1!
   set decimal=!decimal:~9!
)

rem Convert decimal number to binary
set binary=
:nextBinaryDigit
   rem Divide the decimal number by 2
   set carry=0
   for /L %%i in (%groups%,-1,1) do (
      set /A partial=carry*1000000000+group[%%i], group[%%i]=partial/2, carry=partial%%2
   )
   rem Insert remainder in the result, in right-to-left order
   set binary=%carry%%binary%
   rem If last (most significant) group was completely converted: eliminate it
   if !group[%groups%]! equ 0 set /A groups-=1
   rem And pass to convert the rest of decimal groups
if %groups% gtr 0 goto nextBinaryDigit

echo %binary%

Previous program could convert a decimal number up to 8KB digits long! However, because the result must be stored in a variable, the maximum number of binary digits in the result is limited to 8KB. This mean that the maximum decimal number that can be converted is "just" 2^8KB-1 approx. 8)

Antonio
Last edited by Aacini on 03 Dec 2012 08:10, edited 1 time in total.

john924xps
Posts: 65
Joined: 08 Jun 2012 07:48

Re: Backup Creator Problem & Unrestricted variable maximum

#6 Post by john924xps » 01 Dec 2012 22:17

There is the code below. In the :save function, it checks if BackupSaves.txt exists. BackupSaves.txt contains directories to just select and backup to or from, to make it more efficient, besides having to always type in the same and long directories.

If BackupSaves.txt DOES exist, it asks the user how many lines he/she wants to load. Since the user may have millions of directories in that file, and it may be really inconvenient to have to scan through all. After recieving the amount of lines to load up, the computer just does it. It puts each directory (one directory on each line) from the first line, to %loadup% line. The variables the computer puts the directories into is something like _save1, _save2, etc. After inputting all of the directories into the variables, the computer does "set _save" to list them all.

THE PROBLEM IS: It checks if BackupSaves.txt exists all right. But when I type in "1" for the computer to load, it doesn't. It says _save is not defined.

I think the problem may occur here:

Code: Select all

<BackupSaves.txt (
for /l %%G in (1,1,%stringload%) do (
set /p _save%%G=
)))



And this is the full code:

Code: Select all

@echo off
title Backup
color 0a
:varset
cls
set location=
set destination=
set agree=
echo Enter a location and destination or simply type save
echo to add and create paths to inherit from.
set /p location=Location of file/folder:
if /i "%location%"=="save" (
set save=location
goto save
)
set /p destination=Destination to copy to:
if /i "%destination%"=="save" goto save
echo.
echo Location: %location%
echo Destination: %destination%
echo Agree?
set /p agree=(yes/no):
if /i "%agree%"=="no" goto varset
if not exist "%destination%\BackupMainDir" md "%destination%\BackupMainDir"
set checknum=1

:checkfunc
if exist "%destination%\BackupMainDir\Backup %checknum%" (
set /a checknum=%checknum% + 1
goto checkfunc
) else (
md "%destination%\BackupMainDir\Backup %checknum%"
xcopy "%location%\*.*" "%destination%\BackupMainDir\Backup %checknum%" /s /h /e /k /f /c 2>nul
xcopy "%location%.*" "%destination%\BackupMainDir\Backup %checknum%" /s /h /e /k /f /c 2>nul
pause
goto :eof
)

::this section is where it starts to get glitched
:save
cls
set stringload=
set new=
if exist "%appdata%\BackupSaves.txt" (
echo How many strings do you want to load?
set /p stringload=:: 
cd %appdata%
<BackupSaves.txt (
for /l %%G in (1,1,%stringload%) do (
set /p _save%%G=
)))
cls
echo Select a variable to inherit or type "new" to create a new list...
set _save
echo.
set /p select=::
if /i "%select%"=="new" (
echo.
set /p new=New Address:
cd %appdata%
if not exist "BackupSaves.txt" (
echo %new% > "BackupSaves.txt") else (
echo %new% >> "BackupSaves.txt") else (
if "%save%"=="location" (
set location=%select%) else (
set destination=%select%))
goto varset

Aacini
Expert
Posts: 1914
Joined: 06 Dec 2011 22:15
Location: México City, México
Contact:

Re: Backup Creator Problem & Unrestricted variable maximum

#7 Post by Aacini » 01 Dec 2012 23:35

john924xps wrote:I think the problem may occur here:

Code: Select all

<BackupSaves.txt (
for /l %%G in (1,1,%stringload%) do (
set /p _save%%G=
)))


Yes. stringload variable is read inside an IF command, so in order to use its new value, the variable must be enclosed in exclamation marks and you must enable Delayed Expansion at beginning of the program:

Code: Select all

setlocal EnableDelayedExpansion

. . . . .

<BackupSaves.txt (
for /l %%G in (1,1,!stringload!) do (
set /p _save%%G=
)))
For an explanation, type SET /? and carefully read the part that start at "Finally, support for delayed environment variable expansion has been added."

Antonio

Post Reply