Search code examples
batch-filehttp-redirectcmdtfs-power-tools

insert command into start cmd


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.


Solution

  • 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.