Jump to content











Photo
- - - - -

Batch file to find "real" Windows drive under Windows PE

windows pe 7 se

  • Please log in to reply
18 replies to this topic

#1 Rootman

Rootman

    Frequent Member

  • Advanced user
  • 243 posts
  • Location:USA

Posted 26 March 2013 - 03:16 PM

I've had a little utility named DeleteGUI in my arsenal ever since way back in the PartPE days.  It simply crawls the user and system folders looking for temp files and deletes them.  It was made for XP and has not been updated for Windows 7 so it finds very few files any more due to the DOCUMENTS AND SETTING vs USERS change.  I use it to clean up a system before I Ghost it. Its simpler and faster than most of the heavier duty cleaners lik CCleaner.

 

I came up with a simple script to do most of what DeleteGUI did but am having trouble with it under WIndows PE as the "real" system disk is seldomly drive C: under PE any more as there are often one or two other partitions on the drive for recovery etc. .

 

Anyone have a script or method to determine the actual drive that the full Windows resides on so I can plug it into my batch file?

 

BTW, here's the script, any help with it would be greatly appreciated.

 

@echo off
cd /D C:\Users
REM ?-Clean Temp Folder?
for /D %%a in (*.*) do DEL /F /S /Q "%%a\AppData\Local\Temp\*.*"
for /D %%a in (*.*) do FOR /D %%b IN ("%%a\AppData\Local\Temp\*.*") DO RMDIR /S /Q "%%b"
REM ?-Clean IE Cache?
for /D %%a in (*.*) do DEL /F /S /Q "%%a\AppData\Local\Microsoft\Windows\Temporary Internet Files\*.*"
for /D %%a in (*.*) do FOR /D %%b IN ("%%a\AppData\Local\Microsoft\Windows\Temporary Internet Files\*.*") DO RMDIR /S /Q "%%b"
DEL /F /S /Q "C:\temp\*.*"
RMDIR /S /Q "C:\temp\*.*"
DEL /F /S /Q "C:\Windows\temp\*.*"
RMDIR /S /Q "C:\Windows\temp\*.*"
 



#2 Wonko the Sane

Wonko the Sane

    The Finder

  • Advanced user
  • 13690 posts
  • Location:The Outside of the Asylum (gate is closed)
  •  
    Italy

Posted 26 March 2013 - 04:24 PM

Which OS are you running?

The switches (and actually the usage) of RD are seemingly UNlike the "standard" ones. :w00t:

 

http://ss64.com/nt/rd.html

 

As an example, in my day to get rid of C:\temp and of anything in it, you would have done:

 

 

RD /S /Q C:\temp
MD C:\temp

There is NOT really a way to determine safely a Windows install, safely in the sense that at the most you can list all the Windows installs (maybe), one could use the same kind of checks the Recovery Console does:

http://www.911cd.net...ndpost&p=141854

 

:cheers:

Wonko



#3 Rootman

Rootman

    Frequent Member

  • Advanced user
  • 243 posts
  • Location:USA

Posted 26 March 2013 - 06:24 PM

Trying to run this batch file in Windows PE 7 on machines that have Windows 7 on them, as I stated the old DeleteGUI works for XP because it's hardcoded to find temp files under the Document and Settings folders instead of the USERS subfolder for Vista and greater. Somehow it figures out where the "real" Windows installation is, either that or it just blindly hits all the disks deleting what it can.  I thing the DeleteGUI is an AutoIT exe and I don't have the source.

 

Not sure what you are referring to about the RD command, it is doing precisely what you cite in your example: "RMDIR /S /Q" using the long name for the RD internal command.  The stuff before it is finding all the %SYSTEMDRIVE%\USER's folders names and substituting with a parameter to hit all the users subfolders regardless of what they're called.  The script works fine as long as the "real" Windows OS is on C:, most PCs we are getting nowadays when you boot to Win PE 7 it shows up as D: or E: with a System Reserved partition and perhaps a manufacturers rescue partition occupying the first 2 partitions This makes the drive letter of the actual system D: or E: under PE.  I suppose I can brute force it and specify ALL the drive letters (C D E F), no real harm should happen because the USER folders are only on the "real" OS that should have these folders. 

 

Batch file scripting has always fascinated me and I have lost touch with a lot of it over the years.  I thought maybe someone already had a way to determine where the "real" OS sat and I could incorporate it into my script.

 

I can't determine what it is you are pointing to in the link you gave me about the Recovery Console, I can't seem to find anything pertinent there.



#4 Wonko the Sane

Wonko the Sane

    The Finder

  • Advanced user
  • 13690 posts
  • Location:The Outside of the Asylum (gate is closed)
  •  
    Italy

Posted 26 March 2013 - 06:47 PM

I was trying to say that you first 
 
 



DEL /F /S /Q "C:\temp\*.*"

which deletes each and every file in C:\temp and all it's subdirectories matching the mask *.*.
Then you:
 
 



RMDIR /S /Q "C:\temp"

that removes ANYTHING that is left, including the "relative root" directory C:\temp.
I am trying to tell you that running these two commands:
 
 



DEL /F /S /Q "C:\temp\*.*"
RMDIR /S /Q "C:\temp"

provides EXACTLY the same result of running ONLY:
 
 



RMDIR /S /Q "C:\temp"


The linked to post explains in detail what the Recovery Console does to list the Windows intstalls on disk:
http://www.911cd.net...54

 

Now, it seems like RC "detects" the existence of a XP install on HD by checking for the existence of BOTH:
a. a subdirectory \whatever\System32\config
b. a subdirectory \whatever\System32\drivers
then it connects to the hive SOFTWARE inside \whatever\System32\config\ and reads ONLY the said key in it.

in order to provide this selection:

0.jpg

 

:cheers:

Wonko



#5 Rootman

Rootman

    Frequent Member

  • Advanced user
  • 243 posts
  • Location:USA

Posted 26 March 2013 - 07:13 PM

Iwas trying to say that you first 

DEL /F /S /Q "C:\temp\*.*"

 

Hmm, I did that LAST because a great majority of our systems never get the TEMP variable set to C:\TEMP, they all use the (now) standard temp folder buried under their user folder ( AppData\Local\Temp\ ) - which is what the gobbledygook at the top of the script deletes, first files then folders that MAY be there. The SYSTEM now uses \Windows\Temp by default - which I also cover,  I just threw in the C:\Temp in there to cover my bases. 

 

I remember running the DeleteGUI on XP boxes and recovering GIGABYTES of disk space by deleting temp crap.  Saves a lot of time and room when Ghosting a bunch of PCs - or setting up a gold image to Ghost FROM.

 

All I really need help in is coming up with a - hopefully - elegant way of finding the drive letter to the real Windows system when booted to Win PE 7.



#6 Wonko the Sane

Wonko the Sane

    The Finder

  • Advanced user
  • 13690 posts
  • Location:The Outside of the Asylum (gate is closed)
  •  
    Italy

Posted 26 March 2013 - 08:06 PM

Well, if you want to do things (UNneededly) in two operations when one will suffice, you are very welcome to do it. :)
All I really need help in is coming up with a - hopefully - elegant way of finding the drive letter to the real Windows system when booted to Win PE 7.
You got one (the mentioned way to find the install that the recovery console uses is valid also on Vista :ph34r: and 7)
Do you want it in batch?
 
 
 





@ECHO OFF
SETLOCAL ENABLEDELAYEDEXPANSION
SET /A Counter=0
::List Drives
FOR /F "tokens=1 delims=\ " %%? IN ('MOUNTVOL.EXE ^|FIND ":\"') DO (
IF EXIST %%?\NUL CALL :find_dir %%?
)
SET FoundWinDir
GOTO :EOF

:find_dir
ECHO Looking for directories on drive %1 ....
FOR /F %%A IN ('DIR "%1\" /AD /B 2^>nul') DO (
FOR /F %%B IN ('DIR "%1\%%A\" /AD /B 2^>nul ^|FIND /I "System32" ') DO (
FOR /F %%C IN ('DIR "%1\%%A\%%B" /AD /B 2^>nul ^|FIND /I "config"') DO (

IF EXIST "%1\%%A\%%B\drivers" (
SET FoundWinDir_!Counter!=%1\%%A\
SET FoundWinDir_!Counter!
SET /A Counter+=1
)
)
)
)
The above might do BUT it will "choke" on machines on which there is one of those CF/SD/Multi card readers.
 
:cheers:
Wonko

#7 Rootman

Rootman

    Frequent Member

  • Advanced user
  • 243 posts
  • Location:USA

Posted 26 March 2013 - 08:30 PM

Well that might do it if I test for something other than \Windows\System32\config and Drivers because that also returns X: as the system drive because PEs X:\Windows\Drivers exists too.  That's a pretty interesting script.  Thanks!



#8 Wonko the Sane

Wonko the Sane

    The Finder

  • Advanced user
  • 13690 posts
  • Location:The Outside of the Asylum (gate is closed)
  •  
    Italy

Posted 26 March 2013 - 08:44 PM

Well that might do it if I test for something other than \Windows\System32\config and Drivers because that also returns X: as the system drive because PEs X:\Windows\Drivers exists too.  That's a pretty interesting script.  Thanks!

Well, you can exclude X: (knowing that your PE is "default" and gets "X:" by design)
 

 

IF NOT %%?\==X:\ IF EXIST %%?\NUL CALL :find_dir %%?

:cheers:
Wonko



#9 Rootman

Rootman

    Frequent Member

  • Advanced user
  • 243 posts
  • Location:USA

Posted 26 March 2013 - 11:43 PM

Actually I just changed the batch file to search for \Users\Administrator\AppData\Local folder which SHOULD exist only on a real Windows 7 drive.

 

 I'll just tack it on the beginning of my script and use the resultant variable. Thanks, that will do the trick.



#10 devdevadev

devdevadev

    Frequent Member

  • Advanced user
  • 406 posts
  •  
    India

Posted 16 July 2017 - 12:23 PM

How I can get Drive Letter of offline 'Windows OS Partition' and OS version of the offline 'Windows OS'  within from WinPE using batch file ? 

 

Should I use

FOR %%w IN (C D E F G H I J K L N M O P Q R S T U V W X Y Z) DO IF EXIST %%w:\Users\Public\ set SYSPART=%%w:
if not exist %SYSPART%\Public echo Could not find Windows OS Partition & pause & exit

Regards..



#11 Wonko the Sane

Wonko the Sane

    The Finder

  • Advanced user
  • 13690 posts
  • Location:The Outside of the Asylum (gate is closed)
  •  
    Italy

Posted 16 July 2017 - 12:52 PM

How I can get Drive Letter of offline 'Windows OS Partition' and OS version of the offline 'Windows OS'  within from WinPE using batch file ? 

 

Should I use



FOR %%w IN (C D E F G H I J K L N M O P Q R S T U V W X Y Z) DO IF EXIST %%w:\Users\Public\ set SYSPART=%%w:
if not exist %SYSPART%\Public echo Could not find Windows OS Partition & pause & exit

Regards..

Maybe you could READ what has been already posted. :whistling:

 

What will your two lines do if more than a Windows installations (actually more than one drive letter containing "\Users\Public" - which is not "\Public" - BTW) exist? :dubbio:

 

:duff:

Wonko



#12 devdevadev

devdevadev

    Frequent Member

  • Advanced user
  • 406 posts
  •  
    India

Posted 16 July 2017 - 01:03 PM

FOR %%w IN (C D E F G H I J K L N M O P Q R S T U V W Y Z) DO IF EXIST %%w:\Windows\System32\ set SYSPART=%%w:
if not exist %SYSPART%\Windows\System32 echo Could not find Windows OS Partition & pause & exit
 
 
May be I should use different variables for each Windows OS Partition found in above loop ? Cascaded loop ?
 
SYSPART1 for First OS found, SYSPART2 for second OS found and so on ??
 
Can you please provide me cascaded codes that will assign Drive Letter for 'SYSPART1' and 'SYSPART2' ?
 
I also want to set 'OSVER1' and 'OSVER2'.....etc for each OS found...
 
Please help..


#13 Wonko the Sane

Wonko the Sane

    The Finder

  • Advanced user
  • 13690 posts
  • Location:The Outside of the Asylum (gate is closed)
  •  
    Italy

Posted 16 July 2017 - 01:54 PM

Can you please provide me cascaded codes that will assign Drive Letter for 'SYSPART1' and 'SYSPART2' ?

 
There is ALREADY posted a small batch that does EXACTLY that.  :ranting2: 
(Ok, not "exactly" it sets FoundWinDir_1, FoundWinDir_2, etc., but the idea is that one uses the example and then adapts it to his/her needs)
 

I also want to set 'OSVER1' and 'OSVER2'.....etc for each OS found...

I also want a nice, hot, cup of coffee (black, 2 spoons of sugar, please).

 

:duff:

Wonko


  • devdevadev likes this

#14 devdevadev

devdevadev

    Frequent Member

  • Advanced user
  • 406 posts
  •  
    India

Posted 16 July 2017 - 03:07 PM

May be following script will do my work. 
@echo off
set SIG=sigcheck.exe
if exist "X:\Program files (x86)" set SIG=sigcheck64.exe

set SYSPART=
set SYSPART1=
set OSVER=
set OSVER1=
FOR %%w IN (C D E F G H I J K L N M O P Q R S T U V W Y Z) DO (
if not defined SYSPART IF EXIST %%w:\Windows\explorer.exe set SYSPART=%%w:
if defined SYSPART IF EXIST %%w:\Windows\explorer.exe set SYSPART1=%%w:
)

if defined SYSPART  call :getver %SYSPART%\WIndows\explorer.exe %SYSPART%
if defined SYSPART1 call :getver %SYSPART1%\WIndows\explorer.exe %SYSPART1%

REM show variables
set OSVER
set SYSPART
goto :EOF

:getver
set WIN=
%SIG% /accepteula %1 | find "10.0"   > nul && set WIN=10
%SIG% /accepteula %1 | find "6.1."   > nul && set WIN=7
%SIG% /accepteula %1 | find "6.2.7"  > nul && set WIN=7
%SIG% /accepteula %1 | find "6.2.92" > nul && set WIN=8
%SIG% /accepteula %1 | find "6.3.96" > nul && set WIN=8.1
if defined OSVER set OSVER1=%WIN%
if not defined OSVER set OSVER=%WIN%
echo Found %WIN% on %2
goto :EOF

But your example is very cool but a bit tricky !!! Do you know about any way by which we can detect OS version of 'Windows Partition' within from WinPE ? In above code we have used 'sigcheck.exe'. Can you provide a more better and small way ?



#15 Wonko the Sane

Wonko the Sane

    The Finder

  • Advanced user
  • 13690 posts
  • Location:The Outside of the Asylum (gate is closed)
  •  
    Italy

Posted 16 July 2017 - 03:33 PM

 

May be following script will do my work. 

 

May be.

 

Interesting way how you now limited the discovery to two OS instances.

 

Of course nothing of the (rest assured carefully selected) files to check made way into your batch. 

 

It is really depressing, actually more than depressing, frustrating. :(

 

WMIC way (example):

wmic datafile where name='c:\\windows\\system32\\notepad.exe' get version 

VBS:

Set objFSO = CreateObject("Scripting.FileSystemObject")
 file = "C:\windows\system32\notepad.exe"
Wscript.Echo objFSO.GetFileVersion(file)

:duff:

Wonko



#16 devdevadev

devdevadev

    Frequent Member

  • Advanced user
  • 406 posts
  •  
    India

Posted 16 July 2017 - 03:43 PM

Does WMIC and VBS really work within vanilla WinPE Environment (/sources/boot.wim) ?



#17 Wonko the Sane

Wonko the Sane

    The Finder

  • Advanced user
  • 13690 posts
  • Location:The Outside of the Asylum (gate is closed)
  •  
    Italy

Posted 16 July 2017 - 08:24 PM

You want an exe?

https://www.codeproj...O-display-progr

 

You want to backport this to batch?

http://reboot.pro/to...nd-dll-version/

Use gsar or Swissknife for the hex finding.

 

:duff:

Wonko



#18 devdevadev

devdevadev

    Frequent Member

  • Advanced user
  • 406 posts
  •  
    India

Posted 16 July 2017 - 08:53 PM

Where I am doing wrong in following batch file ?

@ECHO OFF
SETLOCAL ENABLEDELAYEDEXPANSION
SET USERDIR=
FOR /F %%A IN ('DIR "C:\Users\" /AD /B 2^>nul') DO (
if /i not "%%A"=="Default" FOR /F %%B IN ('DIR "C:\Users\%%A\" /AD /B 2^>nul ^|FIND /I "Desktop" ') DO (

IF EXIST "C:\Users\%%A\Desktop" (
SET USERDIR=C:\Users\%%A
echo. %USERDIR%
)
)
)

robocopy "%USERDIR%\Music\*.*" "K:\BACKUP\Music\*.*" /E /MT:1
robocopy "%USERDIR%\Videos\*.*" "K:\BACKUP\Videos\*.*" /E /MT:1
robocopy "%USERDIR%\Desktop\*.*" "K:\BACKUP\Desktop\*.*" /E /MT:1
robocopy "%USERDIR%\Pictures\*.*" "K:\BACKUP\Pictues\*.*" /E /MT:1
robocopy "%USERDIR%\Documents\*.*" "K:\BACKUP\Documents\*.*" /E /MT:1
robocopy "%USERDIR%\Downloads\*.*" "K:\BACKUP\Downloads\*.*" /E /MT:1

PAUSE
exit


#19 Wonko the Sane

Wonko the Sane

    The Finder

  • Advanced user
  • 13690 posts
  • Location:The Outside of the Asylum (gate is closed)
  •  
    Italy

Posted 16 July 2017 - 09:16 PM

Post what you expected that batch to do and what instead it does when run.

 

Also some description of the problem that it is supposed to solve would help.

 

And as always, you are mistaking this thread (that you already partially hijacked) for one titled:

"Hi, I'm devdevadev, please help me with whatever crosses my mind that I won't properly describe, and that I want to solve my own way withou tistening to any suggesiton you will provide" 

 

I will repeat to you (but it is a common enough BAD habit of several members, you are not the only one) how - roughly - a board works well by having threads with meaningful titles that respect the contents discussed within and viceversa having discussed contents related to the thread title.

A thread has (should have) a specific topic, the idea of "Please help me with this, 

 

Start a NEW thread (meaningful titled)

Describe the problem/issue that you are trying to solve (the general goal, not only the tiny fraction of it that you are having issues at the moment).

Describe how your attempted solution doesn't work, what is the error message (if any), how it behaves differently from what you expected, what you expected, etc.,etc.

 

Anyway:

...

SETLOCAL ENABLEDELAYEDEXPANSION

...

echo. %USERDIR%

 

 

 

:dubbio:

 

 

:duff:

Wonko


  • devdevadev likes this




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users