move: empty files finder to root

This commit is contained in:
Inanc Gumus
2019-03-06 16:50:38 +03:00
parent 94dd511c63
commit a9e327c36b
36 changed files with 0 additions and 0 deletions

View File

@ -0,0 +1 @@
learngoprogramming.com

View File

@ -0,0 +1 @@
learngoprogramming.com

View File

@ -0,0 +1 @@
learngoprogramming.com

View File

@ -0,0 +1,35 @@
// For more tutorials: https://blog.learngoprogramming.com
//
// Copyright © 2018 Inanc Gumus
// Learn Go Programming Course
// License: https://creativecommons.org/licenses/by-nc-sa/4.0/
//
package main
import (
"fmt"
"io/ioutil"
"os"
)
func main() {
args := os.Args[1:]
if len(args) == 0 {
fmt.Println("Provide a directory")
return
}
files, err := ioutil.ReadDir(args[0])
if err != nil {
fmt.Println(err)
return
}
for _, file := range files {
if file.Size() == 0 {
name := file.Name()
fmt.Println(name)
}
}
}

View File

@ -0,0 +1 @@
learngoprogramming.com

View File

@ -0,0 +1 @@
learngoprogramming.com

View File

@ -0,0 +1 @@
learngoprogramming.com

View File

@ -0,0 +1,50 @@
// For more tutorials: https://blog.learngoprogramming.com
//
// Copyright © 2018 Inanc Gumus
// Learn Go Programming Course
// License: https://creativecommons.org/licenses/by-nc-sa/4.0/
//
package main
import (
"fmt"
"io/ioutil"
"os"
)
func main() {
args := os.Args[1:]
if len(args) == 0 {
fmt.Println("Provide a directory")
return
}
files, err := ioutil.ReadDir(args[0])
if err != nil {
fmt.Println(err)
return
}
var names []byte
for _, file := range files {
if file.Size() == 0 {
name := file.Name()
names = append(names, name...)
names = append(names, '\n')
}
}
err = ioutil.WriteFile("out.txt", names, 0644)
if err != nil {
fmt.Println(err)
return
}
fmt.Printf("%s", names)
}
// See: https://www.tutorialspoint.com/unix/unix-file-permission.htm
// See: http://permissions-calculator.org/

View File

@ -0,0 +1 @@
learngoprogramming.com

View File

@ -0,0 +1 @@
learngoprogramming.com

View File

@ -0,0 +1 @@
learngoprogramming.com

View File

@ -0,0 +1,70 @@
// For more tutorials: https://blog.learngoprogramming.com
//
// Copyright © 2018 Inanc Gumus
// Learn Go Programming Course
// License: https://creativecommons.org/licenses/by-nc-sa/4.0/
//
package main
import (
"fmt"
"io/ioutil"
"os"
)
// You can easily write to a file using bash.
//
// However, when what you're creating is a library,
// then you won't have that option. So, it's good to learn
// how to write to a file using a byte slice.
// ioutil.ReadDir
// os.FileInfo
func main() {
args := os.Args[1:]
if len(args) == 0 {
fmt.Println("Provide a directory")
return
}
files, err := ioutil.ReadDir(args[0])
if err != nil {
fmt.Println(err)
return
}
// 1st: find the total size of all the empty files
var total int
for _, file := range files {
if file.Size() == 0 {
// +1 for the newline character
// when printing the filename
total += len(file.Name()) + 1
}
}
// 2nd: allocate a large enough byte slice in one go
names := make([]byte, 0, total)
for _, file := range files {
if file.Size() == 0 {
name := file.Name()
names = append(names, name...)
names = append(names, '\n')
}
}
err = ioutil.WriteFile("out.txt", names, 0644)
if err != nil {
fmt.Println(err)
return
}
fmt.Printf("%s", names)
}
// See: https://www.tutorialspoint.com/unix/unix-file-permission.htm
// See: http://permissions-calculator.org/

View File

@ -0,0 +1,42 @@
package main
// ---------------------------------------------------------
// EXERCISE: Sort to a file
//
// 1. Get arguments from command-line
//
// 2. Sort them
//
// 3. Write the sorted slice to a file
//
//
// EXPECTED OUTPUT
//
// go run main.go
// Send me some items and I will sort them
//
// go run main.go orange banana apple
//
// cat sorted.txt
// apple
// banana
// orange
//
//
// HINTS
//
// ONLY READ THIS IF YOU GET STUCK
//
// Below, []string means string slice, []byte means byte slice.
//
// + You can use the os.Args[1:] to get a []string
// + Then you can sort it using sort.Strings
// + Use ioutil.WriteFile to write to a file.
// + But you need to convert []string to []byte to be able to
// write it to a file using the ioutil.WriteFile.
// + To do that, create a new []byte and append the elements of your
// []string.
// ---------------------------------------------------------
func main() {
}

View File

@ -0,0 +1,37 @@
// For more tutorials: https://blog.learngoprogramming.com
//
// Copyright © 2018 Inanc Gumus
// Learn Go Programming Course
// License: https://creativecommons.org/licenses/by-nc-sa/4.0/
//
package main
import (
"fmt"
"io/ioutil"
"os"
"sort"
)
func main() {
items := os.Args[1:]
if len(items) == 0 {
fmt.Println("Send me some items and I will sort them")
return
}
sort.Strings(items)
var data []byte
for _, s := range items {
data = append(data, s...)
data = append(data, '\n')
}
err := ioutil.WriteFile("sorted.txt", data, 0644)
if err != nil {
fmt.Println(err)
return
}
}

View File

@ -0,0 +1,70 @@
package main
import (
"fmt"
"io/ioutil"
"os"
"sort"
)
// ---------------------------------------------------------
// EXERCISE: Sort to a file with ordinals
//
// Use the previous exercise: Append #4
//
// This time, print the sorted items with ordinals
// (see the expected output)
//
//
// EXPECTED OUTPUT
//
// go run main.go
// Send me some items and I will sort them
//
// go run main.go orange banana apple
//
// cat sorted.txt
// 1. apple
// 2. banana
// 3. orange
//
//
// HINTS
//
// ONLY READ THIS IF YOU GET STUCK
//
// + You can use strconv.AppendInt function to append an int
// to a byte slice. strconv contains a lot of functions for appending
// other basic types as well.
//
// + You can append individual characters to a byte slice using
// rune literals:
//
// var slice []byte
// slice = append(slice, 'h', 'i', ' ', '!')
// fmt.Printf("%s\n", slice)
//
// Above code prints: hi !
// ---------------------------------------------------------
func main() {
items := os.Args[1:]
if len(items) == 0 {
fmt.Println("Send me some items and I will sort them")
return
}
sort.Strings(items)
var data []byte
for _, s := range items {
data = append(data, s...)
data = append(data, '\n')
}
err := ioutil.WriteFile("sorted.txt", data, 0644)
if err != nil {
fmt.Println(err)
return
}
}

View File

@ -0,0 +1,40 @@
// For more tutorials: https://blog.learngoprogramming.com
//
// Copyright © 2018 Inanc Gumus
// Learn Go Programming Course
// License: https://creativecommons.org/licenses/by-nc-sa/4.0/
//
package main
import (
"fmt"
"io/ioutil"
"os"
"sort"
"strconv"
)
func main() {
items := os.Args[1:]
if len(items) == 0 {
fmt.Println("Send me some items and I will sort them")
return
}
sort.Strings(items)
var data []byte
for i, s := range items {
data = strconv.AppendInt(data, int64(i+1), 10)
data = append(data, '.', ' ')
data = append(data, s...)
data = append(data, '\n')
}
err := ioutil.WriteFile("sorted.txt", data, 0644)
if err != nil {
fmt.Println(err)
return
}
}

View File

@ -0,0 +1,56 @@
package main
// ---------------------------------------------------------
// EXERCISE: Print the directories
//
// Create a program that can get multiple directory paths from
// the command-line, and prints only their subdirectories into a
// file named: dirs.txt
//
//
// 1. Get the directory paths from command-line
//
// 2. Append the names of subdirectories inside each directory
// to a byte slice
//
// 3. Write that byte slice to dirs.txt file
//
//
// EXPECTED OUTPUT
//
// go run main.go
// Please provide directory paths
//
// go run main.go dir/ dir2/
//
// cat dirs.txt
//
// dir/
// subdir1/
// subdir2/
//
// dir2/
// subdir1/
// subdir2/
// subdir3/
//
//
// HINTS
//
// ONLY READ THIS IF YOU GET STUCK
//
// + Get all the files in a directory using ioutil.ReadDir
// (A directory is also a file)
//
// + You can use IsDir method of a FileInfo value to detect
// whether a file is a directory or not: go doc os.FileInfo.IsDir
//
// + You can use '\t' escape sequence for indenting the subdirs.
//
// + You can find a sample directory structure under:
// solution/ directory
//
// ---------------------------------------------------------
func main() {
}

View File

@ -0,0 +1,4 @@
*
!subdir1
!subdir2
!.gitignore

View File

@ -0,0 +1,2 @@
*
!.gitignore

View File

@ -0,0 +1,2 @@
*
!.gitignore

View File

@ -0,0 +1,5 @@
*
!subdir1
!subdir2
!subdir3
!.gitignore

View File

@ -0,0 +1,2 @@
*
!.gitignore

View File

@ -0,0 +1,2 @@
*
!.gitignore

View File

@ -0,0 +1,2 @@
*
!.gitignore

View File

@ -0,0 +1,9 @@
dir/
subdir1/
subdir2/
dir2/
subdir1/
subdir2/
subdir3/

View File

@ -0,0 +1,51 @@
// For more tutorials: https://blog.learngoprogramming.com
//
// Copyright © 2018 Inanc Gumus
// Learn Go Programming Course
// License: https://creativecommons.org/licenses/by-nc-sa/4.0/
//
package main
import (
"fmt"
"io/ioutil"
"os"
)
func main() {
paths := os.Args[1:]
if len(paths) == 0 {
fmt.Println("Please provide directory paths")
return
}
var dirs []byte
for _, dir := range paths {
files, err := ioutil.ReadDir(dir)
if err != nil {
fmt.Println(err)
return
}
dirs = append(dirs, dir...)
dirs = append(dirs, '\n')
for _, file := range files {
if file.IsDir() {
dirs = append(dirs, '\t')
dirs = append(dirs, file.Name()...)
dirs = append(dirs, '/', '\n')
}
}
dirs = append(dirs, '\n')
}
err := ioutil.WriteFile("dirs.txt", dirs, 0644)
if err != nil {
fmt.Println(err)
return
}
}

View File

@ -0,0 +1,7 @@
# File Append Exercises
1. **[Sort and write items to a file](https://github.com/inancgumus/learngo/tree/master/15-slices/19-empty-file-finder-project/exercises/1-sort-to-a-file)**
2. **[Sort and write items to a file with their ordinals](https://github.com/inancgumus/learngo/tree/master/15-slices/19-empty-file-finder-project/exercises/2-sort-to-a-file)**
3. **[Find and write the names of subdirectories to a file](https://github.com/inancgumus/learngo/tree/master/15-slices/19-empty-file-finder-project/exercises/3-print-directories)**