Batch File trouble

May 1, 2009 at 01:38:03
Specs: Windows Vista
Hi guys
Having some trouble getting a code to work to extract the numerical values out of the following two lines into a seperate variable each to be called later in my batch file.
These two lines are the last two lines in a text file called logfile.txt

Number of files to delete: 27 Total
Total size of files to delete: 1,427 KB

I just want the 27 in one variable, and the 1,427 in another. These are randomly generated from a scan, and could include more digits. Hope you understand :)

Thanks for yall your help

Batch file please, i am using windows

See More: Batch File trouble

Report •

May 1, 2009 at 03:19:02
First of all start your batch by coding

@echo off & setlocal EnableDelayedExpansion

Then the following statements do the job

set last=????
for /F "delims=" %%j in (logfile.txt) do (
  set before_last=!last!
  set last=%%j
for /F "tokens=6" %%j in ("%before_last%") do set Number=%%j
for /F "tokens=7" %%j in ("%last%") do set Size=%%j

The script is not tested, so, please, post again if anything goes wrong.

Report •

May 1, 2009 at 03:30:40
I get this error here

Report •

May 1, 2009 at 03:40:18
The initial line of the batch starts with


From your display it seems you have an extra exoteric char prefing the @. Please verify your text .bat

Report •

Related Solutions

May 1, 2009 at 04:41:11
This is nothing before the @echo off. This is the batch file here..

@echo off & setlocal EnableDelayedExpansion
set last=????
for /F "delims=" %%j in (cleanafterme.txt) do (
  set before_last=!last!
  set last=%%j
for /F "tokens=6" %%j in ("%before_last%") do set Number=%%j
for /F "tokens=7" %%j in ("%last%") do set Size=%%j

And that screenshot I posted before is the result.

Report •

May 1, 2009 at 05:49:13

@echo off & setlocal EnableDelayedExpansion


echo off
setlocal EnableDelayedExpansion


set last=????


set last=XXXX

Report •

May 1, 2009 at 16:27:31
Tried that; still didn't work

If I remove the @ from the start of the line, the error comes up with an 'e' instead of an '@'


I'm running Windows Vista Home Basic SP1.
And launchin' the command prompt via cmd.exe

Also tried

Still get the same error..

Report •

May 1, 2009 at 17:46:13
An update.

I tried a batch file with just @echo off in it and nothing else and ran it, I get the same error. So I know now its now your script.
But now I don't know what my problem is..
I have piped the set command to a text file and pasted it below incase it might help someone help me... It doesn't look like anythin' has changed to me..

CLASSPATH=.;C:\Program Files\Java\jre6\lib\ext\
CommonProgramFiles=C:\Program Files\Common Files
Path=C:\Windows\system32;C:\Windows;C:\Windows\System32\Wbem;C:\Program Files\QuickTime\QTSystem\
PROCESSOR_IDENTIFIER=x86 Family 6 Model 15 Stepping 13, GenuineIntel
ProgramFiles=C:\Program Files
QTJAVA=C:\Program Files\Java\jre6\lib\ext\

Report •

May 1, 2009 at 19:13:54
Ok well I realised that i had been editing the same batch file over and over again.
So I deleted that one and started a new one, and it worked.
So I dont know what was wrong with the other one, starting to think it was somethin' to do with the encoding when i saved the notepad file to .bat

But anyway, ran your script with no errors from a command prompt. Awesome!

When I try to do a

echo %Number%

 echo %Size%

I get the return value %Number% or %Size%
Not the numerical values that are in those lines.

Would it help if i gave u the whole text file?

Report •

May 1, 2009 at 21:12:40
here's a vbscript
Set objFS = CreateObject("Scripting.FileSystemObject")
strFile = "c:\test\file.txt"
Set objFile = objFS.OpenTextFile(strFile)
Dim arr()
Do Until objFile.AtEndOfStream
	strLine = objFile.ReadLine
	If InStr(strLine,"Number of files to delete") > 0 Or  _
	InStr(strLine,"Total size of files to delete") > 0 Then 
		splitted = Split(strLine," ")
		ReDim Preserve arr(j)
		arr(j) = splitted(UBound(splitted)-1)
	End If 
'	WScript.Echo strLine	
For i=0 To UBound(arr)
	WScript.Echo arr(i)

save as myscript.vbs and on command prompt

c:\test> cscript /nologo myscript.vbs

Unix Win32 tools | Gawk for Windows

Report •

May 2, 2009 at 17:51:39
Cheers for your efforts there ghostdog.
I would prefer it in batch though =)
I am running Windows Vista Home Basic SP1
Would like the code compatible with XP to, and maybe Win 7.

Report •

May 3, 2009 at 01:14:01
i doubt you don't have cscript/wscript engines in your system.

Unix Win32 tools | Gawk for Windows

Report •

May 3, 2009 at 12:02:59
To ghostdog,

for your info the guy sent me the log file to parse and I realized his true problem:

the text file is encoded in a Unicode-like format, but missing the 0xFF-0xFE first two bytes marker. So the commands refuse to correctly interpret its content. It seems he saves using this mysterious format according to the error faced while running my batch script.

As you know, Windows can usually process Unicode text files assumed they are correctly encoded.

I'll send him the modified version of my script avoiding the use of delayed expansion and warn him about the format of his text files.

Report •

Ask Question