I am not sure if I am doing this correctly, but ultimately I would like to find the most recent modified date of a file in a directory and return the file name. The code I have so far is as follows. Can someone please help me with a more efficient solution than this. I really have a feeling this is super hacky. What I am doing is getting the dates and removing the
package main
import (
"fmt"
"io/ioutil"
"os"
"strconv"
"strings"
)
func main() {
dir := "C:\\temp\\"
files, _ := ioutil.ReadDir(dir)
for _, f := range files {
fi, _ := os.Stat(dir + f.Name())
s := strings.Split(fi.ModTime().Format("2006-01-02 15.04.05.000"), " ")
fdate, err := strconv.Atoi(strings.Replace(s[0], "-", "", -1))
if err != nil {
fmt.Println(err)
}
ftime, err := strconv.Atoi(strings.Replace(s[1], ".", "", -1))
if err != nil {
fmt.Println(err)
}
fmt.Println(fi.Name(), fdate+ftime)
}
}
Pay attention to details and efficiency. Check for errors. You asked for files so skip directories and other things. Allow for multiple files with the same modified time stamp (for example, Windows file times have a resolution of, at best, 100-nanoseconds). You already have ModTime() so don't call os.Stat(). Use time.Time methods directly. And so on.
For example,
package main
import (
"fmt"
"io/ioutil"
"os"
"time"
)
func main() {
dir := `C:\temp\` // Windows directory
files, err := ioutil.ReadDir(dir)
if err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}
var modTime time.Time
var names []string
for _, fi := range files {
if fi.Mode().IsRegular() {
if !fi.ModTime().Before(modTime) {
if fi.ModTime().After(modTime) {
modTime = fi.ModTime()
names = names[:0]
}
names = append(names, fi.Name())
}
}
}
if len(names) > 0 {
fmt.Println(modTime, names)
}
}
You can just compare the outputs of fi.ModTime().Unix() and keep the largest value to find the most recently modified file.
For example:
package main
import (
"fmt"
"io/ioutil"
"os"
)
func main() {
dir := "/tmp/"
files, _ := ioutil.ReadDir(dir)
var newestFile string
var newestTime int64 = 0
for _, f := range files {
fi, err := os.Stat(dir + f.Name())
if err != nil {
fmt.Println(err)
}
currTime := fi.ModTime().Unix()
if currTime > newestTime {
newestTime = currTime
newestFile = f.Name()
}
}
fmt.Println(newestFile)
}
Related
There is a program which creates file per second. I want to append file names into slice and print them. Now my program executes incorrect, it appends names but only for one file name. So I expect to get []string{"1","2","3"}, instead I get []string{"1","1","1"}, []string{"2","2","2"}, []string{"3","3","3"}. How to correct my prog to get expected result?
package main
import (
"encoding/csv"
"fmt"
"os"
"strconv"
"time"
)
func main() {
for {
time.Sleep(1 * time.Second)
createFile()
}
}
func createFile() {
rowFile := time.Now().Second()
fileName := strconv.Itoa(rowFile)
file, err := os.OpenFile(fileName, os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0644)
if err != nil {
fmt.Println(err)
}
defer file.Close()
writer := csv.NewWriter(file)
writer.Comma = '|'
err = writer.Write([]string{""})
if err != nil {
fmt.Println(err)
}
countFiles(fileName)
}
func countFiles(fileName string) {
arrFiles := make([]string, 0, 3)
for i := 0; i < 3; i++ {
arrFiles = append(arrFiles, fileName)
}
fmt.Println(arrFiles)// here I expect ["1","2","3"] then ["4","5","6"] and so on. But now there is ["1","1","1"] then ["2","2","2"] and so on
}
createFile() does not persist created file names in any way. You can do something like that:
package main
import (
"encoding/csv"
"fmt"
"os"
"strconv"
"time"
)
func main() {
files := []string{}
for {
time.Sleep(1 * time.Second)
files = append(files, createFile())
fmt.Println(files)
}
}
func createFile() string {
rowFile := time.Now().Second()
fileName := strconv.Itoa(rowFile)
file, err := os.OpenFile(fileName, os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0644)
if err != nil {
fmt.Println(err)
}
defer file.Close()
writer := csv.NewWriter(file)
writer.Comma = '|'
err = writer.Write([]string{""})
if err != nil {
fmt.Println(err)
}
return fileName
}
I have a folder named "myfolder" that has some txt and jpeg files and and a folder named "test".
I want to copy files inside myfolder to test until it reaches a certain size, for example 10MB, then stop to copy.
This is my code that does not work:
package main
import (
"errors"
"fmt"
"io/ioutil"
"os"
"path/filepath"
"strconv"
)
var (
MyFolderPath = "/home/jim/myfolder"
files []string
)
func copy(source []string, destination string) {
for a, b := range source {
input, _ := ioutil.ReadFile(b)
ioutil.WriteFile(destination+strconv.Itoa(a), input, 0777)
}
}
func CopyFile(path string) {
var size int64
done := errors.New("size reached")
err := filepath.Walk(path, func(_ string, info os.FileInfo, err error) error {
if err != nil {
return err
}
copy(files[1:], fmt.Sprintf("%v/test/%v", MyFolderPath, "file"))
size += info.Size()
if size > 10000 {
return done
}
return err
})
if err != nil {
fmt.Println(err)
}
}
func CollectFiles() {
err := filepath.Walk(MyFolderPath, func(path string, info os.FileInfo, err error) error {
if err != nil {
fmt.Println(err)
}
files = append(files, path)
return nil
})
if err != nil {
fmt.Println(err)
}
}
func main() {
CollectFiles()
CopyFile(fmt.Sprintf("%v/test", MyFolderPath))
}
What I do wrong and how can I do in right way?
This line is wrong:
copy(files[1:], fmt.Sprintf("%v/test/%v", MyFolderPath, "file"))
Every time you find a file match, you tell it to (re)-copy every file in your files slice, except the first one.
You probably want to copy a single file there.
Although your code is difficult to reason through--I'm not sure why files exists at all, nor why you're walking your directory structure twice. So your goals aren't really clear, so it's hard to be more specific.
Also note: You should not use copy as the name of a function, as that is a built-in, so makes your code very confusing to read.
You have multiple places where you're wrong. Perhaps check this code and compare:
package main
import (
"errors"
"fmt"
"os"
"path/filepath"
)
var (
MyFolderPath = "/tmp"
DestPath = "/tmp/dest"
CumulativeSize int64 = 0 // in bytes
ErrDone = errors.New("done")
)
const UpperLimit = 1024 * // 1 Kilobyte
1024 * // 1 Megabyte
10 // 10 Megabytes
func main() {
err := filepath.Walk(MyFolderPath, func(path string, info os.FileInfo, err error) error {
if err != nil {
fmt.Printf("problem with file (%s): %v", path, err)
return nil
}
if info.IsDir() {
return filepath.SkipDir // skip it, walk will enter it too
}
CumulativeSize += info.Size()
if CumulativeSize >= UpperLimit {
return ErrDone
}
// copy file here
return nil
})
if err == ErrDone {
fmt.Println("finished successfully")
} else {
fmt.Printf("error: %v", err)
}
}
In my Go project, I need to sort the .json files and to display their name on the terminal when I'm running this command on the terminal go run main.go.
I coded a program which displays all the files in the folder, but I need to sort the .json file.
My code is the following :
package main
import (
"fmt"
"log"
"os"
"bytes"
"io"
)
func main() {
if os.Args[1] == "display-json-name" {
//reads the directory name and returns a list of directory entries
dirname := "."
f, err := os.Open(dirname)
if err != nil {
log.Fatal(err)
}
files, err := f.Readdir(-1)
f.Close()
if err != nil {
log.Fatal(err)
}
for _, file := range files {
fmt.Println(file.Name())
}
}
How can we sort just the different .json files?
And the hierarchy of my project is :
Based on comments, it appears that the question is "How to print files where the file has a .json extension". Here's the code:
if os.Args[1] == "display-json-name" {
//reads the directory name and returns a list of directory entries
dirname := "."
f, err := os.Open(dirname)
if err != nil {
log.Fatal(err)
}
files, err := f.Readdir(-1)
f.Close()
if err != nil {
log.Fatal(err)
}
for _, file := range files {
if filepath.Ext(file.Name()) == ".json" {
fmt.Println(file.Name())
}
}
}
https://golang.org/pkg/io/ioutil/#ReadDir
package main
import (
"fmt"
"io/ioutil"
"log"
)
func main() {
files, err := ioutil.ReadDir(".")
if err != nil {
log.Fatal(err)
}
for _, file := range files {
fmt.Println(file.Name())
}
}
i have this simple server writed in golang :
package main
import (
"net/http"
)
func main() {
http.Handle("/", http.FileServer(http.Dir("./static")))
http.ListenAndServe(":3000", nil)
}
I want add new function :
every request GET /rotate return one file content in rotation from /static folder.
for example in /static folder exist 7 file, for every request server return : file1, file2, file3 ...
How i can do this in go ?
Below is a simple server that should get you started toward your goal. A couple of things to note about the implementation:
The file names are loaded only once during program startup. If a file disappears while the program is running, the server will return a 404 when it comes up in the rotation.
We need to use locking (in this case, via sync.Mutex) as each request will run in its own go routine.
package main
import (
"flag"
"net/http"
"os"
"path/filepath"
"sync"
)
func main() {
dir := flag.String("dir", ".", "directory of files to serve")
flag.Parse()
f, err := os.Open(*dir)
if err != nil {
panic(err)
}
files, err := f.Readdir(0)
if err != nil {
panic(err)
}
filenames := make([]string, 0, len(files))
for _, file := range files {
if !file.IsDir() {
filenames = append(filenames, file.Name())
}
}
var (
idxLock sync.Mutex
idx int
)
http.HandleFunc("/rotate", func(w http.ResponseWriter, r *http.Request) {
if len(filenames) == 0 {
http.NotFound(w, r)
return
}
idxLock.Lock()
i := idx
idx++
if idx >= len(filenames) {
idx = 0
}
idxLock.Unlock()
http.ServeFile(w, r, filepath.Join(*dir, filenames[i]))
})
if err := http.ListenAndServe(":3000", nil); err != nil {
panic(err)
}
}
I'm able to get a list of files and folders from a directory, I've written a function called isDir to return True if the path is a directory.
Now my problem is that I want to make sure that none of the folders listed match a list of strings in a slice. The code I have might skip the first match but it will print out everything else anyways. I need to process the folders that aren't to be avoided.
Code is for Windows 7/8 directories but I should be able to get a Linux sample working too should one be provided.
package main
import (
"fmt"
"io/ioutil"
"os"
"strings"
)
func isDir(pth string) (bool) {
fi, err := os.Stat(pth)
if err != nil {
return false
}
return fi.Mode().IsDir()
}
func main() {
gcomputer := "localhost"
location := fmt.Sprintf("\\\\%s\\c$\\Users\\", gcomputer)
// Profiles to avoid
avoid := []string{"Administrator", "Default", "Public"}
// walk through files & folders in the directory (location) & return valid profiles
files, _ := ioutil.ReadDir(location)
for _, f := range files {
fdir := []string{location, f.Name()}
dpath := strings.Join(fdir, "")
if isDir(dpath) {
for _, iavoid := range avoid {
for iavoid != f.Name() {
fmt.Println(dpath)
break
}
break
}
}
}
}
I don't mind using a third-party module, I've been working on this too long and starting to lose my cool, making understanding the docs a bit difficult. Any tips would be appreciated. Thank you.
If your list of strings to avoid gets bigger, you might not want to be iterating over them for every directory. Might I suggest a small modification to #peterSO's answer:
package main
import (
"fmt"
"io/ioutil"
)
var avoidanceSet = map[string]bool{
"Administrator": true,
"Default": true,
"Public": true,
}
func avoid(name string) bool {
_, inSet := avoidanceSet[name]
return inSet
}
func main() {
gcomputer := "localhost"
location := fmt.Sprintf("\\\\%s\\c$\\Users\\", gcomputer)
files, err := ioutil.ReadDir(location)
if err != nil {
fmt.Println(err)
return
}
for _, f := range files {
if f.IsDir() && !avoid(f.Name()) {
dpath := location + f.Name()
fmt.Println(dpath)
}
}
}
For example,
package main
import (
"fmt"
"io/ioutil"
)
func avoid(name string) bool {
profiles := []string{"Administrator", "Default", "Public"}
for _, p := range profiles {
if name == p {
return true
}
}
return false
}
func main() {
gcomputer := "localhost"
location := fmt.Sprintf("\\\\%s\\c$\\Users\\", gcomputer)
files, err := ioutil.ReadDir(location)
if err != nil {
fmt.Println(err)
return
}
for _, f := range files {
if f.IsDir() && !avoid(f.Name()) {
dpath := location + f.Name()
fmt.Println(dpath)
}
}
}