Created
September 2, 2015 19:25
-
-
Save ArseniyShestakov/d7f62fc311242b971782 to your computer and use it in GitHub Desktop.
Simple tool to copy N files into different direcoty
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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