0

I am trying to run a batch script to get hostname and ip address and pipe to a .csv file. It seems I am experiencing an endless loop. I couldn't find ways to end the loop when exporting to excel. Here is my current script. I am testing to see whether this script is capable of updating from multiple PC's. Thanks

   @echo off
    set ip_address_string="IPv4 Address"
    echo Network Connection Test
    for /F "tokens=4 delims= " %%i in ('route print ^| find " 0.0.0.0"') do set localIp=%%i
    echo %ComputerName%
    echo Your IP Address is: %localIp%
    break
    echo Export into file 
    forfiles /s /C "cmd /c echo %ComputerName%, %localIp%" > MyTextOutput.csv
    break
Gerhard
  • 22,678
  • 7
  • 27
  • 43
  • you want the current IP address in Excel? – ashleedawg Jan 26 '18 at 07:09
  • Possible duplicate of [How to retrieve this computer's IP address?](https://stackoverflow.com/questions/828496/how-to-retrieve-this-computers-ip-address) – ashleedawg Jan 26 '18 at 07:14
  • yes please result i get endless loop that mean to be successful export in bash script! –  Jan 26 '18 at 07:16
  • Is there a reason you need to complicate this with a batch file instead of retrieving the IP from within Excel (like [this](https://stackoverflow.com/a/949986/8112776))? – ashleedawg Jan 26 '18 at 07:50
  • @ashleedawg _"from within Excel"_ ? That is far from "within" Excel, that is vbscript using WMI. – Gerhard Jan 26 '18 at 08:02
  • @GerhardBarnard -- hmm, apparently I'm not the only one feeling argumentative tonight... :-) ..The [link](https://stackoverflow.com/a/949986/8112776) I provided is **VBA** which runs from *within* an **Excel Module**. It *might* run with VBS as well, I don't know. Sure, it calls a WMI Service, but no external program (including batch file) is necessary. I consider that to be the definition of *"from within Excel"*... – ashleedawg Jan 26 '18 at 08:18
  • @ashleedawg. Not feeling argumentative. The link does not provide running from the excel module, it only shows the vbascript which would surely be more complicated to the OP... besides, I am getting the idea that the OP wants to run the batch script on PC's in the network writing output to a share, it will be much simpler to distribute the batch rather than installing (where needed) multiple wmi scripting modules etc. – Gerhard Jan 26 '18 at 08:22
  • @GerhardBarnard - The code from that link, copy-and-pasted into a blank Excel VBA module as-is, provides me with the current IP as well as additional information, and could be easily simplified to provide only the IP.. – ashleedawg Jan 26 '18 at 08:31
  • @GerhardBarnard -- [Here](https://stackoverflow.com/a/48458267/8112776) you go. – ashleedawg Jan 26 '18 at 09:00
  • What is `forfiles` supposed to do? Effectly it outputs the same line as often as you have files in the current folder and it's subfolders (technically not an "endless" loop, as you don't have infinite number of files on your disk) – Stephan Jan 26 '18 at 09:30

1 Answers1

-1

Here is a VBA function to return current primary IP Address from within an Excel/VBA Module, simplified for @gerhard-barnard to minimize number of lines of VBA code:

Function GetIP()
    Dim IPConfig: For Each IPConfig In GetObject("winmgmts:{impersonationLevel=impersonate}!\\.\root\cimv2").ExecQuery("SELECT * FROM Win32_NetworkAdapterConfiguration WHERE IPEnabled=TRUE"): GetIP = IPConfig.IPAddress(0): Next
End Function

This is technically three lines of code (but improper coding technique.. just making a point).

See the full (proper) ingenious answer by "Helen", here. No batch files or VB Script required.

ashleedawg
  • 20,365
  • 9
  • 72
  • 105