lundi 20 avril 2015

Windows cmd echo / pipe is adding extra space at the end - how to trim it?

I'm working on a script that executes a command line application which requires user input at runtime (sadly command line arguments are not provided).

So my first attempt looked like this:

@echo off
(echo N
echo %~dp0%SomeOther\Directory\
echo Y)|call "%~dp0%SomeDirectory\SadSoftware.exe"

At first glance it looked like it worked pretty well, but as it turned out it didn't. After investigation I found out that the directory I was passing to the software contained extra space at the end, which resulted in some problems.

I looked around for a while and found out following question: echo is adding space when used with a pipe .

This explained what is going on, but didn't really help me solve my problem (I'm not too familiar with batch programming).

At the moment I "kind of solved" my problem with an ugly workaround:

echo N> omg.txt
echo %~dp0%SomeOther\Directory\>> omg.txt
echo Y>> omg.txt

"%~dp0%SomeDirectory\SadSoftware.exe"<omg.txt

del omg.txt

This solution works, but I'm less than happy with it. Is there some prettier way? Or even uglier way, but without temporary file?

Aucun commentaire:

Enregistrer un commentaire