Link to home
Start Free TrialLog in
Avatar of xodos
xodos

asked on

visual basic code delete .bmp files from a directory

dear sir
i have in d:\logo some bmp files , i need a visual basic code that deletes all bmp files from c:\logo and select 1 image.bmp from to copy it to c:\images after a set of time ( 15 minutes ) , the script must select randomly any image from c:\logo each 15 minutes ,
thanks

Avatar of gabeso
gabeso
Flag of United Kingdom of Great Britain and Northern Ireland image

Is thiis in VBScript or in VB6?
Avatar of Jarodtweiss
Jarodtweiss

To delete all the files from a specific folder, you can use :

    Dim file As String
    For Each file In System.IO.Directory.GetFiles("d:\logo", "*.bmp")
      System.IO.File.Delete(file)
    Next
Avatar of xodos

ASKER

in visual basic 6
the code you gave works fine , but how to copy randomely from a directory a file .bmp and paste it to c:\images
To do a "random copy" every 15 minutes, you will have to use a timer.
eg you can do :

    Dim myTimer As New Timer
    myTimer.Interval = 15 * 60 * 1000 '15 minutes
    AddHandler myTimer.Tick, AddressOf Timer_Tick
    myTimer.Start()

each 15 mn, a new event will be raised, and you can handle it to select a random file and copy it where you want :

  Private Sub Timer_Tick(ByVal sender As Object, ByVal e As EventArgs)
    ' Get a random file
    Dim files As String() = System.IO.Directory.GetFiles("mypath")
    Dim myRand As New Random(DateTime.Now.Ticks)
    Dim i As Integer = myRand.Next(0, files.Length - 1)
    System.IO.File.Copy(files(i), "destination path")
  End Sub

Here we will use the DateTime.Now.Tick value to have a real random number in our list which will give us a random file
Oh sorry... that's VB.NET code... if it is for VB6 you will have to translate it a little bit...
Avatar of xodos

ASKER

would you please translate it ,
it will take less than a minute with you , but it will take a year with me to do it :D
Sub DeleteBMP(YourfolderPath as string) ' YourfolderPath must contain the "\" at the end
'already
dim F as string
f=dir(yourfolder+"*.bmp")
while (f<>"")
     kill f
     f=Dir()
wend
end sub
'--------------------------
Function SelectRandomBmp(Folderpath as string )as string ' Returns the bmp full name, or "" if doesnt exist.
Dim A() as string, i as integer,F as string, J as integer
i=0
f=dir(folderpath+"*.bmp")
while (f<>"")
     i=i+1
     redim preserve A(i)
     A(i)=F
     F=Dir()
wend
if i>0 then
Randomize timer
J=int(rnd*i)+1
SelectRandomBmp=A(j)
else
selectrandombmp=""
end if
end function


Its four AM in Argentina, where i am. So, i could miss a point or comma. But i hope it helps.
ASKER CERTIFIED SOLUTION
Avatar of Mike Tomlinson
Mike Tomlinson
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
to copy files
Filecopy "c:\logo", "c:\images"

to delete files
Kill "c:\logo\*.bmp"

Avatar of xodos

ASKER

hello idle mind
i created a project and added in it a timer ,
but the code deletes all bmp files from c:\logo , and put only one image in c:\images , while it must do this :
select a bmp file from c:\logo and put it in c:\images , and after 15 minutes it must delete the file from c:\images and select another bmp file from c:\logo until it will select all files ,
thanks
So sorry about that...that is actually what you asked for in your original description:

    "i need a visual basic code that deletes all bmp files from c:\logo"

Anyhoo, it's easy enough to fix.  I'll post back later.

~IM