lundi 2 mars 2020

Trying to insert extracted file names into a single file

This program takes directory name and read all the file inside of the directory after this I am collecting the name of the files and trying to insert the all file names into a new file but I am getting only last index, I want to store all the file names into a single file.

package main

import (
    "flag"
    "fmt"
    "log"
    "os"
    "regexp"
)

func readCurrentDir(folderpath string) {
    file, err := os.Open(folderpath)
    if err != nil {
        log.Println("failed opening directory: ", err)
        os.Exit(1)
    }
    defer file.Close()

    list, _ := file.Readdirnames(0) // 0 to read all files and folders
    for _, name := range list {
        getDomainFromFile(name)
    }
}

func CreateDomainsFile(domains string) {
    filename, err := os.Create("All-Sitemap-Domains.txt")
    if err != nil {
        log.Println("Error to create txt file", err)
    }
    defer filename.Close()
    writestrings, err := filename.WriteString(domains)
    if err != nil {
        log.Println("Error to append data into txt file", err)
    }
    log.Println("Successfully data insetred into txt file")
    fmt.Printf("wrote %d bytes\n", writestrings)
    filename.Sync()
}

func getDomainFromFile(PageContent string) {
    re := regexp.MustCompile(`(.*)-robots.txt`)
    // re := regexp.MustCompile(`Sitemap: (.*)`)
    FileToDomain := re.FindAllStringSubmatch(PageContent, -1)
    for _, Domain := range FileToDomain {
        log.Println(Domain[1])
        // log.Println(Domain[1])
        fmt.Println("*************************************************************")
        CreateDomainsFile(string(Domain[1]))
        return
    }

}
func main() {
    folder := flag.String("f", "", "Provide the path of the folder")
    flag.Parse()
    readCurrentDir(*folder)

}



Aucun commentaire:

Enregistrer un commentaire