Certainly! Below is the updated Go script wrapped in a main function that can be run using go run main.go. The script will also return an appropriate exit status based on whether the file sequence is valid or not.
package main
import (
"fmt"
"os"
"sort"
"strconv"
"strings"
)
func checkFileSequence(directory string) bool {
// List all files in the directory
files, err := os.ReadDir(directory)
if err != nil {
fmt.Println("Error reading directory:", err)
return false
}
if len(files) == 0 {
fmt.Println("Directory is empty.")
return false
}
// Initialize slices to store numbers and filenames
numbers := make([]int, 0)
filenames := make([]string, 0)
for _, file := range files {
if !file.IsDir() && strings.Contains(file.Name(), ".") {
// Extract the number from the filename (assuming it's at the beginning)
parts := strings.SplitN(file.Name(), ".", 2)
numPart := parts[0]
num, err := strconv.Atoi(numPart)
if err != nil {
fmt.Printf("Invalid file name: %s. File names should start with a number.\n", file.Name())
return false
}
// Store the number and filename
numbers = append(numbers, num)
filenames = append(filenames, file.Name())
}
}
if len(numbers) == 0 {
fmt.Println("No valid files found.")
return false
}
// Sort the numbers to check for gaps
sort.Ints(numbers)
// Initialize variables to track the minimum and maximum numbers found
minNumber := numbers[0]
maxNumber := numbers[len(numbers)-1]
// Check for gaps in the sequence
validSequence := true
for i, num := range numbers {
if num != minNumber+i {
validSequence = false
break
}
}
if validSequence && minNumber == maxNumber-len(numbers)+1 {
fmt.Println("File sequence is continuous and starts with the correct order.")
return true
} else if !validSequence {
fmt.Println("File sequence has gaps or is out of order.")
return false
} else {
fmt.Printf("File sequence starts with the correct order but may have gaps.\n")
return false
}
}
func main() {
directoryPath := "/path/to/your/directory"
if checkFileSequence(directoryPath) {
os.Exit(0)
} else {
os.Exit(1)
}
}- Listing Files: The script lists all files in the specified directory using
os.ReadDir. - Filtering Non-Files and Cleaning: It filters out non-file entries (like directories) and trims any leading/trailing spaces from filenames.
- Initialization: It initializes slices to store numbers and filenames.
- Processing Each File:
- It attempts to extract a number from each filename (assuming it's at the beginning).
- It stores the number and filename in separate slices.
- Validation:
- It sorts the numbers to check for gaps.
- It checks if the sequence of numbers is continuous starting from the correct order.
- Output: Finally, it outputs whether the file sequence in the directory is valid or not and returns an appropriate exit status (
0for success,1for failure).
- Replace
"/path/to/your/directory"with the actual path to the directory you want to check. - Run the script using
go run main.go. - The script will print the result to the console and return an exit status of
0if the file sequence is valid, or1otherwise.