Skip to content

Instantly share code, notes, and snippets.

@ArseniyShestakov
Created September 2, 2015 19:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ArseniyShestakov/d7f62fc311242b971782 to your computer and use it in GitHub Desktop.
Save ArseniyShestakov/d7f62fc311242b971782 to your computer and use it in GitHub Desktop.
Simple tool to copy N files into different direcoty
package main
import (
"path"
"path/filepath"
"os"
"time"
"strconv"
"math/rand"
)
var Files []string
func shuffle(arr []string) {
t := time.Now()
rand.Seed(int64(t.Nanosecond()))
for i := len(arr) - 1; i > 0; i-- {
j := rand.Intn(i)
arr[i], arr[j] = arr[j], arr[i]
}
}
func visit(path string, f os.FileInfo, err error) error {
fi, _ := os.Stat(path)
if err != nil {
panic(err)
}
if !fi.IsDir() {
Files = append(Files, path)
}
return nil
}
func main() {
root, _ := os.Getwd()
filepath.Walk(root, visit)
resultPath := filepath.Join(root, "..", "r" + strconv.Itoa(int(time.Now().Unix())))
os.Mkdir(resultPath, 0777)
shuffledFiles := Files
shuffle(shuffledFiles)
i := 0
for _,file := range shuffledFiles {
if i == 3 {
return
}
os.Rename(file, filepath.Join(resultPath, path.Base(file)))
i++
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment