Compare commits

...

2 Commits

2 changed files with 97 additions and 52 deletions

View File

@ -1,45 +1,72 @@
# Directory Tree Generator
This Go program generates a directory tree and saves it into a text file named `allFiles.txt`. It also includes the content of each file in the directory tree.
This Go program generates a directory tree and saves it into an XML file named `allFiles.xml`. It also includes the content of each file in the directory tree, each wrapped with its filename as an XML tag.
## How to Run
1. Ensure Go is installed on your machine. If not, you can download it from [here](https://golang.org/dl/).
2. Save the Go script into a file, for example `main.go`.
3. Open a terminal and navigate to the directory where you saved `main.go`.
4. Run the program by typing `go run main.go` and pressing `Enter`.
5. When prompted, enter the path to the directory you want to generate the tree for.
4. Run the program by typing `go run main.go` and pressing `Enter`. Alternatively, you can also specify the directory path as an argument like so: `go run main.go /path/to/directory`.
## Sample Output
The output will be saved in a file named `allFiles.txt` in the same directory where you ran the script. The content of the file will look something like this:
The output will be saved in a file named `allFiles.xml` in the same directory where you ran the script. The content of the file will look something like this:
```
Directory tree:
.
├── dir1
│ ├── file1.txt
│ └── file2.txt
└── dir2
├── dir3
│ └── file3.txt
└── file4.txt
Start of file: ./dir1/file1.txt
Hello, this is file1.
End of file: ./dir1/file1.txt
Start of file: ./dir1/file2.txt
This is another file in dir1.
End of file: ./dir1/file2.txt
Start of file: ./dir2/dir3/file3.txt
Nested file in dir3.
End of file: ./dir2/dir3/file3.txt
Start of file: ./dir2/file4.txt
File in dir2.
End of file: ./dir2/file4.txt
```xml
<?xml version="1.0" encoding="UTF-8"?>
<directoryTree>
<node>
<name>dir1</name>
<isDir>true</isDir>
<children>
<node>
<name>file1.txt</name>
<isDir>false</isDir>
</node>
<node>
<name>file2.txt</name>
<isDir>false</isDir>
</node>
</children>
</node>
<node>
<name>dir2</name>
<isDir>true</isDir>
<children>
<node>
<name>dir3</name>
<isDir>true</isDir>
<children>
<node>
<name>file3.txt</name>
<isDir>false</isDir>
</node>
</children>
</node>
<node>
<name>file4.txt</name>
<isDir>false</isDir>
</node>
</children>
</node>
<file1.txt>
<path>./dir1/file1.txt</path>
<content>Hello, this is file1.</content>
</file1.txt>
<file2.txt>
<path>./dir1/file2.txt</path>
<content>This is another file in dir1.</content>
</file2.txt>
<file3.txt>
<path>./dir2/dir3/file3.txt</path>
<content>Nested file in dir3.</content>
</file3.txt>
<file4.txt>
<path>./dir2/file4.txt</path>
<content>File in dir2.</content>
</file4.txt>
</directoryTree>
```
In this example, the directory structure is displayed first. Then, the contents of each file are shown, with the path to the file displayed before and after the content.
In this example, the directory structure is displayed first. Then, the contents of each file are shown, with the path to the file and its content wrapped in its filename tag.

View File

@ -2,19 +2,26 @@ package main
import (
"bufio"
"encoding/xml"
"fmt"
"os"
"sort"
"path/filepath"
"strings"
"io/ioutil"
"os"
"os/user"
"path/filepath"
"sort"
"strings"
)
type Node struct {
Name string
IsDir bool
Children []*Node
XMLName xml.Name `xml:"node"`
Name string `xml:"name"`
IsDir bool `xml:"isDir"`
Children []*Node `xml:"children"`
}
type FileContent struct {
Path string `xml:"path"`
Content string `xml:",innerxml"`
}
func (n *Node) String(prefix string) string {
@ -83,10 +90,15 @@ func expandPath(path string) (string, error) {
}
func main() {
reader := bufio.NewReader(os.Stdin)
fmt.Print("Enter directory: ")
dir, _ := reader.ReadString('\n')
dir = strings.TrimSuffix(dir, "\n")
var dir string
if len(os.Args) > 1 {
dir = os.Args[1]
} else {
reader := bufio.NewReader(os.Stdin)
fmt.Print("Enter directory: ")
dir, _ = reader.ReadString('\n')
dir = strings.TrimSuffix(dir, "\n")
}
dir, err := expandPath(dir)
if err != nil {
@ -100,40 +112,46 @@ func main() {
return
}
f, err := os.Create("allFiles.txt")
f, err := os.Create("allFiles.xml")
if err != nil {
fmt.Println(err)
f.Close()
return
}
f.WriteString("Directory tree:\n")
f.WriteString(".\n")
f.WriteString(tree.String(""))
f.WriteString(xml.Header)
f.WriteString("<directoryTree>\n")
output, _ := xml.MarshalIndent(tree, "", " ")
f.WriteString(string(output) + "\n")
filepath.Walk(dir, func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}
if !info.IsDir() {
f.WriteString("\nStart of file: " + path + "\n")
bytes, err := ioutil.ReadFile(path)
if err != nil {
fmt.Println(err)
}
f.WriteString(string(bytes) + "\n")
f.WriteString("End of file: " + path + "\n")
fileContent := FileContent{
Path: path,
Content: string(bytes),
}
f.WriteString("<" + info.Name() + ">\n")
output, _ := xml.MarshalIndent(fileContent, "", " ")
f.WriteString(string(output) + "\n")
f.WriteString("</" + info.Name() + ">\n")
}
return nil
})
f.WriteString("</directoryTree>\n")
err = f.Close()
if err != nil {
fmt.Println(err)
return
}
fmt.Println("The directory tree has been saved in 'allFiles.txt'")
fmt.Println("The directory tree has been saved in 'allFiles.xml'")
}