Help with Cmd.Exec

I want to read the text of multiple PDF files, I could not find proper GO lib, so I’m using PDF2Text tool, and wrote the below code:

package main

import (
	"fmt"
	"log"
	"os"
	"os/exec"
	"path/filepath"
)

func main() {
	var files []string

	root := "."
	err := filepath.Walk(root, func(path string, info os.FileInfo, err error) error {
		if info.IsDir() {
			return nil
		}
		if filepath.Ext(path) != ".pdf" {
			return nil
		}
		files = append(files, info.Name())
		return nil
	})
	if err != nil {
		panic(err)
	}
	for _, file := range files {
		fmt.Println(file)
		cmd := exec.Command("pdf2text", "-o", "files", file)
		err := cmd.Run()
		if err != nil {
			log.Fatalf("cmd.Run() failed with %s\n", err)
		}
	}
}

This is working file, and extracting all pdf files into folder “files”, but as demo version of this tool is extraction the pdf file into multiple text file (file per page), I want to have the folder to which the pfd file is extracted to be same as the file name itself, so I tried replacing the:

cmd := exec.Command("pdf2text", "-o", "files", file)

By

cmd := exec.Command("pdf2text", "-o", file, file)

But it did not work, nothing had been executed, no error had been thrown.

Any help!

I found that, the issue is because both output folder having the ext .pdf so the pdf2txt understand that I’m converting 2 pdf with the same name.

To fix it, I removed the ext .pdf from the first string which is to be used for the output directory name using strings.Split so my code became:

	for _, file := range files {
		fmt.Println(file)
		s := strings.Split(file, ".")
		cmd := exec.Command("pdf2text", "-o", s[0], file)
		err := cmd.Run()
		if err != nil {
			log.Fatalf("cmd.Run() failed with %s\n", err)
		}
	}

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.