Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

vbs script to delete first file in a directory

Posted on 2011-02-18
8
Medium Priority
?
486 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
ID: 34925789
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
ID: 34925806
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 85

Expert Comment

by:oBdA
ID: 34925816
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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 

Author Comment

by:Frans_Truyens
ID: 34925817
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
 
LVL 7

Expert Comment

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

Author Comment

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

Accepted Solution

by:
kemi67 earned 2000 total points
ID: 34926120
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
ID: 34926413
Thanks a lot. This solved my problem
0

Featured Post

Prepare for your VMware VCP6-DCV exam.

Josh Coen and Jason Langer have prepared the latest edition of VCP study guide. Both authors have been working in the IT field for more than a decade, and both hold VMware certifications. This 163-page guide covers all 10 of the exam blueprint sections.

Question has a verified solution.

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

This article provides a convenient collection of links to Microsoft provided Security Patches for operating systems that have reached their End of Life support cycle. Included operating systems covered by this article are Windows XP,  Windows Server…
It is a real story and is one of my scariest tech experiences. Most users think that IT experts like us know how to fix all computer problems. However, if there is a time constraint and you MUST not fail the task or you will lose your job, a simple …
The viewer will learn how to create two correlated normally distributed random variables in Excel, use a normal distribution to simulate the return on different levels of investment in each of the two funds over a period of ten years, and, create a …
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …

916 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