-
-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(cmd/scripts/rename/spaces_and_commas): Add command which renames…
… Google Docs exports to better filenames suitable for URLs
- Loading branch information
1 parent
fac482f
commit 9912a96
Showing
3 changed files
with
77 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
package rename | ||
|
||
import ( | ||
parent_cmd "github.com/sikalabs/slu/cmd/scripts" | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
var Cmd = &cobra.Command{ | ||
Use: "rename", | ||
Short: "Rename files", | ||
} | ||
|
||
func init() { | ||
parent_cmd.Cmd.AddCommand(Cmd) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
package spaces_and_commas | ||
|
||
import ( | ||
"fmt" | ||
"io/ioutil" | ||
"os" | ||
"path/filepath" | ||
"strings" | ||
|
||
parent_cmd "github.com/sikalabs/slu/cmd/scripts/rename" | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
var FlagDry bool | ||
var FlagVersion string | ||
|
||
var Cmd = &cobra.Command{ | ||
Use: "spaces-and-commas", | ||
Short: "Replace spaces and commans in filenames with underscores", | ||
Aliases: []string{"sac"}, | ||
Args: cobra.NoArgs, | ||
Run: func(c *cobra.Command, args []string) { | ||
replaceSpacesAndCommas() | ||
}, | ||
} | ||
|
||
func init() { | ||
parent_cmd.Cmd.AddCommand(Cmd) | ||
} | ||
|
||
func replaceSpacesAndCommas() { | ||
dir := "." | ||
|
||
files, err := ioutil.ReadDir(dir) | ||
if err != nil { | ||
fmt.Println("Error reading directory:", err) | ||
return | ||
} | ||
|
||
for _, file := range files { | ||
if !file.IsDir() { | ||
oldName := file.Name() | ||
newName := strings.ReplaceAll(oldName, " - ", "__") | ||
newName = strings.ReplaceAll(newName, ",", "_") | ||
newName = strings.ReplaceAll(newName, " ", "_") | ||
|
||
if oldName != newName { | ||
oldPath := filepath.Join(dir, oldName) | ||
newPath := filepath.Join(dir, newName) | ||
|
||
err = os.Rename(oldPath, newPath) | ||
if err != nil { | ||
fmt.Printf("Error renaming file '%s' to '%s': %s\n", oldName, newName, err) | ||
} else { | ||
fmt.Printf("Renamed '%s' to '%s'\n", oldName, newName) | ||
} | ||
} | ||
} | ||
} | ||
} |