Tom's Guide | Tom's Hardware | Tom's Games
![]() |
![]() |
![]() |
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 ??

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

Hi
Something like this help.-----
@echo off
setlocal EnableDelayedExpansionset 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+=1echo %Count1%
echo %Count2%
set /a Percent=%Count1%/%Count2%*100
echo %Percent%

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 EnableDelayedExpansionfor /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

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 !

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 !

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=0for /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+=1echo %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
:endcls
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 !

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 !

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.batIt 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 !

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 !

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".

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

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 !

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

![]() |
![]() |
![]() |

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