Frans_Truyens
asked on
vbs script to delete first file in a directory
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.
Ops! You told only one file!
Here is tyhe corrected script
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
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:
To delete the oldest file:
To delete the alphabetically first file:
f
And the alphabetically last file:
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%"
To delete the oldest file:
for /f "delims=" %%a in ('dir /a:-d /o:-d /b') do set DeleteFile=%%a
ECHO del "%DeleteFile%"
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%"
And the alphabetically last file:
for /f "delims=" %%a in ('dir /a:-d /o:n /b') do set DeleteFile=%%a
ECHO del "%DeleteFile%"
ASKER
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.
With my second post you can delete only a file.
But again. First by name or what?
But again. First by name or what?
ASKER
That looks better. How do I delete the newest one? With VBS please
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thanks a lot. This solved my problem
First created file?
First name?
Random one of them?
Is this code the one you are looking for?
Open in new window