-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.go
77 lines (63 loc) · 1.5 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
package main
import (
"fmt"
"io"
"io/ioutil"
"log"
"net/url"
"os"
"path/filepath"
"strings"
)
//Read file with path images
const fileImages = "/home/peter/currentImg.txt"
const sourceDirImg = "/home/peter/proyectos/monoforms8/web/sites/default/files/"
const destinationPictures = "/home/peter/Imágenes/monoformsUsed/"
func main() {
fmt.Println("Init")
data, err :=ioutil.ReadFile(fileImages)
if err != nil {
fmt.Println("Error reading file", err)
} else {
dataAsLines := strings.Split(string(data), "\n")
count := 0
for _, pathImg := range dataAsLines {
if !strings.HasPrefix(pathImg, "http") {
imgName := filepath.Base(pathImg)
decodeImageName, err := url.QueryUnescape(imgName)
if err != nil{
fmt.Println("Unescape error", err)
}
fullPath := sourceDirImg+decodeImageName
if Exist(fullPath){
sourceFile, err := os.Open(fullPath)
if err != nil {
log.Fatal(err)
}
defer sourceFile.Close()
// Create new file
newPathName := destinationPictures+decodeImageName
newFile, err := os.Create(newPathName)
if err != nil {
log.Fatal(err)
}
defer newFile.Close()
bytesCopied, err := io.Copy(newFile, sourceFile)
if err != nil {
log.Fatal(err)
}
log.Printf("Copied %d bytes.", bytesCopied)
count ++
}
}
}
fmt.Println("Counting objects:", count)
}
}
func Exist(filename string) bool {
info, err := os.Stat(filename)
if os.IsNotExist(err){
return false
}
return !info.IsDir()
}