Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

vbs script to delete first file in a directory

Posted on 2011-02-18
8
Medium Priority
?
483 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Connect further...control easier

With the ATEN CE624, you can now enjoy a high-quality visual experience powered by HDBaseT technology and the convenience of a single Cat6 cable to transmit uncompressed video with zero latency and multi-streaming for dual-view applications where remote access is required.

 

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

Will your db performance match your db growth?

In Percona’s white paper “Performance at Scale: Keeping Your Database on Its Toes,” we take a high-level approach to what you need to think about when planning for database scalability.

Question has a verified solution.

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

This article shows how to use a free utility called 'Parkdale' to easily test the performance and benchmark any Hard Drive(s) installed in your computer. We also look at RAM Disks and their speed comparisons.
Windows Server 2003 introduced persistent Volume Shadow Copies and made 2003 a must-do upgrade.  Since then, it's been a must-implement feature for all servers doing any kind of file sharing.
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 …
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…

661 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