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 •


#1
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 •

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

Report •

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

@echo

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


Report •

Related Solutions

#4
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 •

#5
May 1, 2009 at 05:49:13
Replace

@echo off & setlocal EnableDelayedExpansion

with

echo off
setlocal EnableDelayedExpansion

and

set last=????

with

set last=XXXX


Report •

#6
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 '@'

Help?

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

Also tried command.com

Still get the same error..


Report •

#7
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..

ALLUSERSPROFILE=C:\ProgramData
APPDATA=C:\Users\Methical\AppData\Roaming
CLASSPATH=.;C:\Program Files\Java\jre6\lib\ext\QTJava.zip
CommonProgramFiles=C:\Program Files\Common Files
COMPUTERNAME=NEMESIS
ComSpec=C:\Windows\system32\cmd.exe
DFSTRACINGON=FALSE
FP_NO_HOST_CHECK=NO
HOMEDRIVE=C:
HOMEPATH=\Users\Methical
LOCALAPPDATA=C:\Users\Methical\AppData\Local
LOGONSERVER=\\NEMESIS
NUMBER_OF_PROCESSORS=1
OS=Windows_NT
Path=C:\Windows\system32;C:\Windows;C:\Windows\System32\Wbem;C:\Program Files\QuickTime\QTSystem\
PATHEXT=.COM;.EXE;.BAT;.CMD;.VBS;.VBE;.JS;.JSE;.WSF;.WSH;.MSC
PROCESSOR_ARCHITECTURE=x86
PROCESSOR_IDENTIFIER=x86 Family 6 Model 15 Stepping 13, GenuineIntel
PROCESSOR_LEVEL=6
PROCESSOR_REVISION=0f0d
ProgramData=C:\ProgramData
ProgramFiles=C:\Program Files
PROMPT=$P$G
PUBLIC=C:\Users\Public
QTJAVA=C:\Program Files\Java\jre6\lib\ext\QTJava.zip
SystemDrive=C:
SystemRoot=C:\Windows
TEMP=C:\Users\Methical\AppData\Local\Temp
TMP=C:\Users\Methical\AppData\Local\Temp
TRACE_FORMAT_SEARCH_PATH=\\NTREL202.ntdev.corp.microsoft.com\4F18C3A5-CA09-4DBD-B6FC-219FDD4C6BE0\TraceFormat
USERDOMAIN=Nemesis
USERNAME=Methical
USERPROFILE=C:\Users\Methical
windir=C:\Windows


Report •

#8
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%

or
 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 •

#9
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()
j=0
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)
		j=j+1
	End If 
'	WScript.Echo strLine	
Loop
For i=0 To UBound(arr)
	WScript.Echo arr(i)
Next

save as myscript.vbs and on command prompt

c:\test> cscript /nologo myscript.vbs

Unix Win32 tools | Gawk for Windows


Report •

#10
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 •

#11
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 •

#12
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