How to write STDOUT/STDIN via RedMon/cmd.exe to file?

938 views Asked by At

I am trying to redirect a PS output to a file and process it further. For this I am using the Printer Port Redirection RedMon which is sending the output to CMD.exe

C:\Windows\system32\cmd.exe

As arguments I expected that something like the following should work, but it does not. "%1" contains the user input for filename.

/c >"%1"

or

/c 1>"%1"

or

/c |"%1"

or

/c > "%1" 2>&1

What almost works if I send the output to a batch file which writes it then to file.

/c WriteOutput.bat "%1"

However, the batch file is somehow altering the file (skipping empty lines, and ignoring exclamation marks and so on...) If possible I want to avoid a batch file. Is there a way to get it "directly" to a file? Select "Print to FILE" in the printer options is not an option for me. I want the same end result but via cmd.exe being able to process it further. Any ideas?

Edit: Well, that's the batch file I used. It neglects empty lines and space at the beginning.

@echo off
setlocal
set FileName=%1
echo(>%FileName%.ps
for /F "tokens=*" %%A in ('more') do (
    echo %%A>>%FileName%.ps
)
1

There are 1 answers

0
theozh On

Well, so far I still haven't found a direct way to write STDIN via RedMon via CMD.exe to a file. As @aschipfl wrote, all the versions with for /F will skip lines and ignore certain characters.

However, with the following batch script (via RedMon) I end up with a "correct looking" file on disk.

C:\Windows\system32\cmd.exe /c WritePS.bat "%1"

"%1" contains the user input for filename without extension. The Batch-File WritePS.bat looks as simple as this:

@echo off & setlocal
set FileName=%1.ps
more > "%FileName%"

However,

the resulting Postscript file is different from a file which I "Print to FILE" via the Postscript-Printer setup. I am pretty sure that all the printer settings which I can set are the same in both cases. If anybody has an idea why there might be a difference, please let me know.