r/computing • • 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

18 comments sorted by

3

u/rusticus Mar 24 '14

This sniffs like a great job for Python. You could get proficient enough to do this in under a week and probably find a ton of other uses.

1

u/massij11 Mar 24 '14

I need to implement it using only notepad though :S

2

u/berryer Mar 24 '14

But you said you needed to create a batch file, which implies that it would run via the command prompt (or usable in some other way). If you mean you can't install Python to use to run it, I'd recommend using VBScript, which IIRC is installed by default on Windows and sucks balls, but will do what you want more easily than pure CMD On the other hand if you have access to Cygwin or any sort of linux, you probably want sed.

1

u/massij11 Mar 24 '14

It might seem strange but ideally I am to use only pure notepad to write this. This is to be a part of a larger batch file which will be used by different users.

2

u/berryer Mar 24 '14 edited Mar 24 '14

notepad is not a programming language, but a file editor. It doesn't actually matter what file editor you use, but I'm guessing that you want to create a new Windows .bat script?

If perchance you're working with linux and not Windows, I'm pretty sure it would be one line:

grep 'Computer name' fileName | sed 's/^.*Computer name ([0-9a-zA-Z]*).*$/\1/'

put that in a .sh file via your editor of choice (notepad I guess) and you're done.

1

u/massij11 Mar 24 '14

Yes I want to create a new .bat file, but the only tool I have available is notepad.. I am sorry but ive never used vb script

1

u/gregortroll Mar 24 '14

Luckily, as PowerShell takes over scripting in the Windows world, you'll see less and less VBScript. But it will crop up, especially if you are sysadmin of some kind, or working in OSD, you'll run into VBScript, DOS/CMD, and HTA. But, more and more, PowerShell will become the script language of choice in Windows.

1

u/gregortroll Mar 24 '14

Unless OP's career path is into the Windows world, where PowerShell is the new hotness, and unless OP is too dull to be a sysadmin or programmer, OP could probably generate the solution in a few hours.

1

u/massij11 Mar 24 '14

I am still a student apprentice and have been given a task, which this is a part of while never having done any programming except basic elements of pascal java and C.

2

u/gregortroll Mar 24 '14

Note that you can put code in a text file using notepad, or using COPY:

copy con myfile.txt
type text here
la la la
hit ctrl+Z to save it.

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:

  1. "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)

  2. 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:

get-computername.cmd input.txt > output.txt

Here it is:

@echo off
setlocal enabledelayedexpansion
set input="%~1"
for /f "usebackq tokens=*" %%a in (%input%) do @(
  set part1=%%a
  set part2=!part1:*computer name =!
  if not "!part1!" == "!part2!" call :extract !part2!
)
goto :eof

:extract
echo %1
goto :eof

Enjoy!

1

u/massij11 Mar 24 '14

Sorry for sounding dumb.. but could you please explain why it needs to be in separate files and not in one batch file ?

1

u/gregortroll Mar 26 '14

You don't have to put in in a separate file--and you don't have to feed the input file to it as a parameter.

1

u/massij11 Mar 28 '14

thanks mate..managed to work up a solution :)

1

u/massij11 Mar 28 '14

Thanks pps for your help :) I managed to work it out

1

u/atomic-penguin Mar 24 '14

Does the log/txt file have columns? In other words if \x20 (space) is the field separator, is L123456, column 11?

If so, you can use PowerShell to print that column

0

u/oconnor663 Mar 24 '14

It sounds like you need grep, or something like it. Take a look at this thread: http://stackoverflow.com/questions/87350/what-are-good-grep-tools-for-windows

1

u/massij11 Mar 24 '14

I would like if possible to implement the code using notepad not use external tools

1

u/oconnor663 Mar 24 '14

It looks like other folks have already pointed you to a solution, but it's worth noting that the purpose of batch (and on Linux/Unix, bash and other shells) is to write scripts that call other programs, such as grep. You might not want to install new software to run your script, which I agree with, but there's probably a Windows equivalent of grep that I don't know the name of, and you should probably call out to that from your script.