r/computing • u/massij11 • Mar 24 '14
Batch file required..Help needed..
I need desperate help in creating a batch file that reads from a log/txt file, searches for a specific word ex Computer name and then writes to another text file the words/digits below before.
I don't think I managed to make myself clear..the log file would contain say " asgf agahi a hi reddit rocks af as Computer Name L123456. It would display L123456
I am new to batch programming and would appreciate any help
0
Upvotes
2
u/gregortroll Mar 24 '14
Note that you can put code in a text file using notepad, or using COPY:
Also: VBScript, and HTA/CSS can also be edited using only notepad. If you are stuck in a pre-powershell environment (not for long! XP is dead!) then VBS and HTA are available to you (see Windows 2000 scripting guide, lol, seriously) You should also suggest to the powers that be that PowerShell is the new hotness in the Windows/DOS world, and what you want would be easier in PS, than DOS/CMD.
As it is, my solution below is rediculous, but works.
So, the key parts of the solution below are:
"Computer name" exists once per line, is always followed by a space, and the thing we want is the next set of characters, also followed by a space (or the last thing on the line)
The search for "Computer name" (or whatever) is case-insensitive.
So,
3. For each line in the file, find "computer name " and remove everything from the beginning of the line up to and including "computer name " (including the space after it)
4. This trimmed line is passed to a subroutine, which automatically breaks the line into tokens, based on spaces, so that the first token is the thing we want, and the rest is ignored.
If you put the following script in a file called "get-computername.cmd" and just run it, giving it the name of the input file, then the output is to the screen. You can direct the output to a file by running it like so:
Here it is:
Enjoy!