Tom's Guide | Tom's Hardware | Tom's Games
![]() |
![]() |
![]() |
Firstly I beg for your forgiveness if this is already explained in earlier posts, i tried searching but couldn't find quite what i needed.
I am building a program that logs user inputs into %USERNAME%.log -files with other useful information (workState, userID, workID, date, time, computer) in next form:
"active u000000 000000000001 31.12.2000 0:00:00,00 W0000000"I am already using this to get a hold of the latest line:
setLocal EnableDelayedExpansion
for /f "tokens=* delims= " %%a in (%USERNAME%.log) do (
set var=%%a
)
echo !var!As always after already creating everything. I heard too late that a good reporting tool has to be implemented so I most definitely need all of the words to be set as variables for later use. For example "active" would be "%workState"
Is this even possible?
I'm sorry if it was hard for you guys to understand what I ment, batch coding is totally new area for me.

<untested>
for /f "tokens=1-6 delims= " %%a in (%USERNAME%.log) do (
set workState=%%a
set userID=%%b
set workID=%%c
set wdate=%%d
set wtime=%%e
set computer=%%f
)I changed date and time to wdate and wtime because it is generally not a good idea to use these variable names with nt sp6(so I've read) and above. These systems have inbuilt system time and date functions built in to these variables, if you set to the they will hold your information until the process or setlocal, if present, is ended. If they are holding your information they cannot serve the current time or date.

Such professional help is awarded with more(!) problem solving!
My next problem is somewhat similiar.
I am having workIDs.txt with lines such as:000000000001 WORK1
000000000002 WORK TWO
000000000003 THIS IS WORK THREE
000000000004 And fourthThe user inputs ID-number to the program with barcode and the program takes it to check if it's on the list. This all is ok.
But this reporting system I told earlier about.. i require it to print out only the text from work (for example "THIS IS WORK THREE"), the code is not needed in report.
I understand it is not possible to use your suggested code in workIDs.txt for it having lines with variating amount of words.
In addition I don't know how batch understands TABs this file has between the workIDs and the texts (which are apparently not shown in my example). Something that MIGHT help is that the workID-code always has the same amount of digits.
Could you help?

slarsta,
If the below batch file is called getcode.bat,
launching it with "getcode1 000000000003"
gets you "THIS IS WORK THREE" as output.
for /f "tokens=1*" %%B in (workids.txt) do (
if [%1]==[%%B] (echo %%C & exit/b)
)
Assumption is that the code is not repeated in the workids.txt. If it repeats, you have remove exit/b.--
Holla.

Hi Judago,
Yep, bad idea to tinker with built in vars in any version of DOS or windows.
Also a bad plan to name a file, directory or script the same as a built in var, a system defined device, a command or a reserved word.
To name just a few: DATE PATH CD CON AUX PRN
=====================================
If at first you don't succeed, you're about average.M2

Thank you Holla, that did the trick!
I am unable to use a separate program but I could turn that into a subroutine I can call.
Thank you guys!

Hi M2,
Your right about the device names, just as troublesome as command or special variable names. There are too many of these to list in each thread so I think I might write one of those tutorials(unless someone beats me to it) on the subject so it would be as simple as throwing a link instead of repeating it over and repeating it over and....

"These systems have inbuilt system time and date functions built in to these variables"
I was already aware of this fact since I'm already using them as my advantage. Please forgive me that I forgot to write them correctly into my example.

Don't be too concerned, I was just bringing it up in case you didn't know. When it comes down to it you decide what goes into your script and what doesn't.
The only thing that made me mention it is that I thought "workState, userID, workID, date, time, computer" correlated to "active u000000 000000000001 31.12.2000 0:00:00,00 W0000000".
I mention these sorts of things because they can cause problems and most people asking questions here are either new to this sort of thing or are already having problems.

Better safe than sorry, got it!
I finished the whole program this morning and it works like a charm. This is because of you guys! Thank you again.

![]() |
Holding Window Message
|
Batch File Scripting
|

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.
| Ads by Google |