Solved

vbs script to delete first file in a directory

Posted on 2011-02-18
8
455 Views
Last Modified: 2012-05-11
I am looking for a vbs script in a directory. I just want to delete the first one, not all of them. Also I don't know its name.
0
Comment
Question by:Frans_Truyens
  • 4
  • 3
8 Comments
 
LVL 7

Expert Comment

by:kemi67
Comment Utility
What do you mean by first file?
First created file?
First name?
Random one of them?
Is this code the one you are looking for?
Dim fso, folder, files, sFolder, flf
  
  Set fso = CreateObject("Scripting.FileSystemObject")
  sFolder = "c:\tmp"
  Set folder = fso.GetFolder(sFolder)
  Set files = folder.Files
  
  For each flf In files
    flf.Delete
  Next

Open in new window

0
 
LVL 7

Expert Comment

by:kemi67
Comment Utility
Ops! You told only one file!
Here is tyhe corrected script
Dim fso, folder, files, sFolder, flf
  
  Set fso = CreateObject("Scripting.FileSystemObject")
  sFolder = "c:\tmp"
  Set folder = fso.GetFolder(sFolder)
  Set files = folder.Files
  
  For each flf In files
    flf.Delete
    exit for
  Next

Open in new window

0
 
LVL 82

Expert Comment

by:oBdA
Comment Utility
Well, what constitutes "first one"? In alphabetical order, by date, by size, by name length, by extension, ...?
Some examples for batch (.cmd) files (all in test mode, remove the ECHO to run them for real):

To delete the newest file in the current folder:
for /f "delims=" %%a in ('dir /a:-d /o:d /b') do set DeleteFile=%%a
ECHO del "%DeleteFile%"

Open in new window


To delete the oldest file:
for /f "delims=" %%a in ('dir /a:-d /o:-d /b') do set DeleteFile=%%a
ECHO del "%DeleteFile%"

Open in new window


To delete the alphabetically first file:
f
or /f "delims=" %%a in ('dir /a:-d /o:-n /b') do set DeleteFile=%%a
ECHO del "%DeleteFile%"

Open in new window


And the alphabetically last file:
for /f "delims=" %%a in ('dir /a:-d /o:n /b') do set DeleteFile=%%a
ECHO del "%DeleteFile%"

Open in new window

0
 

Author Comment

by:Frans_Truyens
Comment Utility
I don't want to delete them all. I just want to delete the first one in the list. The date is not important, the size is not important. I just want to delete the first one.
0
How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

 
LVL 7

Expert Comment

by:kemi67
Comment Utility
With my second post you can delete only a file.
But again. First by name or what?
0
 

Author Comment

by:Frans_Truyens
Comment Utility
That looks better. How do I delete the newest one? With VBS please
0
 
LVL 7

Accepted Solution

by:
kemi67 earned 500 total points
Comment Utility
With this code you can delete the last modified file
Dim fso, folder, files, sFolder, flf, lastTime,LastFile

  
  Set fso = CreateObject("Scripting.FileSystemObject")
  sFolder = "c:\tmp"
  Set folder = fso.GetFolder(sFolder)
  Set files = folder.Files
  'Search last modified file
 lastTime=0
 LastFile=""
  For each flf In files
    if LastTime<flf.DateLastModified Then
       lastTime= flf.DateLastModified 
       LastFile=flf.Path 
    end if   
  Next
  
  fso.Deletefile( LastFile)

Open in new window

0
 

Author Closing Comment

by:Frans_Truyens
Comment Utility
Thanks a lot. This solved my problem
0

Featured Post

Are your corporate email signatures appalling?

Is it scary how unprofessional your email signatures look? Do users create their own terrible designs and give themselves stupid job titles? You can make this a lot easier for yourself by choosing an email signature management solution from Exclaimer today.

Join & Write a Comment

Article by: Leon
Software Metering within our group of companies has always been an afterthought until auditing of software and licensing became a pain point. Orchestrator and SCCM metering gave us the answer and it was an exciting process.
NTFS file system has been developed by Microsoft that is widely used by Windows NT operating system and its advanced versions. It is the mostly used over FAT file system as it provides superior features like reliability, security, storage, efficienc…
The viewer will learn how to create a normally distributed random variable in Excel, use a normal distribution to simulate the return on an investment over a period of years, Create a Monte Carlo simulation using a normal random variable, and calcul…
With the advent of Windows 10, Microsoft is pushing a Get Windows 10 icon into the notification area (system tray) of qualifying computers. There are many reasons for wanting to remove this icon. This two-part Experts Exchange video Micro Tutorial s…

762 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

6 Experts available now in Live!

Get 1:1 Help Now