LoginSignup
0
1

Goでファイルからフォルダを自動生成

Posted at

下書きのまま放置してたやつを公開します。
2022年の4月の情報です

初めに

初心者がのたのたしながらフォルダを作る話です。
突っ込みどころ等ご了承ください。

きっかけ

私はとある部活で幹部をしていたのですが、部員全員分のデータ保存フォルダを学校のNASに作る必要があり、手動がめんどくさかったので作りました。

やりたいこと

テキストファイルを読み込んでフォルダを一括生成。

プログラム

先に結論から

main.go
func main() {
	// -1-
	file := input("ファイル名を拡張子も含めて入力してください。(.txt)")
	// ファイル形式チェック
	fcheck := strings.Contains(file, ".txt")
	if fcheck == false {
		fmt.Println("無効なファイル形式です。")
		goto Start
	}
	// -2-
	// フォルダを作る
	for scanner.Scan() {
		fmt.Println(num, ".", scanner.Text())
		num++
		er := os.Mkdir(scanner.Text(), 0777)
		if er != nil {
				err(er)
			ers++
		}
	}
}

func input(msg string) string {
	scan := bufio.NewScanner(os.Stdin)
	fmt.Print(msg + ":")
	scan.Scan()
	return scan.Text()
}
実行結果
ファイル名を拡張子も含めて入力してください。(.txt):名簿.txt
1 . 織田信長
2 . 豊臣秀吉
3 . 徳川家康
4 . フランシスコ・ザビエル
5 . 西郷隆盛
6 . 大久保利通
7 . 木戸孝允

main.goと同じ階層にフォルダができます。
それだけ。

0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1