• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 174
  • Last Modified:

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

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
drmarston
Asked:
drmarston
2 Solutions
 
oBdACommented:
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
 
sirbountyCommented:

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
 
drmarstonAuthor Commented:
Sirbounty: works perfectly, Thanks!
oBdA: I got an error with yours, but appreciate the speedy response!
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: CompTIA Cloud+

The CompTIA Cloud+ Basic training course will teach you about cloud concepts and models, data storage, networking, and network infrastructure.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now