Solved

Need batch file to accept piped input, or widows 'cat' equivalent that can be piped-to. 'type' and 'for /f' not working.

Posted on 2013-05-16
10
723 Views
Last Modified: 2014-06-11
Hello,
     In unix shell scripts, I could use cat > /tmp/sometempfile at the beginning of a script to receive input being piped into the script, to then do something with it. I'm trying to do the same on Windows, using built in commands, so it can work on all computers in the company without installing anything.

The 'type' command requires a filename argument / doesn't seem to support being piped to, and putting  
for /F "delims=" %%a in ('more') do @echo %%a >> %temp%\sometempfile

Open in new window

 at the beginning of the batch file is adding CR characters and otherwise screwing up just the binary parts of files I'm piping through it.

Before I give up, and install cygwin on all computers, just to get the cat command, is there any way to do this with native commands in a batch file? Switching the character set to unicode, etc?
0
Comment
Question by:RalphNY
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
10 Comments
 
LVL 43

Expert Comment

by:Steve Knight
ID: 39173258
You should be able to do:

@echo off
more > x.txt

to receive output of dir | script.cmd  into x.txt

which then you can parse the x.txt as you wish.  You might also be better off using one redirect rather than line by line... but presumably more is messing with the data you are piping in.

(for /F "delims=" %%a in ('more') do @echo %%~a)> %temp%\sometempfile

you could also probably use this, not sure if any better or worse for you.

find /v "" > x.txt

Does the batch have to be piped into, could it not take a parameter, e.g.

yourbatch.cmd mybinaryfile.dat

Steve
0
 
LVL 56

Expert Comment

by:Bill Prew
ID: 39173605
Why are you piping binary data, that's typically a bad idea, piping is really meant for text data.

~bp
0
 

Author Comment

by:RalphNY
ID: 39173720
@Dragon-it,
    Thanks for the tries, but unfortunately, all of those give the same output/problems as the 'for /f' command I had- the output has CRLFs instead of just LFs and the binary/Stream portion is modified in a lot of ways, and made longer.

@billprew,
   I'm redirecting printer output through this script, using redmon, to convert (mostly PDFs, but anything sent to this printer) to single-page-per-file TIFs using ghostscript.  There are also other things I want to do, like crop the files, where I will need to solve this same inability-to-cat-piped-input-to-a-temp-file-problem.

I have the redirected printer to ghostscript working: You print something to it, you get sequentially numbered TIFs on your desktop. But - if you do it a 2nd, etc time, it starts the page numbers at 1 again and overwrite the previous pages printed.

I want to have the TIFs be named with both timestamps and page numbers, so subsquent printouts don't clobber the previous ones.

I can get the timestamp, but currently, ghostscript is the first command called in the script (see below), so I can't get the timestamp until after I need it.

If I could receive the piped in input into a temp file, I could then get the timestamp to use for the file name, then use that temp file as an argument to the 'type' command, to pipe that temp file to ghostscript, in the batch file.

C:\gs\bin\gs.exe -Ic:\gs\lib;C:\gs\fonts -sDEVICE=tiffg4 -sOutputFile="%timestampfilename%p%d.tif" -dNOPAUSE -dSAFER -dBATCH -r300 -sPAPERSIZE=letter -
0
Instantly Create Instructional Tutorials

Contextual Guidance at the moment of need helps your employees adopt to new software or processes instantly. Boost knowledge retention and employee engagement step-by-step with one easy solution.

 
LVL 43

Expert Comment

by:Steve Knight
ID: 39173803
hmm, so presumably you could get the timestamp you want regardless of the redirecting issue then as that would run the same - I presume you just want it as 'now', why not:

@echo off
set timestamp=%date:/=-%@%time::=%

i.e. crude way to get the current date and time without ? or : chars.  you could also put alll pages in a subdir perhaps, i.e.
md %timestamp% and then have "%timestamp%\%p%d" or whatever as filename.

on phone so sorry if missed something there... will look back on pc later.

steve
0
 
LVL 16

Expert Comment

by:DansDadUK
ID: 39174116
I agree with billprew - most of the DOS commands were designed for text data (and in Windows systems, text line termination is usually the CarriageReturn-LineFeed pair), although the copy command allows for binary data (with the /b switch) - but (like type) requires parameters rather than accepting an input pipe.

What about investigating use of Windows PowerShell scripts instead of the old DOS batch scripts?
I don't know whether or not the numerous built-in PowerShell cmdlets will provide a solution to your requirement.
0
 
LVL 43

Expert Comment

by:Steve Knight
ID: 39174193
If you want a date in specific format BTW try one of these methods of mine.  Get that into an environment variable and use it in your commandline for gs.exe

http://www.experts-exchange.com/OS/Microsoft_Operating_Systems/MS_DOS/A_1153-Using-dates-in-batch-files-scripts.html

Another alternative might be VBScript.  You can read STDIN into a variable if wanted then do what you want with, i.e. parse or write it out into a file:
Dim STDIN
STDIN = WScript.StdIn.ReadAll
WScript.Echo STDIN

Open in new window


dir | cscript //nologo stdin.vbs
0
 
LVL 56

Expert Comment

by:Bill Prew
ID: 39184041
I'm redirecting printer output through this script, using redmon, to convert (mostly PDFs, but anything sent to this printer) to single-page-per-file TIFs using ghostscript.  There are also other things I want to do, like crop the files, where I will need to solve this same inability-to-cat-piped-input-to-a-temp-file-problem.
Rather than using STDOUT/STDIN, why not let the printer driver redirect to an actual file, and then pick up that file in the batch script?

~bp
0
 
LVL 85

Accepted Solution

by:
oBdA earned 500 total points
ID: 39225047
Since you're using RedMon anyway, let RedRun.exe (part of the package) generate a temp file and start your script; RedRun is designed exactly for what you're trying to do.
From the Readme.txt: When started, RedRun will write standard input to a temporary file, until it reaches end of file.  It will then start the specified program.  RedRun will wait until the specified program terminates, then will delete the temporary and terminate itself.
In the properties of RedMon, enter the path to RedRun.exe as program to redirect the port to, and as arguments for the program, enter the path to your script, followed by "%1", for example like this:
C:\CreateTIFF\CreateTIFF.cmd "%1"
In your script, you now can access the print file using "%1" and pass it as argument to Ghostscript.
0

Featured Post

Free Backup Tool for VMware and Hyper-V

Restore full virtual machine or individual guest files from 19 common file systems directly from the backup file. Schedule VM backups with PowerShell scripts. Set desired time, lean back and let the script to notify you via email upon completion.  

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The article will include the best Data Recovery Tools along with their Features, Capabilities, and their Download Links. Hope you’ll enjoy it and will choose the one as required by you.
This article was originally published on Monitis Blog, you can check it here . If you have responsibility for software in production, I bet you’d like to know more about it. I don’t mean that you’d like an extra peek into the bowels of the sourc…
Viewers will learn how to use the Hootsuite Dashboard.
The viewer will learn how to successfully download and install the SARDU utility on Windows 8, without downloading adware.

626 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question