Skip to content

Commit

Permalink
Merge pull request #9 from oliver-hohn/handle-clashing-names
Browse files Browse the repository at this point in the history
Prevent file name clashes when copying
  • Loading branch information
oliver-hohn authored Dec 25, 2020
2 parents 48e7166 + 5ebbd1f commit d4c8935
Showing 1 changed file with 23 additions and 3 deletions.
26 changes: 23 additions & 3 deletions organizemyfiles/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,21 @@ func (f *File) GetQuarter() string {
}
}

func (f *File) GetName() string {
nameWithExt := filepath.Base(f.Path)
ext := f.GetExt()

// For example:
// nameWithExt = "foo.bar"
// ext == ".bar"
// name == "foo"
return nameWithExt[0 : len(nameWithExt)-len(ext)]
}

func (f *File) GetExt() string {
return filepath.Ext(f.Path)
}

func main() {
flag.Parse()

Expand All @@ -78,6 +93,8 @@ func main() {
log.Fatalf("\"in\" and \"out\" parameters have the same value: %s", *inDir)
}

fileIndex := uint64(0)

err := filepath.Walk(*inDir, func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
Expand All @@ -88,10 +105,11 @@ func main() {
return err
}

err = copy(f)
err = copy(f, fileIndex)
if err != nil {
return err
}
fileIndex++
}

return nil
Expand All @@ -101,7 +119,7 @@ func main() {
}
}

func copy(f *File) error {
func copy(f *File, fileIndex uint64) error {
source, err := os.Open(f.Path)
if err != nil {
return fmt.Errorf("unable to open input file: %s, due to: %w", f.Path, err)
Expand All @@ -115,7 +133,9 @@ func copy(f *File) error {
return fmt.Errorf("unable to create output directory: %s, due to: %w", outPrefix, err)
}

out := filepath.Join(outPrefix, filepath.Base(f.Path))
// Suffix filename with an index to avoid clashes for similarly named files
outFilename := fmt.Sprintf("%s_%d%s", f.GetName(), fileIndex, f.GetExt())
out := filepath.Join(outPrefix, outFilename)
dest, err := os.Create(out)
if err != nil {
return fmt.Errorf("unable to create output file: %s, due to: %w", out, err)
Expand Down

0 comments on commit d4c8935

Please sign in to comment.