Need a script to delete files with a number in the neame

Posted on 2008-11-12
Last Modified: 2012-05-05
Hi experts!
I need this fast so I don't have the time to hash through it, and besides, I know somebody out there has already solved it  :)

I need a script that will look at the file names in a folder and delete the ones containing a number.
I have:
I need to delete all the  "file1", "file2", and "file3"..., but leave "file" intact
batch or VBscript will be the easiest to implement..
Question by:drmarston
    LVL 82

    Assisted Solution

    Try the script below, just adjust the folder name at the beginning; it will delete anything with a number in the name found in this folder (it won't process subfolders at the moment).
    The script is currently in test mode, it will only display the "del" commands it would otherwise run, but not actually delete anything, so that you can test it.
    To run it for real, remove the capitalized "ECHO" in front of the "del" command.
    @echo off
    set Root=C:\Folder
    for /f "delims=" %%a in ('dir /b /a:-d "%Root%\*.*" ^| findstr /r ".*[0-9][0-9]*.*"') do (
      Deleting %%a ...
      ECHO del "%Root%\%%a"

    Open in new window

    LVL 67

    Accepted Solution


    Dim objFSO : Set objFSO = CreateObject("Scripting.FileSystemObject")
    Dim objFolder : Set objFolder = objFSO.GetFolder(ParentFolder)
    For Each File in objFolder.Files
      strFile = Replace(, "." & objFSO.GetExtensionName(file),"")
      If IsNumeric(Right(strFile,1)) Then
        wscript.echo "Deleting " &
        objFSO.DeleteFile (file)
      End If

    Open in new window

    LVL 5

    Author Closing Comment

    Sirbounty: works perfectly, Thanks!
    oBdA: I got an error with yours, but appreciate the speedy response!

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Why You Should Analyze Threat Actor TTPs

    After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

    This script will sweep a range of IP addresses (class c only, and report to a log the version of office installed. What it does: 1.)      Creates log file in the directory the script is run from (if it doesn't already exist) 2.)      Sweep…
    When you receive another warning that your shared drive is almost full and you have asked your users to clean out old files again and again, here is a single command that may help. This command will place all the files that have not been used rec…
    Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
    The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)

    761 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

    7 Experts available now in Live!

    Get 1:1 Help Now