Tom's Guide | Tom's Hardware | Tom's Games
![]() |
![]() |
![]() |
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.txtNumber of files to delete: 27 Total
Total size of files to delete: 1,427 KBI 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

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.

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

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=%%jAnd that screenshot I posted before is the result.

Replace
@echo off & setlocal EnableDelayedExpansion
with
echo off
setlocal EnableDelayedExpansionand
set last=????
with
set last=XXXX

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.exeAlso tried command.com
Still get the same error..

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

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 .batBut anyway, ran your script with no errors from a command prompt. Awesome!
When I try to do a
echo %Number%
orecho %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?

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) Nextsave as myscript.vbs and on command prompt
c:\test> cscript /nologo myscript.vbs

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.

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.

![]() |
Script to check and downl...
|
in a Tree
|

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