Write names of files in a folder that have been created/modified today to text file - vbscript

I have written a VBScript to write all the file names in a folder to a text file, but what I actually want is a listing of only the names of .txt files in a specific folder that have been modified or created today.
The code:
Dim fso
Dim ObjFolder
Dim ObjOutFile
Dim ObjFiles
Dim ObjFile
Set fso = CreateObject("Scripting.FileSystemObject")
Set ObjFolder = fso.GetFolder("D:\test")
Set ObjOutFile = fso.CreateTextFile("D:\test\File_names.txt")
Set ObjFiles = ObjFolder.Files
For Each ObjFile In ObjFiles
ObjOutFile.WriteLine(ObjFile.Name)
Next
ObjOutFile.Close

Its a long time since I have written any VBScript but this is along the right lines. You may need to adjust it a bit
Dim fso
Dim ObjFolder
Dim ObjOutFile
Dim ObjFiles
Dim ObjFile
Set fso = CreateObject("Scripting.FileSystemObject")
Set ObjFolder = fso.GetFolder("c:\temp\")
Set ObjOutFile = fso.CreateTextFile("c:\temp\File_names.txt")
Set ObjFiles = ObjFolder.Files
For Each ObjFile In ObjFiles
If (LCase(Mid(objFile.Name, InStrRev(objFile.Name, "."))) = ".txt") Then
If FormatDateTime(ObjFile.DateLastModified, 2) = FormatDateTime(Now(), 2) OR FormatDateTime(ObjFile.DateCreated , 2) = FormatDateTime(Now(), 2) Then
ObjOutFile.WriteLine(ObjFile.Name)
Else
ObjOutFile.WriteLine(ObjFile.Name & " - Modified:" & FormatDateTime(ObjFile.DateLastModified, 2) & " " & FormatDateTime(Now(),2) & " Created: " & FormatDateTime(ObjFile.DateCreated , 2) & " " & FormatDateTime(Now(), 2) )
End if
Else
ObjOutFile.WriteLine(ObjFile.Name & " Not Text")
End if
Next
ObjOutFile.Close

A code snippet with changes only:
For Each ObjFile In ObjFiles
If StrComp( Right( ObjFile.name, 4), ".txt", vbTextCompare) = 0 and _
( Fix( ObjFile.DateCreated) = Fix( Now) or _
Fix( ObjFile.DateLastModified) = Fix( Now) ) Then
ObjOutFile.WriteLine(ObjFile.Name)
End If
Next
FIX() explanation:
date format is a floating point value, counting days since midnight 30
December 1899. Hours and minutes are represented as fractional days

Related

How to remove part of filename using vbscript

I have hundreds of filenames with a - (dash) 0 (zero) like V-45X-0892-0.pdf, V-45X-0893-0.pdf and would like to strip the -0 from filename.
Here's what I have but is not working.
folderspec = "C:\Dave\"
strRename = ""
Set fs = CreateObject("Scripting.FileSystemObject")
Set f = fs.GetFolder(folderspec)
For Each f1 In f.Files
Filename = folderspec & f1.Name
NewFilename = Replace(Filename, "%-0.pdf""%.pdf", strRename)
fs.MoveFile Filename, NewFilename
Next
MsgBox "All Done"
Here's code based on your Question with modifications made to the For Each loop. Note that you don't need to use MoveFile, you can simply update the file's Name property directly:
Dim objFSO
Dim objFolder
Dim objFile
Dim sFolderSpec
sFolderSpec = "C:\Dave\"
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFolder = objFSO.GetFolder(sFolderSpec)
For Each objFile In objFolder.Files
If InStr(objFile.Name, "-0") > 0 Then objFile.Name = Replace(objFile.Name, "-0", "")
Next
MsgBox "All Done"

How do I delete the oldest file in a backup folder if there is more than 15 files in the folder after I made the new backup using vbscript?

How do I delete the oldest file in a backup folder if there is more than 15 files in the folder after I made the new backup using vbscript?
I found that my Backups take up plenty of space on my hdd
Is it possible to do it by counting the number of files in a folder? My backups are named "ST-06.02.18 07h20.zip" . I can always change the name if it wil make it easier...xd
Dim objFSO, objFolder, strDirectory, dNow, yy, mt, dd, hh, nn, objShell, dOpen
Set objFSO = CreateObject("Scripting.FileSystemObject")
strDirectory = "c:\test\"
If objFSO.FolderExists(strDirectory) Then
Set objFolder = objFSO.GetFolder(strDirectory)
WScript.Echo strDirectory & " already created "
Else
Set objFolder = objFSO.CreateFolder(strDirectory)
WScript.Echo "Just created " & strDirectory
End If
dNow = Now
yy = Right(Year(dNow), 2)
mt = Right("00" &Month(dNow), 2)
dd = Right("00" &Day(dNow), 2)
hh = Right("00" &Hour(dNow), 2)
nn = Right("00" &Minute(dNow), 2)
ss = Right("00" &Second(dNow), 2)
Compress "C:\Program Files\ST\Db" ,strDirectory & "ST-" &dd & "." &mt & "." &yy & " " &hh & "h" &nn &".zip"
Sub Compress(Input, ZipFile)
Dim Shell : Set Shell = CreateObject("Shell.Application")
Dim FSO : set FSO = CreateObject("Scripting.fileSystemObject")
FSO.CreateTextFile(ZipFile, true).WriteLine "PK" & Chr(5) & Chr(6) & String(18, 0)
Set ZipFile = Shell.NameSpace(ZipFile)
ZipFile.CopyHere Input
Do Until ZipFile.items.Count = 1
'important, makes the script not fall out and dispose of objects before they are done
'items.count is the amount of root items you anticipate to be in the zip file
wscript.sleep 200
Loop
Set Shell = Nothing
Set FSO = Nothing
Set ZipFile = Nothing
End Sub
Set objShell = CreateObject("Wscript.Shell")
dOpen = "explorer.exe /e," & strDirectory
objShell.Run dOpen
Set fso = CreateObject("Scripting.FileSystemObject")
Set F = fso.GetFolder("C:\Users\David Candy\Desktop\New Folder\Stories\Test")
If F.size > 2^30 Then
Set rs = CreateObject("ADODB.Recordset")
With rs
.Fields.Append "Date", 7
.Fields.Append "Txt", 201, 5000
.Open
For Each Thing in f.files
.AddNew
.Fields("Date").value = thing.datelastmodified
.Fields("Txt").value = thing.path
.UpDate
Next
.Sort = "Date Desc"
Do While not .EOF
fso.deletefile .Fields("Txt").Value
msgbox f.size
If f.size < 2^30 then Exit Do
.MoveNext
Loop
End With
End If
This sample code runs when the folder is greater than 2 gig and deletes the oldest files until under 2 gig.
It uses a disconnected recordset created in memory to sort files by last modified.

Vbscript copy files based on beginning letters

I am trying to get this script to copy all files starting with "XX". Currently it only copies one file.
Dim objFSO, colFiles, objFile, strDestFolder, objNewestFile
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set colFiles = objFSO.GetFolder("C:\source")
strDestFolder = "C:\destination\"
For Each objFile In colFiles.Files
If Left(objFile.Name, 2) = "XX" Then
If objNewestFile = "" Then
Set objNewestFile = objFile
Else
If objNewestFile.DateLastModified < objFile.DateLastModified Then
Set objNewestFile = objFile
End If
End If
End If
Next
If Not objNewestFile Is Nothing Then
objFSO.CopyFile objNewestFile.Path,strDestFolder,True
End If
WScript.Echo "Copied."
You can use wildcards * and ? in [source] argument of FSO .CopyFile method.
So the code may look like:
Dim objFSO
Set objFSO = CreateObject("Scripting.FileSystemObject")
objFSO.CopyFile "C:\source\XX*.*", "C:\destination\", True
WScript.Echo "Copied."

list all files in a folder and sub folder without extention

I've come across the following script that I'd really like to use but I would like it not to have the .extention at the end
Dim fso
Dim ObjOutFile
Set fso = CreateObject("Scripting.FileSystemObject")
Set ObjOutFile = fso.CreateTextFile("C:\Users\User\Movies.csv")
ObjOutFile.WriteLine("Type,File Name,Size")
GetFiles("E:\")
ObjOutFile.Close
Function GetFiles(FolderName)
On Error Resume Next
Dim ObjFolder
Dim ObjSubFolders
Dim ObjSubFolder
Dim ObjFiles
Dim ObjFile
Set ObjFolder = fso.GetFolder(FolderName)
Set ObjFiles = objfolder.Files
For Each ObjFile In ObjFiles
ObjOutFile.WriteLine("File," & ObjFile.Name & "," & objFile.Size & "," & objFile.Type)
Next
Set ObjSubFolders = ObjFolder.SubFolders
For Each ObjFolder In ObjSubFolders
ObjOutFile.WriteLine("Folder," & ObjFolder.Name)
GetFiles(ObjFolder.Path)
Next
End Function
I'm rubbish at this but I would really apperciate the help
Use the .GetBaseName() method of the FileSystemObject. As in:
>> WScript.Echo goFS.GetBaseName("c:\dir\name.ext")
>>
name

Do I need a wait time for setting a new folder vbs?

I am using the following code:
Set StorageFileSystem = CreateObject("Scripting.fileSystemObject")
Set StorageFolder = StorageFileSystem.GetFolder(PathToStorageFiles)
msgBox "Set folders for Storage"
for each Storagefile in StorageFolder.Files 'get the creation time of the oldest recording
msgBox "DateCreated: " & Storagefile.DateCreated & vbCrLf & "EarylDateTime: " & earlyDateTime & vbCrLf & "DateTime to compare: " & dateadd("h" ,-6, Now)
if Storagefile.DateCreated < dateadd("h" ,-6, Now) then
earlyDateTime = Storagefile.DateCreated
end if
next
I have used this before without problem, even in the program that this is in. However this time it never seems to do anything. The folder has over 130,000 files in it (391GB). I don't know if I should include a delay so that the program can emumerate them or if there is some other problem that I just don't see.
Any ideas? I'm using VBS, the msgBox between the 2 set statements and the for loop works, but the one between the opening of the for loop and the if statement does not.
Are you saying the codes in the For loop doesn't seem to work? It seems not work if the folder does not have any files in it. So check the value of PathToStorageFiles.
Your logic of getting the oldest recording creation time is flawed - any time that is 6 hours before Now is treated as oldest and set to earlyDateTime.
Try this code below, with sample output:
PathToStorageFiles = "C:\Test" ' <=- Change this!
Set StorageFileSystem = CreateObject("Scripting.fileSystemObject")
Set StorageFolder = StorageFileSystem.GetFolder(PathToStorageFiles)
sOldestFile = "" ' Stores the full name of the file
earlyDateTime = dateadd("h" ,-6, Now) ' Assuming 6 hours before script started is oldest (it can be just Now)
wscript.echo StorageFolder.Files.Count & " files in the folder " & PathToStorageFiles
for each Storagefile in StorageFolder.Files 'get the creation time of the oldest recording
if Storagefile.DateCreated < earlyDateTime then
sOldestFile = Storagefile.Path
earlyDateTime = Storagefile.DateCreated
wscript.echo "earlyDateTime changed to " & earlyDateTime & " | " & sOldestFile
end if
next
wscript.echo vbCrLf & "Oldest file: " & sOldestFile & vbCrLf & "Created on: " & earlyDateTime
On a side note, you should modify this to process sub folders too, then move files into folders. 130,000 files in a single folder is a mess!
UPDATE
Based on your posted solution, there are improvements you can do.
First, use 1 FileSystemObject.
Then the recentFile in the for loop. You should set it to zero first, rather than 2 comparisons. Having said that, you have the opportunity to time the differences.
recentFile = 0
For Each file in colFiles
If file.DateCreated > recentFile Then
recentFile = file.DateCreated
End If
Next
Lastly, if the D: on the server is a NAS, then you can split the code into 2 parts - one search for most recent, the other for oldest. Then use batch file start cscript.exe //nologo <script#.vbs> method to start them in 2 processes. This you need 2 txt files for output.
If there is only 1 folder to get the latest & oldest file, it can be in 1 for loop.
This is the code that I got to work:
Option Explicit
Dim LocalStorage, NewLocalStorage, recentFile, objFSO, colFiles, objFolder, file, OldestDate, strOldestDate, fso, ts, objFile
LocalStorage = "D:\BlueIris\Storage"
NewLocalStorage = "D:\BlueIris\New"
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFolder = objFSO.GetFolder(NewLocalStorage)
Set colFiles = objFolder.Files
For Each file in colFiles
If recentFile = "" Then
recentFile = file.DateCreated
ElseIf file.DateCreated > recentFile Then
recentFile = file.DateCreated
End If
Next
Set objFolder = objFSO.GetFolder(LocalStorage)
Set colFiles = objFolder.Files
OldestDate = Now
For Each objFile in colFiles
if objFile.DateCreated < OldestDate Then
OldestDate = objFile.DateCreated
strOldestDate = objFile.DateCreated
End if
Next
Set fso = CreateObject("Scripting.FileSystemObject")
Set ts = fso.CreateTextFile ("C:\DVRInfo.txt", true)
ts.writeline recentFile
ts.writeline strOldestDate
ts.close
I run this on the actual server so that it runs a lot faster than the original code I attempted. Let me know if you still flaws in this please, I want to be as efficient as possible.
Thanks
EDIT:
New code:
Option Explicit
Dim LocalStorage, NewLocalStorage, recentFile, objFSO, colFiles, objFolder, file, OldestDate, strOldestDate, fso, ts, objFile
LocalStorage = "D:\BlueIris\Storage"
NewLocalStorage = "D:\BlueIris\New"
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFolder = objFSO.GetFolder(NewLocalStorage)
Set colFiles = objFolder.Files
Set recentFile = 0
For Each file in colFiles
If file.DateCreated > recentFile Then
recentFile = file.DateCreated
End If
Next
Set objFolder = objFSO.GetFolder(LocalStorage)
Set colFiles = objFolder.Files
OldestDate = Now
For Each objFile in colFiles
if objFile.DateCreated < OldestDate Then
OldestDate = objFile.DateCreated
strOldestDate = objFile.DateCreated
End if
Next
Set ts = fso.CreateTextFile ("C:\DVRInfo.txt", true)
ts.writeline recentFile
ts.writeline strOldestDate
ts.close

Resources