1

I am trying to insert the content of a text file into a cmd console, by using:

start cmd.exe < c:\text.txt

I also tried:

start cmd.exe | c:\text.txt

However, the both open the cmd shell but nothing gets passed.

My point being in the end that I have a scheduler jenkins, and I pass the content of the text file inside the console when opening it with the start command. So I am not simply trying to print to the cmd console; I could just use echo for that different case.

aschipfl
  • 33,626
  • 12
  • 54
  • 99
uniXVanXcel
  • 807
  • 1
  • 10
  • 25
  • the answer below is not exactly responding to the question, but the conversation below has a nice hackish but elegant solution which responds to the question! – uniXVanXcel Apr 19 '17 at 15:36

1 Answers1

2

Something like this?

from CMD.exe itself,

type C:\Text.txt

If it is a batch file, then

type C:\Text.txt
pause

or to just see the content, use more

more C:\Text.txt

If you want to actually run commands from the file instead of trying to insert the commands from the text file into the cmd console, you should rather build it as a batch file which is an executable. you do this by renaming the file to either .bat or .cmd

you then insert you commands into the file and execute it by either double clicking the file or running it from a scheduler etc. Here is an example of a batch or cmd file:

echo Please wait while I execute.
tp merge $/ServerFolderA $/ServerFolderB

So just a few explanations on your initial commands. When you ran:

start cmd.exe | c:\text.txt

you actually told the system to run multiple executables from a single command. The pipe commands is like separator to specify each command. so this:

ping 127.0.0.1 | nslookup www.google.com | cmd.exe | c:\text.txt

will actually do all of those commands in sequence, first it will ping, the do nslookup, open cmd.exe then open c:\text.txt

Here you were on the right track, but my guess is you had a single line in the file and not a new line.

start cmd.exe < Text.txt

That will use the Text.txt file as an answer file so if I edit it and insert the following:

echo This is an answer file
ping 127.0.0.1
ping 10.132.4.99
echo Completed all commands

and then run start cmd.exe < Text.txt it will execute everything in sequence. This difference here is it reads the file line by line and displays each commands it runs. so your output will be something like this:

C:\>echo this is an answer file
this is an answer file
ping 127.0.0.1

Pinging 127.0.0.1 with 32 bytes of data:
Reply from 127.0.0.1: bytes=32 time<1ms TTL=128
Reply from 127.0.0.1: bytes=32 time<1ms TTL=128

Ping statistics for 127.0.0.1:
    Packets: Sent = 4, Received = 4, Lost = 0 (0% loss),
Approximate round trip times in milli-seconds:
Minimum = 0ms, Maximum = 0ms, Average = 0ms
ping 10.132.4.99

Pinging 10.132.4.99 with 32 bytes of data:
Reply from 10.132.4.99: bytes=32 time=3ms TTL=254

Ping statistics for 10.132.4.99:
   Packets: Sent = 4, Received = 4, Lost = 0 (0% loss),
Approximate round trip times in milli-seconds:
   Minimum = 3ms, Maximum = 8ms, Average = 4ms

As you can see this works perfectly, but it displays each command you are running except, it does not run the very last command which is echo Completed all commands. So to run all commands you always have to add a new line after your last commands. When however you rename it to .cmd it will just run the commands without displaying the commands to run and runs each line until the end. The other issue with the answer file is it reads line by line, so having 3 new lines with no text in the answer file will result in something like this

C:\>
C:\>
C:\>

So having just this in the answer file:

ping 127.0.0.1

will not work as it is a single line without the enter section.

but by just adding a new line after it will make it work.

I hope all this makes some sense.

Gerhard
  • 22,678
  • 7
  • 27
  • 43
  • thank you but i am not certain.. it flashes the console and then it goes away, have no time to actually see if there is anything inside.. – uniXVanXcel Apr 19 '17 at 13:28
  • are you doing this within a batch file? if it is, then type pause at the end of the batch file.. I will amend the answer. – Gerhard Apr 19 '17 at 13:29
  • What's the content of your text file? – Just Rudy Apr 19 '17 at 13:29
  • the content is "hello" – uniXVanXcel Apr 19 '17 at 13:29
  • @GerhardBarnard iam doing this inside a console just to try it out; then ill put it in a scheduler – uniXVanXcel Apr 19 '17 at 13:29
  • did you type exactly that? `type C:\Text.txt` remember type is a command. so you need to add it to the console. so you console should show `C:\>type C:\Text.txt` – Gerhard Apr 19 '17 at 13:31
  • yea i am , ok it does open it, but there is nothing in the console – uniXVanXcel Apr 19 '17 at 13:34
  • if you type `more C:\Text.txt` – Gerhard Apr 19 '17 at 13:34
  • i mean it's on c:\Desktop but same thing no? – uniXVanXcel Apr 19 '17 at 13:38
  • no it just opens it .i do 'start cmd.exe type C:\text.txt pause' – uniXVanXcel Apr 19 '17 at 13:41
  • no... just do what I said. `type C:\Text.txt` only, not Start cmd etc. just what I said. – Gerhard Apr 19 '17 at 13:42
  • ok thank you thatwas my mistake. it does print it inside that same very console. but my point is i have a jenkins scheduler and i need to open a cmd file and pass the content of a text file into it that console – uniXVanXcel Apr 19 '17 at 13:45
  • ok, then the question is not the same. wht is the point of pushing display to console only? is it for log file monitoring? what do you intend to do with that data? – Gerhard Apr 19 '17 at 13:46
  • i mean it was my question. Well, so yea iam trying to have it run commands on a database. I am trying to open a console, with the content from a different text file being its commands; and those commands would be executed by the console when it opens. I just edited it to be more clear. – uniXVanXcel Apr 19 '17 at 13:50
  • ok, that is completely different then. You want to execute commands with additional paramaters, sort of like "answer files" – Gerhard Apr 19 '17 at 13:51
  • ok. So I need to first know what the commands are you needing to run. does it stay constant? what are these commands exactly? lastly, are these commands issues in a specific type of console or can they be issued from cmd.exe – Gerhard Apr 19 '17 at 13:56
  • but at least, give me an example of what is in the text.txt file. – Gerhard Apr 19 '17 at 14:02
  • ok so iam actually useing the tfpt.exe console which is the developer command prompt console, which has for env variables and path the TFS variables. And so, the content of the text file are similar to this: tf merge $/ServerFolderA $/ServerFolderB When i run them directly it does not work, neither does it work with cmd.exe actually, which explains my questioning. iam doing 'start pathof_tpft.exe < pathof_text.txt' but not working too – uniXVanXcel Apr 19 '17 at 14:03
  • 1
    why not just change the name of the .txt to .cmd and making sure tfpt.exe is in your path then do `tp merge $/ServerFolderA $/ServerFolderB` inside the file, then run `Text.cmd` as executable – Gerhard Apr 19 '17 at 14:10
  • ok let me wrap my head around that and c how it would work out for a min. thanks! - brb – uniXVanXcel Apr 19 '17 at 14:13
  • oh i see, smart, i did not know you could do that, that;s basically like using the console directly via a batch file? --..edit: oh but i wanna use tfpt not cmd.exe – uniXVanXcel Apr 19 '17 at 14:15
  • i get it now, that's very smart!! :=)) – uniXVanXcel Apr 19 '17 at 14:21
  • yes it works!, i have to fix my path, but it does make the calls. i have to put the path of the cmd file within the server and then call it from there. i think that way it will be able to use the tfpt.exe. Otherwise the scheduler would depend on my station being online. However, let me run a local check and get back to you soon. thanks! – uniXVanXcel Apr 19 '17 at 14:48
  • ok, please mark the answer as correct once you feel that I assisted you :) – Gerhard Apr 19 '17 at 14:53
  • do you want to edit your answer so i can rate it? also i am going to add print statements to see if anything happens – uniXVanXcel Apr 19 '17 at 14:54
  • Yes, I will edit the answer once you confirm my help worked for you, then I will add everything that we discussed with explanation. – Gerhard Apr 20 '17 at 05:44