Computing.Net > Forums > Programming > Percent Complete BAT File Attempt

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

Percent Complete BAT File Attempt

Reply to Message Icon

Name: kgp
Date: February 12, 2007 at 09:54:07 Pacific
OS: XP
CPU/Ram: n/a
Product: n/a
Comment:

Hi,

I am working on a batch file that counts the number of files in a start folder and then the number of files in a destination folder and then works out the percentage complete from the two numbers. It is not going to badly except I have no idea how to get the numbers into the batch file.

I tried

set startcount=dir /a /s c:\"Documents and settings"\"kgp"\"my documents"\test\"My Pics"\ |find /c /v ""

and

set /a startcount=dir /a /s c:\"Documents and settings"\"kgp"\"my documents"\test\"My Pics"\ |find /c /v ""

No Luck. Gave a divide by zero error.

I then used the bit after the set startcount= to export the numbers it came up with to a txt document, worked great ! Now how to get the numbers from the first line of the two text documents (startcount.txt and fincount.txt)into the equation, (Fincount) / (Startcount) * 100, to find the percent ?

Any Suggestions ??



Sponsored Link
Ads by Google

Response Number 1
Name: kgp
Date: February 12, 2007 at 10:16:05 Pacific
Reply:

I found this on batfiles but am clueless of how it is supposed to work. It had no explanation with it.

Get up to nine words of first line of input file into variables.

Ansi.sys optional; Esc represents the escape character, created in edit by 'Ctrl+P Esc'.

@ECHO off
:: setvars.bat
:: Author: Laurence Soucy, http://purl.oclc.org/net/Batfiles/
:: Sets variables in sequence var1...var9, for
:: up to 9 words on first line of input file.
IF "%1"=="ReCuRs+" FOR %%c in (SHIFT SHIFT GOTO:recurs) do %%c
IF "%1"=="" FOR %%c in (ECHO GOTO:end) do %%c existing filename required
IF not exist %1 FOR %%c in (ECHO GOTO:end) do %%c existing filename required
SET str=
ECHO @PROMPT SET str=%%%%str%%%%>%temp%.\setvar1.bat
FC/N/LB1 %1 nul |FIND "1:" >>%temp%.\setvar1.bat
CTTY nul
%COMSPEC%/E:2048/C%temp%.\setvar1.bat> %temp%.\setvar2.bat
CTTY con
::%opt%ECHO Esc[2A
for %%c in (%temp%.\setvar2.bat del) do call %%c %temp%.\setvar?.bat
%0 ReCuRs+ %str%

:recurs
SET str=
IF "%1"=="" ECHO. first line of input file is blank
SET var1=%1
SET var2=%2
SET var3=%3
SET var4=%4
SET var5=%5
SET var6=%6
SET var7=%7
SET var8=%8
SET var9=%9
:end


0

Response Number 2
Name: dtech10
Date: February 12, 2007 at 16:04:04 Pacific
Reply:

Hi
Something like this help.

-----
@echo off
setlocal EnableDelayedExpansion

set Count1=0
set Count2=0
for /f "tokens=*" %%a in ('dir /a /s /b d:\Extra\Aug04\') do set /a Count1+=1
for /f "tokens=*" %%a in ('dir /a /s /b d:\Extra\Aug05\') do set /a Count2+=1

echo %Count1%
echo %Count2%
set /a Percent=%Count1%/%Count2%*100
echo %Percent%


0

Response Number 3
Name: Mechanix2Go
Date: February 12, 2007 at 22:31:18 Pacific
Reply:

Seems like you'd want to use /a-d and count only FILES [not directories].

This looks like it works.

::== pc.bat
@echo off
setLocal EnableDelayedExpansion

for /f %%A in ('dir /a-d/s/b ..^|find /v /c "\\"') do set /a src=%%A
for /f %%A in ('dir /a-d/s/b ^|find /v /c "\\"') do set /a dest=%%A
set /a done=(!dest!*100)/!src!
echo !done! percent done
::====================


=====================================
If at first you don't succeed, you're about average.

M2



0

Response Number 4
Name: kgp
Date: February 13, 2007 at 05:04:27 Pacific
Reply:

Thanks so much. I cant say I understand how it works but I am grateful for the help, and so quickly !!


Fight Crime,
Shoot Back !


0

Response Number 5
Name: kgp
Date: February 13, 2007 at 05:23:11 Pacific
Reply:

I am really bad at this !

Ok, I copy and pasted M2s code straight into a .bat and ran it on its own. It came up with two numbers and calculated the percent. I just don't know which two folders it got the numbers from !! How do I tell it which directories I want it to check. I am using this...

for /f "tokens=3 delims=\" %%i in ("%USERPROFILE%") DO (set user=%%i)

to find the local user name and...

c:\"Documents and Settings"\"%user%"\"My Documents"\....

as the directory bit. How do I input the two directories into the code ?

Fight Crime,
Shoot Back !


0

Related Posts

See More



Response Number 6
Name: kgp
Date: February 13, 2007 at 08:46:32 Pacific
Reply:

Success ! Sort of...

With dtech10's code I imputed the directories and it worked perfectly. I modified it and put it on a loop...

@echo off

for /f "tokens=3 delims=\" %%i in ("%USERPROFILE%") DO (set user=%%i)

setlocal EnableDelayedExpansion


:loop

:start

set Count1=0
set Count2=0

for /f "tokens=*" %%a in ('dir /a-d-s/s/b "c:\Documents and settings\%user%\my documents\test 2\kps apps"') do set /a Count1+=1
for /f "tokens=*" %%a in ('dir /a-d-s/s/b l:\"kps apps 2"') do set /a Count2+=1

echo %Count1%
echo %Count2%

::ping -n 10 127.0.0.1 > nul

set /a Percent=%Count2%/%Count1%*100
If %percent%==100 goto fincopy
IF not %percent%==0 goto end
IF %percent%==0 goto start


:end

cls

echo.
echo.
echo %Percent% Percent Complete
echo.
echo.

goto loop

:fincopy

echo.
echo.
echo Finished Copying
echo.
echo.

ping -n 5 127.0.0.1 > nul

exit

It gave me two numbers defined by %count1% and %count2%, %count1% stayed the same and %count2% changed as the files copied. But it could never give a percentage because 351 (the start number ==> %count1%) would not divide with the random numbers of %count2% without having a decimal answer, any decimal answer in dos is taken as 0. So unless there is a way to get dos to accept decimals, it will not show a percentage often enough, if at all, to be usable. Even if M2 explains his suggestion, it still has to divide the two numbers and so will have the same problem.

Still where there's a will there's a way, anyone know of a way to make cmd.exe accept decimals ??

Fight Crime,
Shoot Back !


0

Response Number 7
Name: kgp
Date: February 13, 2007 at 10:20:12 Pacific
Reply:

Right. I found this...

http://tp.lc.ehu.es/jma/msdos.html

that calculates decimals but when I type

set /a finpercent=eval 50/100*100

it gives a "missing operator" error.

Is there any other to get the number produced by Eval into a variable ??

I experimented a bit with errorlevel but apparently it works differently in XP and so set finpercent= errorlevel wont work as set sets an error level of 0 ??

Fight Crime,
Shoot Back !


0

Response Number 8
Name: dtech10
Date: February 13, 2007 at 16:06:36 Pacific
Reply:

Hi BabG
Try this

echo off
eval 3.5*3 > ~x.txt
set /p percent=<~x.txt
del ~x.txt
echo %percent%


0

Response Number 9
Name: dtech10
Date: February 13, 2007 at 16:14:10 Pacific
Reply:

Hi kgp
Sorry I got the name wrong.


0

Response Number 10
Name: kgp
Date: February 14, 2007 at 11:07:59 Pacific
Reply:

Thanks, that looks a lot better than this one I found buried in a batch txt manual somewhere.

echo set result= >setresult.txt
eval round (%Count2%/%Count1%*100) >fin.txt
copy setresult.txt + fin.txt tmp.bat > nul
call tmp.bat
del tmp.bat

It is a good idea though. Useful for other things !!

Looks like I have almost got this sorted.

Top Tip: use "eval round (2*3.4385)" to get a sensible number out for percentages, no decimals !!

Fight Crime,
Shoot Back !


0

Response Number 11
Name: kgp
Date: February 14, 2007 at 11:43:13 Pacific
Reply:

New problem, new post. It is a bit off topic !

Que is, How to get

echo drive >drive.txt

to put the word "drive" into the drive.txt without the space and new line after it ?? I am trying to get "drive" on the first line with nothing after it !!

Thanks for the help given in this post !!

Fight Crime,
Shoot Back !


0

Response Number 12
Name: dtech10
Date: February 14, 2007 at 14:30:57 Pacific
Reply:

Hi kgp
The space may be caused by a space after the
"echo drive^>drive.txt
the ^ stands for a space.
Why don't you want the newline is this so you
can append something on the same line after the "drive".



0

Response Number 13
Name: Mechanix2Go
Date: February 14, 2007 at 23:21:20 Pacific
Reply:

You can not echo into a file and not get a 'line break' [crlf or 0d0a].


=====================================
If at first you don't succeed, you're about average.

M2



0

Response Number 14
Name: kgp
Date: February 15, 2007 at 01:16:14 Pacific
Reply:

Hi dtech10,

I am using two batch files, one to copy files and one to pop up and show the percent copied. The 1st batch file asks the user to input the drive letter of my memory stick, the start folder, and saves it to drive.txt. The second batch file, the percent one, does this...

set /p drive=<drive.txt

to get the same drive letter as the 1st batch file but if there is anything after the letter, the space and new line after echo j>drive.txt, it gives a "cannot find file specified error" and closes. I'm trying to stop it putting anything after the j.

Fight Crime,
Shoot Back !


0

Response Number 15
Name: Mechanix2Go
Date: February 15, 2007 at 01:39:56 Pacific
Reply:

We've got this in another thread. let's stick to one or the other. My recent post in the other thread shows how to create a BAT with no 'line break'. Worth knowing about.

But for the present purpose, it seems much more straightforward to simply set a VAR which is available to the second bat.

::== bat1
@echo off
set /p letter=which drive?
::==

::== bat2
@echo off
echo the letter is: %letter%
::==


=====================================
If at first you don't succeed, you're about average.

M2



0

Sponsored Link
Ads by Google
Reply to Message Icon






Post Locked

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.


Go to Programming Forum Home


Sponsored links

Ads by Google


Results for: Percent Complete BAT File Attempt

ping bat file www.computing.net/answers/programming/ping-bat-file/16605.html

Help with bat file www.computing.net/answers/programming/help-with-bat-file/12224.html

BAT file to open multiple windows www.computing.net/answers/programming/bat-file-to-open-multiple-windows/7513.html