Solved: .bat file doesn't work

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
fr33z3r
Posts: 4
Joined: 22 May 2009 13:27

Solved: .bat file doesn't work

#1 Post by fr33z3r » 22 May 2009 13:34

Hi guys

I'm new here and not used to writing batch files

I tried writing a file that does the following

Code: Select all

start:
   
   IF the free diskspace on C:\ is over 2GB

      do nothing

   ELSE
      
      delete the alfabetically first subdirectory of C:\measuringresults
      jump to start

   END IF


END


I created the following file
NOTE that the language of the system used is Dutch, hence the diskspace deleting the first 5 words

Code: Select all

code:

@echo off
color f0
:begin

fsutil volume diskfree c: > C:\temp.txt
setlocal
for /f "tokens=1*" %%a in (C:\temp.txt) do (
   set vrij=%%b
   goto endloop1
)


:endloop1

pause

for /f "tokens=1,2,3,4,5 delims=/ " %%a in ("%vrij%") do set size=%%e

if %size% GTR 2000000000 goto end

call listEN.bat
pause
for /f "tokens=2*" %%a in (C:\temp.txt) do (
   set name=%%a
   echo name
goto endloop2
)

:endloop2
echo %name%
echo del C:\measuringresults\%name%
pause
:end
del C:\temp.txt




Code: Select all

code listEN.bat:


@echo off
dir /B measuringresults > C:\temp.txt




what happens is this:
the result of the fsutil volume diskree C: function is stored in C:\tempvolfile
the number of free bytes is filtered and stored in "size"
listEN.bat gets called an executed correctly, temp.txt contains the subdirectories as it should
IT GOES WRONG HERE: I want "name" to contain the first subdirectory of "measuringresults" = the first line from temp.txt . It stays empty though

I can't figure out why I can't get "name" get the value of the first string from the temp file, while I could get "size" to the number of free bytes

Anyone here who can help me out?

Thx in advance
Last edited by fr33z3r on 28 May 2009 17:10, edited 1 time in total.

avery_larry
Expert
Posts: 391
Joined: 19 Mar 2009 08:47
Location: Iowa

#2 Post by avery_larry » 22 May 2009 14:29

First problem:

Code: Select all

if %size% GTR 2000000000 goto end


the if GTR only works up to about 2150000000. Once %size% is larger than that (approximately), the compare will not work correctly. I would do this:

Code: Select all

if 1%size:~0,-8% GTR 120 goto end


For the question you asked, I'm pretty sure your problem is the tokens=2*. It should be tokens=*

Of course, you should sort that listing, and you can skip the temp file like this:

Code: Select all

for /f "tokens=*" %%a in ('dir /ad /o-n /b c:\measuringresults) do set name=%%a


Also, when you actually want to delete the subdirectory, use:

rd /s /q "C:\measuringresults\%name%"

fr33z3r
Posts: 4
Joined: 22 May 2009 13:27

#3 Post by fr33z3r » 22 May 2009 14:52

Thanks man!

I got to the following code, and it's very close to what I want. The line you gave me to skip the list file didn't work, but this one does

Code: Select all


@echo off
color f0
:begin

fsutil volume diskfree c: > C:\temp.txt
setlocal
for /f "tokens=1*" %%a in (C:\temp.txt) do (
   set vrij=%%b
   goto endloop1
)


:endloop1

pause

for /f "tokens=1,2,3,4,5 delims=/ " %%a in ("%vrij%") do set size=%%e

if 1%size:~0,-8% GTR 120 goto end

call listEN.bat
pause
for /f "tokens=*" %%a in (C:\temp.txt) do (
   set name=%%a
   echo name
goto endloop2
)

:endloop2
echo %name%

rd /s /q "C:\measuringresults\%name%"

goto begin

pause
:end
del C:\temp.txt


just 1 more thing:
if the free disk space is too small and the folder C:\measuringresults is empty, the program is in an eternal loop. So I should need something like

Code: Select all

if temp.txt is empty goto end


Any idea?

avery_larry
Expert
Posts: 391
Joined: 19 Mar 2009 08:47
Location: Iowa

#4 Post by avery_larry » 23 May 2009 20:30

If the alphabetical part for removing the subdirectories is important, you need to change your listen.bat file needs to be:

dir /on /B measuringresults > C:\temp.txt

the /ad switch will list only directories.

And my for loop -- I forgot the closing single quote:

for /f "tokens=*" %%a in ('dir /ad /o-n /b c:\measuringresults') do set name=%%a

You can add an arbitrary maximum number of loops by simply incrementing a variable and testing the value of that variable. Or you can check that the variable name is defined (this would have to be tested).

Code: Select all

set count=0
:begin
if /i %count% GTR 20 echo Too many loops. && goto end
set /a count +=1

. . .


OR

Code: Select all

:endloop2 
if not defined name echo Nothing left to delete. && goto end

fr33z3r
Posts: 4
Joined: 22 May 2009 13:27

#5 Post by fr33z3r » 24 May 2009 01:53

thx

I used

Code: Select all

:endloop2 
if not defined name echo Nothing left to delete. && goto end


this works great if the folder measuringresults is empty before running the file. If however the folder contains subdirectory A, B and C and the diskspace is too small then

name = A
A gets deleted
name = B
B gets deleted
name = C
C gets deleted
name = C
system cannot find file named measuringresults\C

as for limiting the number of loops, that's not an option for me

Anyway, thanks for your help, I really appreciate it

avery_larry
Expert
Posts: 391
Joined: 19 Mar 2009 08:47
Location: Iowa

#6 Post by avery_larry » 26 May 2009 10:38

This will do it:

Code: Select all

call listEN.bat 
pause
set name=
for /f "tokens=*" %%a in (C:\temp.txt) do (
   set name=%%a
   echo name
goto endloop2
)


Just add
set name=
before the for loop. Then the if defined will work.

fr33z3r
Posts: 4
Joined: 22 May 2009 13:27

#7 Post by fr33z3r » 28 May 2009 17:09

I did it like this

Code: Select all


rd /s /q "C:\measuringresults\%name%"

set name=%

goto begin


and it worked. Still, thanks a lot for your help

Grts

Post Reply