[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

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

Posted on 2008-11-12
3
Medium Priority
?
169 Views
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:
c:\folder\file
c:\folder\file1
c:\folder\file2
c:\folder\file3
etc
I need to delete all the  "file1", "file2", and "file3"..., but leave "file" intact
batch or VBscript will be the easiest to implement..
Thanks!
0
Comment
Question by:drmarston
3 Comments
 
LVL 85

Assisted Solution

by:oBdA
oBdA earned 200 total points
ID: 22941131
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
setlocal
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

0
 
LVL 67

Accepted Solution

by:
sirbounty earned 1800 total points
ID: 22941142

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

Open in new window

0
 
LVL 5

Author Closing Comment

by:drmarston
ID: 31515984
Sirbounty: works perfectly, Thanks!
oBdA: I got an error with yours, but appreciate the speedy response!
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Introduction: Recently, I got a requirement to zip all files individually with batch file script in Windows OS. I don't know much about scripting, but I searched Google and found a lot of examples and websites to complete my task. Finally, I was ab…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
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 how to count occurrences of each item in an array.
Suggested Courses
Course of the Month19 days, 7 hours left to enroll

873 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