Jump to content

Recommended Posts

Posted

I've created a batch (with included VB-script) to set the pagefile to a predefined size on the systemdrive and remove it from all other partitions.

The problem is that it only works sometimes and not always. What's wrong with it?

The reason for using a batch-file instead of just calling a VB-script is that if the pagefile happens to be placed on some other partition/drive, it will only resize it where it is.

Using only the pagefileconfig.vbs present in system32 will not change/delete the pagefile if it hasn't previously been set manually.

What the batch-file is supposed to do:

1. Check if the .vbs-file is already present to determine what to do next:

If the file is not present:

2. Copy itself from the current directory to %systemroot%.

3. Create a .vbs-file and run it.

4. Check whether or not the pagefile is already in the systemdrive and if so change the size of it.

5. If there is no pagefile in the systemdrive it will create it it and delete the other ones.

6. It will add itself to Runonce and reboot. It will then continue:

If the .vbs-file is already present (or has been created before the reboot):

2b. Delete the pagefile from all other partitions since it is no longer in use.

3b. Delete both the .vbs-file it created and itself.

The batch-file is named Vxl.cmd (vxl is a shortening of växlingsfil=pagefile in Swedish, like pgf or something in English):

cmdow @ /HID
@ECHO OFF&setlocal enableextensions enabledelayedexpansion
IF EXIST "%systemroot%\Vxl.vbs" GOTO :DE ELSE GOTO :DO

:DO
xcopy /s /q /h /k /y /r "%CD%\Vxl.cmd" "%systemroot%\"

ECHO strComputer = ".">>%systemroot%\Vxl.vbs
ECHO Set objWMIService = GetObject("winmgmts:" _>>%systemroot%\Vxl.vbs
ECHO ^& "{impersonationLevel=impersonate}!\\" ^& strComputer ^& "\root\cimv2")>>%systemroot%\Vxl.vbs
ECHO Set colPageFiles = objWMIService.ExecQuery _>>%systemroot%\Vxl.vbs
ECHO ("Select * from Win32_PageFileSetting")>>%systemroot%\Vxl.vbs
ECHO For Each objPageFile in colPageFiles>>%systemroot%\Vxl.vbs
ECHO objPageFile.InitialSize = 10>>%systemroot%\Vxl.vbs
ECHO objPageFile.MaximumSize = 10>>%systemroot%\Vxl.vbs
ECHO objPageFile.Put_>>%systemroot%\Vxl.vbs
ECHO Next>>%systemroot%\Vxl.vbs

cscript //B "%systemroot%\vxl.vbs"
IF EXIST "%systemdrive%\pagefile.sys" (
cscript //B "%systemroot%\system32\pagefileconfig.vbs" /Change /I 1028 /M 1028 /VO %systemdrive%
) ELSE (
cscript //B "%systemroot%\system32\pagefileconfig.vbs" /Create /I 1028 /M 1028 /VO %systemdrive% )
FOR %%i 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 IF NOT %%i: == %systemdrive% (
cscript //B "%systemroot%\system32\pagefileconfig.vbs" /Delete /VO %%i: )

REG ADD HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\RunOnce\ /V "Vxl" /D "%systemroot%\Vxl.cmd" /f

GOTO EOF

:DE
FOR %%i 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 IF NOT %%i: == %systemdrive% (
attrib -r -h -s -a "%%i:\pagefile.sys" & del /f /q "%%i:\pagefile.sys" )

del /f /q "%systemroot%\Vxl.vbs"
del /f /q "%systemroot%\Vxl.cmd"

GOTO EOF2

:EOF
endlocal
shutdown -r -t 0

:EOF2
endlocal
EXIT

(I could have used codebox tags on this, but I just couldn't figure out how to include blank lines.)


Posted (edited)

There doesn't appear to be anything particularly wrong with the file you posted, a couple of possible pitfalls at most. The main one being, this can potentially include unrequired spaces

  • %%i: == %systemdrive%

Try this slight modification, I have left the structure the same, let us know of any improvement or change etc.

@ECHO OFF
CMDOW @ /HID

IF EXIST %SYSTEMROOT%\%~n0.vbs GOTO DE

COPY %0 %SYSTEMROOT%>NUL

ECHO strComputer = ".">>%SYSTEMROOT%\%~n0.vbs
ECHO Set objWMIService = GetObject("winmgmts:" _>>%SYSTEMROOT%\%~n0.vbs
ECHO ^& "{impersonationLevel=impersonate}!\\" ^& strComputer ^& "\root\cimv2")>>%SYSTEMROOT%\%~n0.vbs
ECHO.>>%SYSTEMROOT%\%~n0.vbs
ECHO Set colPageFiles = objWMIService.ExecQuery _>>%SYSTEMROOT%\%~n0.vbs
ECHO ("Select * from Win32_PageFileSetting")>>%SYSTEMROOT%\%~n0.vbs
ECHO.>>%SYSTEMROOT%\%~n0.vbs
ECHO For Each objPageFile in colPageFiles>>%SYSTEMROOT%\%~n0.vbs
ECHO objPageFile.InitialSize = 10 >>%SYSTEMROOT%\%~n0.vbs
ECHO objPageFile.MaximumSize = 10 >>%SYSTEMROOT%\%~n0.vbs
ECHO objPageFile.Put_>>%SYSTEMROOT%\%~n0.vbs
ECHO Next>>%SYSTEMROOT%\%~n0.vbs

CSCRIPT //B //NOLOGO %SYSTEMROOT%\%~n0.vbs
IF EXIST %SYSTEMDRIVE%\pagefile.sys (
CSCRIPT //B //NOLOGO "%SYSTEMROOT%\system32\pagefileconfig.vbs" /Change /I 1028 /M 1028 /VO %SYSTEMDRIVE%
) ELSE (
CSCRIPT //B //NOLOGO "%SYSTEMROOT%\system32\pagefileconfig.vbs" /Create /I 1028 /M 1028 /VO %SYSTEMDRIVE%
)

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 (
IF "%%?:" NEQ "%SYSTEMDRIVE%" (
IF EXIST %%?:\pagefile.sys CSCRIPT //B //NOLOGO %SYSTEMROOT%\system32\pagefileconfig.vbs /Delete /VO %%?:
)
)

REG ADD HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\RunOnce /V %~n0 /D "%SYSTEMROOT%\%~nx0" /F

SHUTDOWN -r -f -t 10
EXIT

:DE
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 (
IF "%%?:" NEQ "%SYSTEMDRIVE%" (
IF EXIST %%?:\pagefile.sys DEL /A %%?:\pagefile.sys
)
)

DEL %SYSTEMROOT%\%~n0.vbs %0

<Edit>

You can play with the shutdown delay but I would suggest leaving the force switch in and reducing the seconds one increment at a time.

</Edit>

Edited by Yzöwl
Posted (edited)

Yzöwl, I've been waiting for your post. You are probably the batchfile-expert of this forum.

I'd like to learn more on the subject, do you know any good source for info?

Did a few minor changes to the file and tried again:

Replaced %CD% with . (why doesn't %CD% work?)

Added -f and changed the shutdown timeout from 0 to 1

If the size used in the vbs-script is too small, Windows will set the pagefile automatically. This is because it is smaller than the minimum usage. Setting a larger size (like around 70-120) will work, an even larger size can be set just to be on the safe side for different PC:s.

The problem seems to be something with this part:

IF EXIST "%systemdrive%\pagefile.sys" (
cscript //B //NOLOGO "%systemroot%\system32\pagefileconfig.vbs" /Change /I 1028 /M 1028 /VO %systemdrive%
) ELSE (
cscript //B //NOLOGO "%systemroot%\system32\pagefileconfig.vbs" /Create /I 1028 /M 1028 /VO %systemdrive% )

Since it does everything else it's supposed to do, except from setting the size as specified. Pagefileconfig.vbs is present.

It doesn't work if I run the first command (cscript...%systemdrive%) manually from the prompt (after setting it manually in the gui).

It has worked before, but it won't work now.

Edited by DL.
Posted
The problem seems to be something with this part:

IF EXIST "%systemdrive%\pagefile.sys" (
cscript //B //NOLOGO "%systemroot%\system32\pagefileconfig.vbs" /Change /I 1028 /M 1028 /VO %systemdrive%
) ELSE (
cscript //B //NOLOGO "%systemroot%\system32\pagefileconfig.vbs" /Create /I 1028 /M 1028 /VO %systemdrive% )

Other than changing the order of the options, I cannot see anything wrong in those lines
CSCRIPT %SYSTEMROOT%\%~n0.vbs //B //NOLOGO
IF EXIST %SYSTEMDRIVE%\pagefile.sys (
CSCRIPT "%SYSTEMROOT%\system32\pagefileconfig.vbs" //B //NOLOGO /Change /I 1028 /M 1028 /VO %SYSTEMDRIVE%
) ELSE (
CSCRIPT "%SYSTEMROOT%\system32\pagefileconfig.vbs" //B //NOLOGO /Create /I 1028 /M 1028 /VO %SYSTEMDRIVE%
)

Posted (edited)

Have you tried it on your own system?

The first part, using the created .vbs-file does work just fine. It is the part with pagefileconfig.vbs that doesn't work.

I made the batchfile a few months ago and it was working very well back then. I recently made a few very small changes in it, but nothing that could affect the functionality. To make sure those settings didn't do anything I tried by using the original file, with the same results.

After some more testing I'm not really sure that the problem is in the file.

I've tried it on a few different configurations (all XP Pro and recently installed) with different results:

In VMware:

nLited (ver 1.0rc1, removed only a few components) with my own tweaks - not working

nLited (ver 1.0rc1, removed some more components) without any tweaks - not working

Physical comps:

With my own tweaks. - not working

Standard installation - working

Do you have any idea on what setting and/or component that might be causing this problem?

I will take a look at my tweaks, but I'm not sure that I will find anything because I use a lot of tweaks (4000+ lines in a reg-file).

What confuses me is that it is not working with the nLited, but totally untweaked installation.

Update: Now I know that no service is causing this, tried on a clean install with only the services tweaked.

Edited by DL.
Posted

I found it!

Removing [HKEY_CLASSES_ROOT\CLSID\{7988B571-EC89-11cf-9C00-00AA00A14F56}] or "Disk and Profile Quota" in nLite will disable this functionality.

Posted

I'm glad you found it, I don't use nLite, therefore couldn't for the life of me work out the problem. I even set up a couple of installations, just to test it, and it seemed okay.

Posted

I removed that reg-key with my tweakfile because I thought that it didn't do anything else except removing quota management. To remove only the quota tab in explorer you have to remove the next key, 7988B573-... (and not 7988B571-...).

How did I figure it out?

At first I tried only the tweaks to disable services (on a clean install), but that wasn't it.

Then I targeted the section where I remove keys and searched the web for some info about each key (I don't keep that info in the file). Found the two most likely keys and tried them one at a time (after exporting them).

@Yzöwl, as I asked you earlier in this thread do you know any good source for more info on advanced batch-scripting?

Thanks for your help!

  • 3 months later...
Posted (edited)

Here Is a VB Script to accomplish this

'++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
' Name: ConfigurePageFile.vbs
' Comments: This script examines curent phyiscal RAM and will
' create a page file twice the size
'++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Option Explicit
'++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
' Variable Declaration
'++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

Dim strName, strInitialSize, strMaximumSize, objRtvPageFile, strMsg
Dim intRAM, int2XRAM
Dim objRAM, objSysSet, objItem
Dim valYN
Dim PageFileSz

'++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
' Main Body
'++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

' Puts Total MB of Physical RAM into intRAM variable
GetTotalRam()

' Puts Page Config into variables
PageFileConfig()

' Create Message
CreateMsg()

' Twice Physical RAM
int2XRAM = intRam * 2
strMsg = strMsg & vbcrlf & "Would you like to set the Page File to "_
& vbcrlf & "2 X Total Physical Memory = " & int2XRAM & "?" & vbcrlf & vbcrlf

valYN = MsgBox (strMsg,vbYesNo,"Current Pagefile Settings and Total RAM")

If valYN = 6 then
' Set current Page File to be two times RAM
setPageFileSz(int2XRAM)
strMsg = "Here are your new Page File settings:" & vbcrlf & vbcrlf
CreateMsg()
MsgBox strMsg,,"Current Pagefile Settings and Total RAM"
Else
WScript.echo "No Changes have been made."
End If

' Exit Script
WScript.Quit(0)

'++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
' Sub Procedures and Functions
'++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

' Configure pagefile
Function setPageFileSz (PageFileSz)
Dim objPageFile, objWMIService, colPageFiles

Set objWMIService = GetObject("winmgmts:" & "{impersonationLevel=impersonate}!\\.\root\cimv2")
Set colPageFiles = objWMIService.ExecQuery ("Select * from Win32_PageFileSetting")

For Each objPageFile in colPageFiles
objPageFile.InitialSize = PageFileSz
objPageFile.MaximumSize = PageFileSz
objPageFile.Put_
Next
End Function

' Retrieves Total Phyiscal RAM and Conerts to MB

Sub GetTotalRAM()
For Each objRAM in GetObject("winmgmts:\\.\root\CIMV2").ExecQuery("SELECT TotalPhysicalMemory FROM Win32_ComputerSystem")
intRAM = (objRAM.TotalPhysicalMemory / 1024) / 1024
intRAM = FormatNumber(intRAM, 0) + 1
Next
End Sub

' Retrieves Page File Name, Initial Size and Maximum Size into str variables
Sub PageFileConfig()
For Each objRtvPageFile in GetObject("winmgmts:\\.\root\CIMV2").ExecQuery("SELECT Name, InitialSize, MaximumSize FROM Win32_PageFileSetting",,48)
strName = objRtvPageFile.Name
strInitialSize = objRtvPageFile.InitialSize
strMaximumSize = objRtvPageFile.MaximumSize
Next
End Sub

' Put string together for message
Sub CreateMsg()
strMsg = strMsg & "Current Location:" & strName & vbcrlf
strMsg = strMsg & "InitialSize: " & strInitialSize & vbcrlf
strMsg = strMsg & "MaximumSize: " & strMaximumSize & vbcrlf
strMsg = strMsg & "Total Physical Memory: " & intRAM & vbcrlf
End Sub

Edited by S1LV3RF1$#

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