Skip to content

Instantly share code, notes, and snippets.

@MasterGroosha
Created July 25, 2019 16:55
Show Gist options
  • Save MasterGroosha/2f3d47cd995e681038229b459f4bfb4a to your computer and use it in GitHub Desktop.
Save MasterGroosha/2f3d47cd995e681038229b459f4bfb4a to your computer and use it in GitHub Desktop.
#include <experimental/filesystem>
#include <iostream>
#include <string>
#include <sstream>
#include <iomanip>
using namespace std;
namespace fs = std::experimental::filesystem;
int main(int argc, char** argv)
{
if (argc < 2)
{
cout << "Please enter path!\n";
exit(EXIT_FAILURE);
}
const string dirPath = argv[1];
auto dirIterator = fs::directory_iterator(dirPath);
for (auto &file : dirIterator)
{
auto old_name = file.path().filename().string();
stringstream ss;
ss << std::setfill('0') << std::setw(12 + 5) << old_name;
auto new_name = ss.str();
fs::rename(dirPath + old_name, dirPath + new_name);
cout << "Renamed " << old_name << " to " << new_name << endl;
}
return 0;
}
@stek29
Copy link

stek29 commented Jul 26, 2019

package main

import (
	"os"
	"path"
	"fmt"
)

func readFilesInDir(dir string, n int) <-chan string {
	f, _ := os.Open(dir)
	fnames := make(chan string)
	go func() {
		for {
			infos, err := f.Readdir(n)
			if err != nil {
				break
			}
			for _, inf := range infos {
				fnames <- inf.Name()
			}
		}
		close(fnames)
		f.Close()
	}()
	return fnames
}

func main() {
	if len(os.Args) < 2 {
		fmt.Printf("usage: %v dir\n", os.Args[0])
		os.Exit(1)
	}

	dir := os.Args[1]
	for oldName := range readFilesInDir(dir, 10) {
		newName := fmt.Sprintf("%015s", oldName)
		if oldName == newName {
			fmt.Printf("Skip %v\n", oldName)
			continue
		}
		os.Rename(path.Join(dir, oldName), path.Join(dir, newName))
		fmt.Printf("Renamed %v to %v\n", oldName, newName)
	}
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment