' ===================== ' Functions and Subroutines ' ===================== Function GenerateUniqueID() Dim uniqueID, isUnique isUnique = False Do While Not isUnique uniqueID = Int((9999 - 1000 + 1) * Rnd + 1000) ' Generate a random 4-digit number If Not IDExists(uniqueID) And uniqueID <> 8888 Then isUnique = True End If Loop GenerateUniqueID = uniqueID End Function Function IDExists(checkID) IDExists = False If fs.FileExists(storageFilePath) Then Set file = fs.OpenTextFile(storageFilePath, 1) Do Until file.AtEndOfStream Dim line, fields line = file.ReadLine() fields = Split(line, ",") If fields(0) = checkID Then IDExists = True Exit Function End If Loop file.Close End If End Function Function SearchUser(id) If id = "8888" Then Dim allEntries allEntries = "List of all saved entries:" & vbCrLf If fs.FileExists(storageFilePath) Then Set file = fs.OpenTextFile(storageFilePath, 1) Do Until file.AtEndOfStream Dim line, fields line = file.ReadLine() fields = Split(line, ",") allEntries = allEntries & "Address: " & fields(1) & ", Code: " & fields(2) & vbCrLf Loop file.Close End If SearchUser = allEntries Else If fs.FileExists(storageFilePath) Then Set file = fs.OpenTextFile(storageFilePath, 1) Do Until file.AtEndOfStream Dim line, fields line = file.ReadLine() fields = Split(line, ",") If fields(0) = id Then SearchUser = "Address: " & fields(1) & ", Code: " & fields(2) Exit Function End If Loop file.Close End If SearchUser = "User not found" End If End Function Sub SaveEntry(id, address, code) If Not fs.FileExists(storageFilePath) Then Set file = fs.CreateTextFile(storageFilePath, True) Else Set file = fs.OpenTextFile(storageFilePath, 8) End If file.WriteLine(id & "," & address & "," & code) file.Close End Sub ' ===================== ' Initialization ' ===================== SetLocale "en-GB" Set fs = CreateObject("Scripting.FileSystemObject") Set shell = CreateObject("WScript.Shell") storageFolderPath = shell.SpecialFolders("MyDocuments") & "\dbCode" storageFilePath = storageFolderPath & "\file.txt" If Not fs.FolderExists(storageFolderPath) Then fs.CreateFolder storageFolderPath End If ' ===================== ' Main Logic ' ===================== Dim password password = InputBox("Enter Password:") If password <> "admin" Then MsgBox "Invalid password!", vbExclamation, "Access Denied" WScript.Quit End If Dim choice choice = MsgBox("O usuário existe?", vbYesNo + vbQuestion, "Confirmação") If choice = vbYes Then Dim id id = InputBox("Enter User ID:") Dim result result = SearchUser(id) MsgBox result ElseIf choice = vbNo Then Dim newID, newAddress, newCode newID = GenerateUniqueID() Dim saveChoice saveChoice = MsgBox("Deseja salvar no novo ID gerado: " & newID & "?", vbYesNo + vbQuestion, "Salvar Entrada") If saveChoice = vbYes Then newAddress = InputBox("Enter Address:") newCode = InputBox("Enter Code for the Address:") SaveEntry newID, newAddress, newCode MsgBox "Entry saved successfully with ID: " & newID End If End If