Solved

vbs script to delete first file in a directory

Posted on 2011-02-18
8
473 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 84

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
Simplifying Server Workload Migrations

This use case outlines the migration challenges that organizations face and how the Acronis AnyData Engine supports physical-to-physical (P2P), physical-to-virtual (P2V), virtual to physical (V2P), and cross-virtual (V2V) migration scenarios to address these challenges.

 

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 500 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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Chrome VM 1 38
using interface in TLB 3 30
Installing security patches for Windows 7 - JUST the security patches 6 45
RDP exploit 13 21
Learn how to PXE Boot both BIOS & UEFI machines with DHCP Policies and Custom Vendor Classes
This article summaries thoughts and ideas from two years of sustained use. It provides good reasoning to make the jump to Windows 10.
The viewer will learn how to use a discrete random variable to simulate the return on an investment over a period of years, create a Monte Carlo simulation using the discrete random variable, and create a graph to represent the possible returns over…
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…

739 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