Jump to content

Expand to file size (%~zI) doesn't work in XP


Recommended Posts

I'm trying to install Grub in unattended setup. My script replaces ntldr with grub. However, I want to check the size of ntldr with a script.

I have used the script below and it works flawlessly in Windows 2000. However, in Windows XP, it doesn't work. Anyone know why the difference in the two?

Here is the script:

echo.
@echo off
set tags=boot.ini ntldr
for %%I in (%tags%) do call :setvars %%I

:setvars
for %%J in (Z Y W X W V U T S R Q P O N M L K J I H G F E D C) do if exist "%%J:%%I" set %%I=%%J: &&set %%Iz=%%~zI
echo.
echo.
echo ntldr location is %ntldr%
echo boot.ini location is %boot.ini%
echo.
echo Size of ntldr is %ntldrz%
echo Size of boot.ini is %boot.iniz%
pause
goto :eof

It correctly finds the location of ntldr, but %~zI is not expanded with filesize. So all I get is:

Size of ntldr is

Link to comment
Share on other sites


My learned friend is correct, that file will not work flawlessly!

here is an alternative method for you

@ECHO OFF &SETLOCAL ENABLEEXTENSIONS
FOR /F %%? IN ('MOUNTVOL^|FIND ":\"') DO (CALL :GETSIZE %%?)
ECHO/&ECHO/ ntldr location is %ntldr%
ECHO/ boot.ini location is %boot.ini%
ECHO/&ECHO/ Size of ntldr is %ntldrz% bytes
ECHO/ Size of boot.ini is %boot.iniz% bytes
PAUSE &ENDLOCAL &GOTO :EOF
:GETSIZE
PUSHD %1 2>NUL
FOR %%? IN (BOOT.INI NTLDR) DO (IF EXIST %%? (
SET "%%?=%CD:~0,2%" &SET "%%?z=%%~z?"))
POPD &GOTO :EOF

Link to comment
Share on other sites

Also, it (the original one, I mean, Yzowl beat me on time) loops more times than needed.

See if this works:


@echo off
for %%A in (Z Y W X W V U T S R Q P O N M L K J I H G F E D C) do call :setvars %%A:\ntldr %%A:\boot.ini
pause
goto :eof

:setvars
if exist %1 (
if exist %2 (
echo.
echo.
echo %1 size is %~z1
echo %2 size is %~z2
echo.
)
)
goto :eof

this one checks if BOTH boot.ini and NTLDR are present.

jaclaz

P.S.: reference:

http://www.robvanderwoude.com/ntcall.html

Edited by jaclaz
Link to comment
Share on other sites

If both files are expected on the same drive then jaclaz' solution will serve you a little better.

The only thing that I'm wondering is why Z»C as opposed to C»Z

The following version will break out of the loop as soon as the files are found:

@ECHO OFF
FOR %%? IN (C D E F G H I J K L M N O P Q R S T U V W X Y Z) DO (
CALL :SETVARS %%?:\NTLDR %%?:\boot.ini ||GOTO :ABORT)
:ABORT
PAUSE &GOTO :EOF
:SETVARS
IF EXIST %1 (IF EXIST %2 (
ECHO/&ECHO/%1 size is %~z1 bytes&ECHO/%2 size is %~z2 bytes&ECHO/&&EXIT/B 1))
EXIT/B 0

What that means is that as soon as boot.ini and NTLDR are found on the same drive it will not continue checking any remaining drives.

You can of course still have your drive letters in whichever order you prefer!

Link to comment
Share on other sites

You read my mind Yzowl. If I went from C > Z, then it finds the last drive that has those files, which is what I didn't want. That's why I changed it from Z > C. Thanks for your better script, I will try it.

Other things is that it does work in Windows 2000 *flawlessly.* I've tried it at work just the way I posted it. Then I tried the same script at home on my XP and it finds the location but does not expand the size.

It's strange.

I'll try your last script though.

Edit:

Your last scripts works, Yzowl. And it breaks the loop once it finds the drive.

Thanks, again.

It's baffling why my original one finds location but not the size.... I tried replacing I with 1 as Delprat mentioned, but that doesn't work at all. With 1, it does not even find the location. That's why I used I.

Edited by spacesurfer
Link to comment
Share on other sites

It's baffling why my original one finds location but not the size.... I tried replacing I with 1 as Delprat mentioned, but that doesn't work at all. With 1, it does not even find the location. That's why I used I.

Batches are strange sometimes... Try to qualify the pathes instead of supposing that the CD on each drive is the root (you just need to CALL :setvars \%%I --- but I dunno if the SET %1=... still works with %1 starting with a backslash).

Same bug for the size : to get %~z1 working, %1 must allow the OS to locate the file, thus the CD matter if %1 doesn't have path ("X:" is only a drivespec, not a path on that drive). (and sometimes it just doesn't work -- ex. in a for /f loop with a dir /b command)

I still don't understand how come this works on Win2K. It must be bugged :angel

++

Link to comment
Share on other sites

I still don't understand how come this works on Win2K. It must be bugged

Just for the record it does not work fully in win2k, or at least it does not work how it should.

Here is the output of the original script when run for the first time in a newly open box in Win2K

(I omitted the PAUSE statements and took out some line feeds):


O:\>tntori

O:\>echo.

ntldr location is
boot.ini location is C:

Size of ntldr is
Size of boot.ini is 171

ntldr location is C:
boot.ini location is C:

Size of ntldr is 215472
Size of boot.ini is 171

ntldr location is C:
boot.ini location is C:

Size of ntldr is 215472
Size of boot.ini is 171

Whilst here is the one from my script (I have a copy of NTLDR/BOOT.INI in O:\) :


O:\>tnt
O:\ntldr size is 215472
O:\boot.ini size is 171

C:\ntldr size is 215472
C:\boot.ini size is 741

And the one from first Yzowl one, (I don't like using MOUNTVOL as it accesses the floppy):


O:\>tnt2

ntldr location is O:
boot.ini location is O:

Size of ntldr is 215472 bytes
Size of boot.ini is 171 bytes

And the one from 2nd Yzowl one:


O:\>tnt3

C:\NTLDR size is 215472 bytes
C:\boot.ini size is 741 bytes

jaclaz

Link to comment
Share on other sites

I don't like using MOUNTVOL as it accesses the floppy
I guess that its part of explorer /Windows GUI which accesses the drive; so in testing, to prevent it, I usually use
('MOUNTVOL^|FIND ":\"^|FIND /V "A:\"')

During setup the GUI is not loaded so the floppy drive is not accessed and the script I suggested is fine.

Link to comment
Share on other sites

Well if you have copied and pasted correctly and you have not altered my code, then I suppose it must be.

I can say that because as you can see from my posting, it says

 (IF EXIST %%? (

not

 (IF EXIST " %%?" (

Link to comment
Share on other sites

I tested Yzöwl script on XP and it work but on Vista I get this

VistaDoesNotWork.png

Here is a VBS script that only checks the harddrives for the 2 files. This worked on Both XP and Vista.

Save As Check_BootIni_Ntldr.vbs

Dim Fso	  : Set Fso = CreateObject("Scripting.FileSystemObject")
Dim Act : Set Act = CreateObject("Wscript.Shell")
Dim Cmd1, Cmd2, F1, F2, KB, Lb1, Lb2
Cmd1 = "Attrib.exe -h -s " : Cmd2 = "Attrib.exe +h +s " : KB = 1024
Lb1 = " File Path " : Lb2 = " File Size " : F1 = "\boot.ini" : F2 = "\ntldr"
Dim ChkFile, Drv, File, Rpt, Rpt1, StrD
Set Drv = Fso.Drives
For Each StrD In Drv
If StrD.DriveType = 2 Then
Act.Run(Cmd1 & StrD & F1),0,True : Act.Run(Cmd1 & StrD & F2),0,True '/-> Remove Attributes
If Fso.FileExists(StrD & F1) Then
ChkFile = StrD & F1 : SortTheFileSize()
Rpt1 = Rpt1 & vbCrLf & Rpt : Act.Run(Cmd2 & StrD & F1),0,True '/-> Replace Attribute
End If
If Fso.FileExists(StrD & F2) Then
ChkFile = StrD & F2 : SortTheFileSize()
Rpt1 = Rpt1 & vbCrLf & Rpt : Act.Run(Cmd2 & StrD & F2),0,True '/-> Replace Attribute
End If
End If
Next
Function SortTheFileSize() '/-> Checks File Size Sorts The Size
Set File = Fso.GetFile(ChkFile) : Rpt = Lb1 & File.Path & vbCrLf & Lb2
If File.Size < 1024 Then Rpt = Rpt & File.Size & " Bites" & vbCrLf End If
If File.Size > 1024 Then Rpt = Rpt & Left(File.Size/KB,5) & " KB" & vbCrLf End If
End Function
MsgBox Space(7) & "Found Files" & Rpt1 , 0 + 32 + 4096, "Report Files" '/-> Reports The Drive

VbsWorks.png

Link to comment
Share on other sites

@ECHO OFF
FOR %%? IN (C D E F G H I J K L M N O P Q R S T U V W X Y Z) DO (
CALL :SETVARS %%?:\NTLDR %%?:\boot.ini ||GOTO :ABORT)
:ABORT
PAUSE &GOTO :EOF
:SETVARS
IF EXIST %1 (IF EXIST %2 (
ECHO/&ECHO/%1 size is %~z1 bytes&ECHO/%2 size is %~z2 bytes&ECHO/&&EXIT/B 1))
EXIT/B 0

@ECHO OFF
FOR %%? IN (C D E F G H I J K L M N O P Q R S T U V W X Y Z) DO (
CALL :SETVARS %%?:\NTLDR %%?:\boot.ini ||GOTO :ABORT)
:ABORT
PAUSE &GOTO :EOF
:SETVARSIF EXIST %1 (IF EXIST %2 (
ECHO/&ECHO/%1 size is %~z1 bytes&ECHO/%2 size is %~z2 bytes&ECHO/&&EXIT/B 1))
EXIT/B 0

Look at line 6 and then tell me why you get the error 'The system canot find the batch label specified - SETVARS'
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...