Batch to move if enough space.

Hewlett-packard / PAVILION 734N
December 30, 2008 at 17:49:13
Specs: Windows XP Home, 2Ghz 1Gb
I'm writing a batch file for backing up my computer and installing previously created backups. I want a way to move a file only if there is enough space on the destination drive. It has to check The file size against the free space on the destination drive and copy if there is enough space. This will be the final part of the batch, it moves the archive to another Drive for storage.

See More: Batch to move if enough space.

Report •

December 31, 2008 at 23:11:58
1. Tested only on vista. Should work on xp.
2. if you name it cszcopy.bat you can invoke like you would invoke the copy command. "cszcopy sourfile.ext d:\directory"

@echo off & setlocal enableDelayedExpansion

if %2c==c (
echo Usage %~0 Filename.ext Drive:\Directory\tocopy
goto :eof
for /f "usebackq tokens=2 delims=)" %%a in (`dir "%~2"`) do set freespace=%%a
:after the above command, freespace contains the last line from dir command,
:that is "freespace= 6,246,825,984 bytes free"

:remove comma,
set freespace1=%freespace:,=%
:freespace1= 6246825984 bytes free

:remove space
set freespace2=%freespace1: =%

: remove b and everything after it
for /f "tokens=1 delims=b" %%a in ("%freespace2%") do set freespace=%%a
if /i %~z1 leq %freespace% ( copy %* ) else (Echo Space required: %~z1. ^< Space available on %~2: %freespace%)


Report •

January 1, 2009 at 00:31:58
The effectiveness of the above on 32 bit systems is limited to files less than ~2gb.

Case in point:

if 2147483648 leq 2147483647 echo something went wrong!

On 64bit systems I assume the equivalent will be:

if 9223372036854775808 leq 9223372036854775807 echo something went wrong!

But of course that will be in the region of about eight and a half billion gigabytes(according to my calculations), this is of course a big issue as I know we all have a few files creeping up on this limit ;)

In all seriousness if the files don't reach the limit it isn't an issue in 32bit windows, even if the free space is larger as it will simply substitute the largest number it can compare against in it's place.

Report •

January 1, 2009 at 14:15:58
By "The effectiveness of the above on 32 bit systems is limited to files less than ~2gb." Do you mean it wont work for files above @GB, The last Backup I made was ~5GB.

Also, If you could explain your responses a little bit, that would be appreciated. I'm undertaking This and other batch file projects more to teach myself about them than to lessen my workload. To Demonstrate, here is what I have so far on this project, and it is the most complex one I have written so far.

@echo off
title Backup Utility

REM This is a utility to backup the PC

echo Would You like to Backup the Computer(1)
echo or install a previously created Backup(2)
set/p "cho=>"
If %cho%==1 goto CREATE
If %cho%==2 goto INSTALL
If %cho%==exit goto END
echo Please enter either 1 to create or 2 to Install

REM The following is The section for Creating the archive

echo This Will Backup all files from the desktops
echo and My Documents Folders of the users.
echo The backup will save the archive to D:\
echo Make Sure D:\ is clear.
echo Do you wish to Proceed? y/n
set/p "cho=>"
If %cho%==y goto CREATION
If %cho%==n goto START
If %cho%==exit goto END
echo Please enter either y or n
md C:\"Documents and Settings"\Usr1\"My Documents"\DesktopBackup
md C:\"Documents and Settings"\Usr2\"My Documents"\DesktopBackup
md C:\"Documents and Settings"\Usr1\Desktop\Backup\Usr1
md C:\"Documents and Settings"\Usr1\Desktop\Backup\Usr2
xcopy C:\"Documents and Settings"\Usr1\Desktop C:\"Documents and Settings"\Usr1\"My Documents"\DesktopBackup /E /F /H /K /Y /c /EXCLUDE:these.txt
xcopy C:\"Documents and Settings"\Usr2\Desktop C:\"Documents and Settings"\Usr2\"My Documents"\DesktopBackup /E /F /H /K /Y /c /EXCLUDE:these.txt
xcopy C:\"Documents and Settings"\Usr1\"My Documents" C:\"Documents and Settings"\Usr1\Desktop\Backup\Usr1\ /E /F /H /K /Y /c /EXCLUDE:these.txt
xcopy C:\"Documents and Settings"\Usr2\"My Documents" C:\"Documents and Settings"\Usr1\Desktop\Backup\Usr2\ /E /F /H /K /Y /c /EXCLUDE:these.txt
7z a -t7z "C:\Documents and Settings\Usr1\Desktop\archive.7z" "C:\Documents and Settings\Usr1\Desktop\Backup" -mx9
xcopy "C:\Documents and Settings\Usr1\Desktop\archive.7z" D:\ /E /F /H /K /Y /c
del "C:\Documents and Settings\Usr1\Desktop\archive.7z"
rd C:\"Documents and Settings"\Usr1\Desktop\Backup\
rd C:\"Documents and Settings"\Usr1"My Documents"\DesktopBackup
rd C:\"Documents and Settings"\Usr2\"My Documents"\DesktopBackup
defrag D:
goto END

REM The following is the section for Installing the archive
REM It essentialy Runs the Creation section in reverse.

echo This will Install the Previously Created backup archive
echo Data in the My Documents folders and on Desktops may be lost
echo Do you wish to Proceed? y/n
set/p "cho=>"
If %cho%==y goto INSTALLATION
If %cho%==n goto START
If %cho%==exit goto END
echo Please enter either y or n
xcopy D:\archive.7z "C:\Documents and Settings\Usr1\Desktop\" /E /F /H /K /Y /c
7z x "C:\Documents and Settings\Usr1\Desktop\archive.7z"
xcopy C:\"Documents and Settings"\Usr1\Desktop\Backup\Usr2\ C:\"Documents and Settings"\Usr2\"My Documents" /E /F /H /K /Y /c
xcopy C:\"Documents and Settings"\Usr1\Desktop\Backup\Usr1\ C:\"Documents and Settings"\Usr1\"My Documents" /E /F /H /K /Y /c
xcopy C:\"Documents and Settings"\Usr2\"My Documents"\DesktopBackup C:\"Documents and Settings"\Usr2\Desktop /E /F /H /K /Y /c
xcopy C:\"Documents and Settings"\Usr1\"My Documents"\DesktopBackup C:\"Documents and Settings"\Usr1\Desktop /E /F /H /K /Y /c
del "C:\Documents and Settings\Usr1\Desktop\archive.7z"
rd C:\"Documents and Settings"\Usr1\Desktop\Backup\
rd C:\"Documents and Settings"\Usr1\"My Documents"\DesktopBackup
rd C:\"Documents and Settings"\Usr2\"My Documents"\DesktopBackup
defrag C:
goto END


ECHO  is the bell command, and I used a command line version of 7zip to archive the files.

Report •

Related Solutions

January 1, 2009 at 17:34:28
Ok I'm going to start with holla's script and move on from there.

On Holla's script the file and directory names were to be input as arguments, so it needed to be started from the command line with "script.cmd filename.exe directoryname".

Holla used for /f, which loops over a body of text, the text can either be a txt file or the output of a command, to get the total free space in bytes from the free space listed by the dir command. For more info on the for loop goto the command line and type in "for /?".

There is a trick that can be used on arguments and for loop variables, also explained in "for /?". The variables can become attributes of the file/directory data by using modifiers. These modifers are in the format of %~z1 or %%~pa. There are a few different modifiers, the one of interest here is "z". The z modifier is the file size in bytes.

As the data is in bytes and there are few avenues to obtain the data in megabytes or gigabytes we have to deal with quite large numbers. In windows 1gb = 1024mb, 1mb = 1024kb and 1kb = 1024bytes.

In 32bit windows signed 32bit numbers are used, these range from −2,147,483,648 to +2,147,483,647. Since we are only interested in positive numbers this essentially halves. 2,147,483,647 is the highest most things on the command line(windows nt based) will deal with including "if x mod x" where mod is gtr, lss, equ, geq or leq, see "if /?" for more info. If larger numbers are used 2,147,483,647 will be substituted in it's place.

Holla used "if x leq x...." to test if enough space was present. The largest file this can actually work on is 2,147,483,647/1024/1024/1024 or about 1.99gb. Anything larger will give a false positive if the free space is larger than 2gb and the file is larger still. As demonstrated by:

if 2147483648 leq 2147483647 echo something went wrong!

Now with all that out of the way onto your script.

"quotes" - the way you are using quotes is not suggested and would cause problems with some commands, start for one. If the file name contains a space or ampersand it is best to surround the whole path in double quotes not just the offending parts. For example "c:\documents and settings\usr1\desktop". Even if the path doesn't contain a space the quotes generally won't cause any trouble. Technically quotes are illegal characters in path names, most programs still work but some will call you on it.

You could definitely get some of the xcopy commands down using for loops but this post is getting pretty long so I'll leave that for now.......

As for the limitations I've been working on work-arounds but haven't got to the stage of comparisons yet, I've had some success with division, multiplication and addition. If you want I can post the division batch that can divide very large numbers by 1 to 9999999. That way you could test in megabytes instead...........

Report •

January 2, 2009 at 07:58:10
this code support unsigned 32bit integer. which is around 2147483647*2. if filesize/freespace is bigger than this, you need MB code as suggested by judago to workaround this limitation

for /f "tokens=3 delims= " %%a in ('dir "%~2" ^| find " bytes free"') do set free=%%a
set /a bit1="%free:.=% >> 31 & 0x1"
set /a free="%free:.=% <<1 >>1"

set /a bit2="%~z1 >> 31 & 0x1"
set /a sz="%~z1 <<1 >>1"
if /i %bit2% lss %bit1% (copy %*)
if /i %bit2% equ %bit1% if /i %sz% leq %free% (copy %*)

Report •

January 2, 2009 at 17:12:54
Thanks all, I think I got it now.

Report •

Ask Question