Solved

Using VBScript to delete files

Posted on 2003-11-17
4
3,027 Views
Last Modified: 2011-09-20
Using Vbscript i would like to delete any batch file located under any User's Start Up Folder on Windows 2000. For example under the document and settings if i logged in with my user account "test" then I wouild like to delete any batch files found in C:\Documents and Settings\test\Start Menu\Programs\Startup... also if i looged in as "test2"  I wouild like to delete any batch files found in C:\Documents and Settings\test2\Start Menu\Programs\Startup. So this script would chage the location of the file according to the user that is logged in to the computer.
0
Comment
Question by:gbarrientos
  • 2
4 Comments
 
LVL 9

Accepted Solution

by:
malharone earned 500 total points
ID: 9768183
hope this provides a startup

Dim FSO As Object
Set FSO = CreateObject("Scripting.FileSystemobject")
Set documentSettingFolder = FSO.GetFolder("C:\Documents and Settings\")
Set userFolders = documentSettingFolder.SubFolders
For Each userFolder In userFolders
      FSO.DeleteFile(userFolder & "\Start Menu\Programs\Startup\*.bat")
Next
msgbox("Done deleting batch files")
0
 
LVL 9

Expert Comment

by:bhagyesht
ID: 9770260
gbarrientos: there is a special folder property that directly gives the path to the startup folder


set WshShell = WScript.CreateObject("WScript.Shell")
strStartup = WshShell.SpecialFolders("startup")
Set fso = CreateObject("Scripting.FileSystemObject")
Set f = fso.GetFolder(strStartup)
For Each x In f.files
      if right(x.name,4)=".bat" then
          FSO.DeleteFile(x)
      end if
Next

0
 

Expert Comment

by:gari55zw
ID: 9770692
/* hopefully this will help coz its doing some wonders for me, just put it as a function */


Public Function funtname(delDays as integer, scanDir as string)

   Dim fs As Object
   
   On Error GoTo errorHandler
   Set fs = CreateObject("Scripting.FileSystemObject")
   
   ' CHECK IF THE DIRECTORY EXISTS
   If Dir$(scanDir, vbDirectory) <> "" Then
      ' DIRECTORY DOES EXIST, SO GO ON ...
      Dim fileDate As Date   ' Declare variables.
      Dim f, f1, fc
     
      Set f = fs.GetFolder(scanDir)
      Set fc = f.Files
      For Each f1 In fc
         fileDate = f1.DateCreated
         If DateDiff("d", fileDate, Now) > delDays Then
            f1.Delete

         End If
         
      Next

   End If
   Set fs = Nothing
   Exit Function
   
errorHandler:
   Resume Next
End Function
0
 
LVL 9

Expert Comment

by:bhagyesht
ID: 9776630
gbarrientos: what you have accepted may not work if you execute the script with a non admin previlages besides it doesnt work as you asked. "So this script would chage the location of the file according to the user that is logged in to the computer. "

What I have mentioned does that I gets the startup folder depending on the user logged in. besides there would be no issues of rights since the user should normally be allowed to modify his/her settings.
0

Featured Post

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
how to use wail2ban ?? 13 115
Batch/VBScript : Disable Windows tasks 7 34
SQL Insert parts by customer 12 31
How can i echo a ! to end of a file? 8 15
Entering a date in Microsoft Access can be tricky. A typo can cause month and day to be shuffled, entering the day only causes an error, as does entering, say, day 31 in June. This article shows how an inputmask supported by code can help the user a…
Computer science students often experience many of the same frustrations when going through their engineering courses. This article presents seven tips I found useful when completing a bachelors and masters degree in computing which I believe may he…
An introduction to basic programming syntax in Java by creating a simple program. Viewers can follow the tutorial as they create their first class in Java. Definitions and explanations about each element are given to help prepare viewers for future …
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…

813 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now