ThinkSpace
ThinkSpace

Reputation: 550

Modifying/Combining Text Files with Scripts

I am running a program on my machine that is triggered by running a .bat file. Right now, I am manually modifying the .bat to point to specific files/folders before running the script, which only takes a few minutes since I am running this on 3 to 4 files at a time. In the very near future, I am going to need to run this script on groups of files ranging from 200 to 500. Manually trying to edit the .bat file each time would be a nightmare.

The finished .bat would look like:

cd\[rootfolder]
mkdir Output
cd\folderpath\to\program
this.is.the.program.exe -i "[rootfolder]\[filename1].pdf" -r "[rootfolder]" -o "[rootfolder]\Output" -u username -p password
this.is.the.program.exe -i "[rootfolder]\[filename2].pdf" -r "[rootfolder]" -o "[rootfolder]\Output" -u username -p password
this.is.the.program.exe -i "[rootfolder]\[filename3].pdf" -r "[rootfolder]" -o "[rootfolder]\Output" -u username -p password
this.is.the.program.exe -i "[rootfolder]\[filename4].pdf" -r "[rootfolder]" -o "[rootfolder]\Output" -u username -p password

What I would like to do is create another script, .ps or .bat, that will take a list of filenames from a .txt file (dir /b output) and add the information from above in the correct place.

cd\[rootfolder]
mkdir Output
cd\folderpath\to\program

would only occur in the head of the script; This can be done in another way, but if it is included, that is fine...

this.is.the.program.exe -i "[rootfolder]\

would be added before each filename in the .txt file; I can manage this so far with a search/replace operation...

.pdf" -r "[rootfolder]" -o "[rootfolder]\Output" -u username -p password

would be added behind the filename. This is where I am having the issue!

The only similarity to the files will be the beginning of the filename; such as "Text_", which is why I can do a search/replace operation. The ending of the files are completely random and could be alpha, numeric or symbols, and could be of any character length.

I guess my question would be:

Is there a way to insert text into a .txt file by line position or something similar? Behind the last character on each line?

Upvotes: 1

Views: 118

Answers (3)

Magoo
Magoo

Reputation: 79982

cd\[rootfolder]
mkdir Output
cd\folderpath\to\program
for /f "delims=" %%a in (
 ' the dir /b command that generates the .txt file '
 ) do (
this.is.the.program.exe -i "[rootfolder]\%%a.pdf" -r "[rootfolder]" -o "[rootfolder]\Output" -u username -p password
)

should execute this requirement.

Upvotes: 0

Ansgar Wiechers
Ansgar Wiechers

Reputation: 200203

In you'd probably do something like this:

$rootfolder    = 'C:\path\to\rootfolder'
$outputfolder  = Join-Path $rootfolder 'Output'
$programfolder = 'C:\program\folder'
$filelist      = 'C:\path\to\files.txt'

$user = 'username'
$pass = 'password'

if ( -not (Test-Path -LiteralPath $outputfolder) ) {
  New-Item -ItemType directory -Path $outputfolder
}

Set-Location $programfolder

Get-Content $filelist | % {
  .\this.is.the.program.exe -i (Join-Path $rootfolder $_) -r $rootfolder `
    -o $outputfolder -u $user -p $pass
}

Upvotes: 0

Endoro
Endoro

Reputation: 37569

try this:

cd /d "rootfolder"
md output
for %%a in (*.pdf) do "folderpath\to\program\this.is.the.program.exe" -i "%%~a" -r "rootfolder" -o "rootfolder\Output" -u username -p password

or this:

cd /d "rootfolder"
md output
cd /d "folderpath\to\program"
for %%a in ("rootfolder\*.pdf") do "this.is.the.program.exe" -i "%%~a" -r "%%~dpa" -o "%%~dpaOutput" -u username -p password

Upvotes: 1

Related Questions