Hello everyone:

I have the following issue, i have and old computer and i need to erase pics and all that, but that computer has several folders and some are hidden, and is very time consuming checking where are those folders.

I would like a VBS to erase all the pics from that computer and to show and erase the hidden ones.

Hope someone can help with this.

Note: there is no need to format the computer, just to erase all the pices and the format of those picks are .jpeg and .jpg

Thanks in advance

Dani AI

Generated

’s request and the earlier replies from , and point to the same conclusion: a recursive scan is the right approach. The script below performs a recursive walk starting from a configurable folder, lists every .jpg/.jpeg it finds, writes a log, and can run in a safe "dry run" mode before any deletion. Important caution: the Delete method used here permanently removes files (bypasses the Recycle Bin). Test on a small folder and keep an independent backup before running against an entire drive.

' delete_jpegs.vbs  — recursive scanner with dry-run and logging
Option Explicit

Dim fso, logHandle, startFolder, dryRun
Dim listedCount, deletedCount

startFolder = "C:\"    ' change to a test folder before scanning whole drive
dryRun = True         ' True = list only; False = delete permanently

Set fso = CreateObject("Scripting.FileSystemObject")
Set logHandle = fso.CreateTextFile("delete_jpegs_log.txt", True)

listedCount = 0
deletedCount = 0

Sub Log(msg)
  logHandle.WriteLine Now & "  " & msg
End Sub

Sub ProcessFolder(path)
  Dim folder, file, subfolder, ext
  On Error Resume Next
  Set folder = fso.GetFolder(path)
  If Err.Number <> 0 Then
    Log "ACCESS ERROR: " & path & " - " & Err.Description
    Err.Clear
    On Error GoTo 0
    Exit Sub
  End If
  On Error GoTo 0

  For Each file In folder.Files
    ext = LCase(fso.GetExtensionName(file.Name))
    If ext = "jpg" Or ext = "jpeg" Then
      listedCount = listedCount + 1
      Log "FOUND: " & file.Path
      If Not dryRun Then
        On Error Resume Next
        file.Delete True
        If Err.Number = 0 Then
          deletedCount = deletedCount + 1
          Log "DELETED: " & file.Path
        Else
          Log "DELETE FAILED: " & file.Path & " - " & Err.Description
          Err.Clear
        End If
        On Error GoTo 0
      End If
    End If
  Next

  For Each subfolder In folder.SubFolders
    ProcessFolder subfolder.Path
  Next
End Sub

Log "START: " & startFolder
ProcessFolder startFolder
Log "FINISHED. FOUND=" & listedCount & " DELETED=" & deletedCount
logHandle.Close

Operational notes and quick troubleshooting:

  • Set startFolder to a safe test path and leave dryRun = True for an initial scan.
  • Run from an elevated command prompt with cscript delete_jpegs.vbs to capture console output; administrator rights may be required for protected folders.
  • Permission errors and locked files are logged; files in use may require Safe Mode or mounting the drive on another machine.
  • Long path (>260 chars) issues can be worked around by mapping a deeper folder to a drive letter before scanning.
  • To target additional extensions, extend the If ext = ... check or switch to an array-based match.

This ties the command-line/listing idea mentioned earlier with a safe, test-first recursive approach suggested by .

Recommended Answers

All 5 Replies

You need to write a recursive function to find and delete all the files of specific format.

Kindly post the code that you are working on currently.

You need to write a recursive function to find and delete all the files of specific format.

Kindly post the code that you are working on currently.

I`m new to this VBs so i have not worked on any code yet, i was hopping someone could give me en example or a guide on how to start with

You actually don't need vbscript to do this. Go to a DOS prompt and type

dir /?

That will show you how to get a list of all the things you need. Try this:

dir *.jp* /s /b > myfiles.txt

This will give you a list of all files (with directory information!) that have .jp* extension (e.g. .jpg, jpeg) and store it in a text file. You can then use a good text editor to add a del statement to each line, or to remove lines if you don't want to delete the files in question. Kind of old-school, I know, but if it's a one-time task, why spend the time to learn a new language?

You actually don't need vbscript to do this. Go to a DOS prompt and type

dir /?

That will show you how to get a list of all the things you need. Try this:

dir *.jp* /s /b > myfiles.txt

This will give you a list of all files (with directory information!) that have .jp* extension (e.g. .jpg, jpeg) and store it in a text file. You can then use a good text editor to add a del statement to each line, or to remove lines if you don't want to delete the files in question. Kind of old-school, I know, but if it's a one-time task, why spend the time to learn a new language?

I agree. But this won't work with hidden folders. But then again, if they are hidden folders, you wouldn't have pictures in them, would you?

Alternatively, look at ZTREE. It will allow you do do exactly what you need with little fuss.

You need to read this and this.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.