Jump to content











Photo
- - - - -

Please help me to go ahead ?

cmd winpe boot.wim usb booting multibooting

  • Please log in to reply
146 replies to this topic

#1 devdevadev

devdevadev

    Frequent Member

  • Advanced user
  • 406 posts
  •  
    India

Posted 17 July 2017 - 02:49 PM

Hello Everyone..

Thanks 'Wonko' for your great help and support....

DIRUSE /M "C:\Users\Kdeva\Music" "C:\Users\Kdeva\Videos" "C:\Users\Kdeva\Desktop" "C:\Users\Kdeva\Documents" "C:\Users\Kdeva\Downloads" > myvol.log
pause

I have executed above command and It gives following result in form of 'myvol.log' file-

    Size (mb)  Files  Directory
        17.75     21  SUB-TOTAL: C:\USERS\KDEVA\MUSIC
         1.01     29  SUB-TOTAL: C:\USERS\KDEVA\VIDEOS
       245.22    304  SUB-TOTAL: C:\USERS\KDEVA\DESKTOP
         0.09      2  SUB-TOTAL: C:\USERS\KDEVA\DOCUMENTS
     14420.99  14103  SUB-TOTAL: C:\USERS\KDEVA\DOWNLOADS
     14685.06  14459  TOTAL

Now I want to use a variable 'BACKUP_SIZE' that will hold Total size of all folders. .i.e BACKUP_SIZE=14685 MB

 

So how I can set Total Size value to 'BACKUP_SIZE'  ?

 

May be I should use something like this ?

for /F "usebackq tokens=?" %%A in (`find " TOTAL " myvol.log`) do set BACKUP_SIZE=%%A
set BACKUP_SIZE=%BACKUP_SIZE:~x,-y%
set /a BACKUP_SIZE=BACKUP_SIZE+10
echo BACKUP Folder is %BACKUP_SIZE%MB
pause

Any better .cmd way to calculate Total sizes of all above folders ?

 

What value I should use in place of Red Highlighted values ?

I don't understand how you guys read a value within from .txt or .log file ? 

Please provide me some good pages so that I can learn how to get a value within from .txt files ?

 

Thanks & Regards...



#2 Wonko the Sane

Wonko the Sane

    The Finder

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

Posted 17 July 2017 - 03:30 PM

Well you don't actually need to write to a file (unless you want to).

 

FOR /F "tokens=1 delims=. " %%A in ('DIRUSE /M "C:\Users\Kdeva\Music" "C:\Users\Kdeva\Videos" "C:\Users\Kdeva\Desktop" "C:\Users\Kdeva\Documents" "C:\Users\Kdeva\Downloads"') DO SET BACKUP_SIZE=%%A

SET BACKUP_SIZE

 

should do nicely, using  the . (dot) and space as delimiters should give you 14685 (without decimal part, i.e. suitable to be used in a SET /A command).

 

Anyway, it is usually a good idea to use the TYPE command in a FOR loop to read such a .log file:

FOR /F "tokens=1 delims=. " %%A in ('TYPE myvol.log') DO SET BACKUP_SIZE=%%A

SET BACKUP_SIZE

 

In both the above there is no need to "find" anything as the BACKUP_SIZE will be given the value of each line first token, and since the value you are looking for is the last one of the output, it will be the one that "remains".

 

Usual references:

http://www.robvanderwoude.com/for.php

http://www.robvander...e.com/ntfor.php

 

:duff:

Wonko


  • devdevadev likes this

#3 devdevadev

devdevadev

    Frequent Member

  • Advanced user
  • 406 posts
  •  
    India

Posted 17 July 2017 - 03:57 PM

Thanks for the great explanation. Now I am getting some idea of syntax  used in cmd and it's uses..

 

Now I want to get Drive Letter of a partition having FREE SPACE greater than %BACKUP_SIZE%. i will first look for a partition of Internal Harddisk having Free Space greater than %BACKUP_SIZE%. if no partition of Internal Harddisk have Free Space greater thank %BACKUP_SIZE%, Then I will look for a partition of Removable USB Drives having Free Space greater than %BACKUP_SIZE%. If no partition having Free Space greater than %BACKUP_SIZE%, then It will just open Windows Explorer for Manual Backup...!!!

 

To achieve  above GOAL, Which tool will be best for listing FREE SPACE of partitions ? 



#4 Wonko the Sane

Wonko the Sane

    The Finder

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

Posted 17 July 2017 - 04:45 PM

Like? :unsure:

https://ss64.com/nt/diskuse.html

https://ss64.com/nt/freedisk.html

 

The "strategy" is however "perplexing".  :dubbio:

 

Automating the decision on WHERE to store a backup.

 

What could possibly go wrong? :unsure:

 

Also - usually - backups are stored in a compressed form.

 

:duff:

Wonko



#5 IAmTheTrueMeaningOfCovfefe

IAmTheTrueMeaningOfCovfefe

    Silver Member

  • Advanced user
  • 555 posts
  • Location:In hiding
  • Interests:An investigation is underway to determine whether Trump has any ties to America.
  •  
    United States

Posted 17 July 2017 - 08:52 PM

@devdevadev: You're cleared to go ahead.



#6 devdevadev

devdevadev

    Frequent Member

  • Advanced user
  • 406 posts
  •  
    India

Posted 18 July 2017 - 01:36 AM

Mostly when I format and re-install Windows in any System then I copy OLD 'Desktop', 'Music', 'Pictures', 'Documents', and 'Download' folder from SYSPART to any other partition within from WinPE environment. I am using 'SMART_AUTO.cmd' in PTN2 of my Removable USB Drive which I call within from 'boot_%BIT%' of Win 10 1703 (can access all hidden partitions of Removable USB Drive). 'SMART_AUTO.cmd' allow me to do many things. As my vision (RP) is going worse . So I want to improve my 'SMART_AUTO.cmd' so that I can do different action just by entering numbers 1,2,3...etc...instead of seeing white screen of File Explorer in WinPE environment. Here I am not backup up Windows. I just want to copy these 5 folders to any safe place for future uses. 

 

Where I can download 'FREEDISK' and 'DISKUSE' tools. Are these tools also compatible with Win 10 'boot.wim' environment ?

 

It's looking I should check free spaces of all Volumes (Volume 0 to Volume x) if a volume have free space greater than %BACKUP_SIZE%, then set it's Drive Letter to 'FREEVOL' variable. How I can get this in one cmd code ?

 

Is there any tool that will show free spaces of volumes in MB  ? DISKPART LIST VOLUME show only Total size *(both in MB and GB ??) but not free spaces.



#7 Atari800XL

Atari800XL

    Member

  • Members
  • 74 posts
  •  
    Netherlands

Posted 18 July 2017 - 07:09 AM

I'm sure Rex Conn's excellent Take Command/ TCC/ TCCLE has something for this. I'm sure you've heard of 4DOS etc, TCC just has so much more power than plain old CMD.EXE (and is much easier to learn, and faster to start, also in PE).

 

Maybe, if you have time to spare (and I think you have) you could check out the "runtime" version of TCC, this one's even free of charge!

 

Check here:

https://jpsoft.com/f...953/#post-43931



#8 Wonko the Sane

Wonko the Sane

    The Finder

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

Posted 18 July 2017 - 08:37 AM

The mentioned tools are (see the top of the given pages) are either in the 2003 Resource kit or in Server OS's.

BUT:

fsutil volume diskfree c:

 

OR

 

wmic LogicalDisk Where DriveType="3" Get DeviceID,FreeSpace

 

would both do.

 

 

Or you can use a simple DIR:

DIR C:\ /-O /W

or

DIR C:\ /-C /-O /W

the last line will contain available bytes, so it can be used in batch *like*:

 

FOR /F "tokens=3" %A IN ('DIR C:\ /-C /-O /W') DO SET FREE_SPACE=%%A
)
ECHO FREE_SPACE is %FREE_SPACE%

 

Of course you will need to do some formatting of the string as the data is in bytes.

 

OR try using the "divide.cmd" here :unsure::
http://reboot.pro/to...deccmd/?p=22754

 

:duff:

Wonko



#9 devdevadev

devdevadev

    Frequent Member

  • Advanced user
  • 406 posts
  •  
    India

Posted 18 July 2017 - 11:17 PM

I was looking for LOGIC something like this ? 

@echo off
pushd "%~dp0"
setlocal EnableDelayedExpansion

call :BACKUP_PREP
if exist %FREEDRIVE% SET FREEDRIVE
PAUSE
if exist %FREEDRIVE% call :DO_BACKUP
if not exist FREEDRIVE explorer 
echo SMART BACKUP HAS BEEN DONE !!!
PAUSE


:BACKUP_PREP
SET USERDIR=
FOR /F %%A IN ('DIR "%SYSPART%\Users\" /AD /B 2^>nul') DO (
if /i not "%%A"=="Public" FOR /F %%B IN ('DIR "%SYSPART%\Users\%%A\" /AD /B 2^>nul ^|FIND /I "Desktop" ') DO (
IF EXIST "%SYSPART%\Users\%%A\Desktop" (
SET USERDIR=%SYSPART%\Users\%%A
)
)
)
SET USERDIR
FOR /F "tokens=1 delims=. " %%A in ('DIRUSE /M "%USERDIR%\Music" "%USERDIR%\Videos" "%USERDIR%\Desktop" "%USERDIR%\Documents" "%USERDIR%\Downloads"') DO SET BACKUP_SIZE=%%A
SET BACKUP_SIZE
pause

for /F "tokens=2-16 delims=:\ " %%A in ('fsutil fsinfo drives') do (
  for %%d in (%%A %%B %%C %%D %%E %%F %%G %%H %%I %%J %%K %%L %%M %%N %%O) do (
    for /F "tokens=2 delims=:" %%f in ('fsutil volume diskfree %%d: ^| find "of free bytes" ') do @(
      set mb=%%f
      set mb=!mb:~,-3!
      set /A mb /= 1024 >nul
      echo %%d: !mb!
      if !mb! GTR %BACKUP_SIZE% set FREEDRIVE=%%d:& goto :EOF
    )
  )
)


:DO_BACKUP
robocopy "%USERDIR%\Music" "%FREEDRIVE%\BACKUP\Music" /E /MT:1
robocopy "%USERDIR%\Videos" "%FREEDRIVE%\BACKUP\Videos" /E /MT:1
robocopy "%USERDIR%\Desktop" "%FREEDRIVE%\BACKUP\Desktop" /E /MT:1
robocopy "%USERDIR%\Pictures" "%FREEDRIVE%\BACKUP\Pictues" /E /MT:1
robocopy "%USERDIR%\Documents" "%FREEDRIVE%\BACKUP\Documents" /E /MT:1
robocopy "%USERDIR%\Downloads" "%FREEDRIVE%\BACKUP\Downloads" /E /MT:1
goto :EOF

But above code does not seems to work properly  if !mb! is less than %BACKUP_SIZE% ?

i am unable to find out where I am doing wrong ? May be Last LOOP have some wrong LOGIC ?

 

Can anybody please help me to work it out ?

 

Thanks & Regards..



#10 Wonko the Sane

Wonko the Sane

    The Finder

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

Posted 19 July 2017 - 12:25 PM

Personally I would write:

if NOT !mb! GTR %BACKUP_SIZE%

as:

if !mb! LSS %BACKUP_SIZE%

or - maybe - as:

if !mb! LEQ %BACKUP_SIZE%

But also :

if  %BACKUP_SIZE% GTR  !mb!

might do.

 

But is this the issue? :unsure:

 

Both "main" and each subroutine MUST end with an unconditional  GOTO :EOF

 

What is :w00t::

if not exist FREEDRIVE explorer 

 

 

:duff:

Wonko



#11 devdevadev

devdevadev

    Frequent Member

  • Advanced user
  • 406 posts
  •  
    India

Posted 19 July 2017 - 07:05 PM

This seems working as expected.

@echo off
pushd "%~dp0"
setlocal EnableDelayedExpansion

call :BACKUP_PREP
if not "%FREEDRIVE%"=="" mkdir "%FREEDRIVE%\BACKUP" & Explorer.exe "%FREEDRIVE%\BACKUP" & call :DO_BACKUP
if "%FREEDRIVE%"=="" Explorer ,
echo SMART BACKUP HAS BEEN DONE !!!
PAUSE
exit

:BACKUP_PREP
SET USERDIR=
SET BACKUP_SIZE=
SET FREEDRIVE=
FOR /F %%A IN ('DIR "%SYSPART%\Users\" /AD /B 2^>nul') DO (
if /i not "%%A"=="Public" FOR /F %%B IN ('DIR "%SYSPART%\Users\%%A\" /AD /B 2^>nul ^|FIND /I "Desktop" ') DO (
IF EXIST "%SYSPART%\Users\%%A\Desktop" (
SET USERDIR=%SYSPART%\Users\%%A
)
)
)
FOR /F "tokens=1 delims=. " %%A in ('DIRUSE /M "%USERDIR%\Music" "%USERDIR%\Videos" "%USERDIR%\Desktop" "%USERDIR%\Documents" "%USERDIR%\Downloads"') DO SET BACKUP_SIZE=%%A

FOR /F "tokens=2-16 delims=:\ " %%A in ('fsutil fsinfo drives') do (
  FOR %%d in (%%A %%B %%C %%D %%E %%F %%G %%H %%I %%J %%K %%L %%M %%N %%O) do (
    FOR /F "tokens=2 delims=:" %%f in ('fsutil volume diskfree %%d: ^| find "of free bytes" ') do @(
      set mb=%%f
      set mb=!mb:~,-3!
      set /A mb /= 1024 >nul
      echo %%d: !mb!
      if !mb! GTR %BACKUP_SIZE% set FREEDRIVE=%%d:&goto :EOF
    )
  )
goto :EOF
)

:DO_BACKUP
robocopy "%USERDIR%\Music" "%FREEDRIVE%\BACKUP\Music" /E /MT:1
robocopy "%USERDIR%\Videos" "%FREEDRIVE%\BACKUP\Videos" /E /MT:1
robocopy "%USERDIR%\Desktop" "%FREEDRIVE%\BACKUP\Desktop" /E /MT:1
robocopy "%USERDIR%\Pictures" "%FREEDRIVE%\BACKUP\Pictues" /E /MT:1
robocopy "%USERDIR%\Documents" "%FREEDRIVE%\BACKUP\Documents" /E /MT:1
robocopy "%USERDIR%\Downloads" "%FREEDRIVE%\BACKUP\Downloads" /E /MT:1
goto :EOF

Am I still using something improper or Invalid syntax ? Any improvements ?



#12 Wonko the Sane

Wonko the Sane

    The Finder

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

Posted 20 July 2017 - 08:08 AM

I doubt that it actually works (you have the GOTO :EOF of :BACKUP_PREP inside a FOR loop).

 

There is in practice NO error control of any kind, if you are happy with it, by all means go ahead, but given it is something that can - at least in theory - overfill a volume or however copy large amounts of data (presumably important) before throwing a fit (for *whatever* reasons) AND - potentially - overwrite previous backups I would not trust it at all.

 

If (when) it works, the batch first backups those folders to an automatically (random?) selected disk, then tells you that it succeeded (maybe) or "nothing" if it failed, not exactly something I would like to run.

 

This is only "style", but in batch there is the ELSE, so you normally write:

IF condition=true do_this ELSE do_that

as opposed to:

IF condition=false do_that

IF condition=true do_this

 

:duff:

Wonko



#13 paraglider

paraglider

    Gold Member

  • .script developer
  • 1716 posts
  • Location:NC,USA
  •  
    United States

Posted 20 July 2017 - 12:08 PM

You assume there is only one user which is not Public with  desktop folder. That is not true in my case.

 

Also profile folders may not be in their default location and in none english os will have different names as well.

 

Think you should back up all users with desktop folder and include the user name in the backup folder structure.

 

I think

 

if !mb! GTR %BACKUP_SIZE% set FREEDRIVE=%%d:&goto :EOF

 

should be:

 

if !mb! GTR %BACKUP_SIZE% set FREEDRIVE=%%d:&goto :DO_BACKUP


  • devdevadev likes this

#14 Wonko the Sane

Wonko the Sane

    The Finder

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

Posted 20 July 2017 - 02:52 PM

Either of these:

@ECHO OFF
SETLOCAL

FOR /F "tokens=1,2 delims=\ " %%A in ('fsutil fsinfo drives^|find /v""') do CALL :Parse_drives %%A %%B
SET ALLDrives
GOTO :EOF


:Parse_drives
IF NOT %2.==. SHIFT
SET AllDrives=%Alldrives% %1
GOTO :EOF
@ECHO OFF
SETLOCAL EnableDelayedExpansion
FOR /F "delims=" %%A in ('fsutil fsinfo drives^|find /v""') do (
SET ThisDrive=%%A
SET AllDrives=!Alldrives! !ThisDrive:~-3,2!
)

SET ALLDrives

will list all drives (unless the original code limited to variables A-O, i.e. 15 drives.

 

On newer OS (on XP it throws a warning and a Continue/Retry/Cancel popup when it finds CD's and Card Readers, i.e. "drives" WITHOUT media inserted) this might even work:

@ECHO OFF
SETLOCAL EnableDelayedExpansion
FOR /F "delims=" %%A in ('fsutil fsinfo drives^|find /v""') do SET AllDrives=!Alldrives! %%~dA

SET ALLDrives

:duff:

Wonko



#15 devdevadev

devdevadev

    Frequent Member

  • Advanced user
  • 406 posts
  •  
    India

Posted 21 July 2017 - 03:00 AM

Think you should back up all users with desktop folder and include the user name in the backup folder structure.

 

I think

 

if !mb! GTR %BACKUP_SIZE% set FREEDRIVE=%%d:&goto :EOF

 

should be:

 

if !mb! GTR %BACKUP_SIZE% set FREEDRIVE=%%d:&goto :DO_BACKUP

Thanks for the necessary suggestions...

@echo on
pushd "%~dp0"
setlocal EnableDelayedExpansion

call :BACKUP_PREP
if "%FREEDRIVE%"=="" Explorer ,
echo SMART BACKUP HAS BEEN DONE !!!
PAUSE
exit


:BACKUP_PREP
SET USER=
SET USERDIR=
FOR /F %%A IN ('DIR "C:\Users\" /B 2^>nul') DO (
if /i not "%%A"=="Public" FOR /F %%B IN ('DIR "C:\Users\%%A\" /B 2^>nul ^|FIND /I "Desktop" ') DO (
IF EXIST "C:\Users\%%A\Desktop" (
SET USER=%%A& SET USERDIR=C:\Users\%%A
call :BACKUP_SIZE

FOR /F "tokens=2-16 delims=:\ " %%A in ('fsutil fsinfo drives') do (
  FOR %%d in (%%A %%B %%C %%D %%E %%F %%G %%H %%I %%J %%K %%L %%M %%N %%O) do (
    FOR /F "tokens=2 delims=:" %%f in ('fsutil volume diskfree %%d: ^| find "of free bytes" ') do @(
      set mb=%%f
      set mb=!mb:~,-3!
      set /A mb /= 1024 >nul
      echo %%d: !mb!
      SET BACKUP_SIZE
      if !mb! GTR %BACKUP_SIZE% set FREEDRIVE=%%d:& call :DO_BACKUP
    )
  )
)
    )
  )
)

:BACKUP_SIZE
FOR /F "tokens=1 delims=. " %%A in ('DIRUSE /M "%USERDIR%\Music" "%USERDIR%\Videos" "%USERDIR%\Desktop" "%USERDIR%\Documents" "%USERDIR%\Downloads"') DO SET BACKUP_SIZE=%%A
goto :EOF

:DO_BACKUP
mkdir "%FREEDRIVE%\BACKUP_%USER%_%DATE:~7,2%-%DATE:~4,2%-%DATE:~-4%" & Explorer.exe "%FREEDRIVE%\BACKUP_%USER%_%DATE:~7,2%-%DATE:~4,2%-%DATE:~-4%"
robocopy "%USERDIR%\Music" "%FREEDRIVE%\BACKUP_%USER%_%DATE:~7,2%-%DATE:~4,2%-%DATE:~-4%\Music" /E /MT:1
robocopy "%USERDIR%\Videos" "%FREEDRIVE%\BACKUP_%USER%_%DATE:~7,2%-%DATE:~4,2%-%DATE:~-4%\Videos" /E /MT:1
robocopy "%USERDIR%\Desktop" "%FREEDRIVE%\BACKUP_%USER%_%DATE:~7,2%-%DATE:~4,2%-%DATE:~-4%\Desktop" /E /MT:1
robocopy "%USERDIR%\Pictures" "%FREEDRIVE%\BACKUP_%USER%_%DATE:~7,2%-%DATE:~4,2%-%DATE:~-4%\Pictures" /E /MT:1
robocopy "%USERDIR%\Documents" "%FREEDRIVE%\BACKUP_%USER%_%DATE:~7,2%-%DATE:~4,2%-%DATE:~-4%\Documents" /E /MT:1
robocopy "%USERDIR%\Downloads" "%FREEDRIVE%\BACKUP_%USER%_%DATE:~7,2%-%DATE:~4,2%-%DATE:~-4%\Downloads" /E /MT:1
goto :EOF

Why following line does not utilize value of %BACKUP_SIZE% in above codes ?

if !mb! GTR %BACKUP_SIZE% set FREEDRIVE=%%d:& call :DO_BACKUP

Other things looking working except it ? Where I am doing WRONG ?

 

Regards..



#16 Wonko the Sane

Wonko the Sane

    The Finder

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

Posted 21 July 2017 - 08:31 AM

Do you really expect that someone will run your batch to find why it doesn't work?

 

Your batch is too convoluted .

 

Make it simpler.

 

You have 3 actions to perform:

1) determine the path and size to be backed up -> this will output "Backup_size"

2) determine which (first? :w00t:) volume that has enough free space to receive the backup -> this will output the target volume, i.e. "FreeDrive"

3) do the backup.

 

This can be written more easily as three separate, self-standing batches.

 

Do that.

 

When each of the three batches works, then (and only then) you can consolidate them in a single batch.

 

The issue you are having is likely connected with the delayed expansion, you have 6 levels of brackets/FOR loops.

 

Simplify.

 

Put some PAUSE's displaying the values



echo %%d: !mb!
SET mb
ECHO BackupSize %BACKUP_SIZE%
SET BACKUPSIZE
ECHO if !mb! GTR %BACKUP_SIZE% set FREEDRIVE=%%d:
PAUSE

:duff:

Wonko



#17 paraglider

paraglider

    Gold Member

  • .script developer
  • 1716 posts
  • Location:NC,USA
  •  
    United States

Posted 21 July 2017 - 12:39 PM

There are also many other scripting languages you could use which would make your job easier once you learn the language - autoit / vbscript to name 2. Powershell is also a very powerful language but does require .NET.

 

Yes you can probably get it working with a batch file but they are very painful to debug and usually become very difficult to understand later even for the person who wrote them.



#18 Wonko the Sane

Wonko the Sane

    The Finder

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

Posted 21 July 2017 - 04:08 PM

There are also many other scripting languages you could use which would make your job easier once you learn the language - autoit / vbscript to name 2. Powershell is also a very powerful language but does require .NET.

 

Yes you can probably get it working with a batch file but they are very painful to debug and usually become very difficult to understand later even for the person who wrote them.

Sure :), once you have learned them, and JFYI, I can read and understand just fine batches I wrote  some 20+ years ago, maybe I am an exception :dubbio:

 

:duff:

Wonko


  • devdevadev likes this

#19 devdevadev

devdevadev

    Frequent Member

  • Advanced user
  • 406 posts
  •  
    India

Posted 22 July 2017 - 06:13 AM

This display Drive Letter and it's free spaces *(in MB). 

for /F "tokens=2-16 delims=:\ " %%A in ('fsutil fsinfo drives') do (
  for %%d in (%%A %%B %%C %%D %%E %%F %%G %%H %%I %%J %%K %%L %%M %%N %%O) do (
    for /F "tokens=2 delims=:" %%f in ('fsutil volume diskfree %%d: ^| find "of free bytes" ') do @(
      set mb=%%f
      set mb=!mb:~,-3!
      set /A mb /= 1024 >nul
      echo %%d: !mb!
    )
  )
) 

But how can I get Drive Letter of Disk Volume having maximum FREE SPACE *(in MB) using batch file ? 



#20 cdob

cdob

    Gold Member

  • Expert
  • 1338 posts

Posted 22 July 2017 - 07:28 AM

Which windows version do you use?
'fsutil fsinfo drives' does not print spaces between the drive list, fsutil prints 0x0 instead.
Use a work arround to change the 0x0 byte.
 
set first=true
FOR /F "tokens=1-2* delims=\ " %%a in ('fsutil fsinfo drives ^| more') do (
  if not defined first set drive=%%a
  if defined first ( set drive=%%b &set first=)
  echo !drive!
  
  FOR /F "tokens=2 delims=:" %%f in ('fsutil volume diskfree !drive! ^| find "of free bytes" ') do @(
    set mb=%%f
    set mb=!mb:~,-3!
    set /A mb /= 1024 >nul
    echo !drive! !mb!
    SET BACKUP_SIZE
    if !mb! GTR %BACKUP_SIZE% set FREEDRIVE=!drive!& set FREEDRIVE
  )
)


#21 devdevadev

devdevadev

    Frequent Member

  • Advanced user
  • 406 posts
  •  
    India

Posted 22 July 2017 - 08:53 AM

2) determine which (first? :w00t:) volume that has enough free space to receive the backup -> this will output the target volume, i.e. "FreeDrive"

It may or may not be first volume. AFAIK, C: Drive may or may not be the 'Windows OS' Drive in WinPE10 ? I will not prefer to backup files to 'Windows OS' Drive i.e. %SYSPART% *(which I will definitely format after backup.) And I will also not prefer to copy Backup files to X: Drive (i.e %SystemDrive%).

 

Should I use something like this ?

if !mb! GTR %BACKUP_SIZE% if not "%SYSPART%"=="%%d:" if not "%SystemDrive%"=="%%d:" set FREEDRIVE=%%d:& GOTO :DO_BACKUP

Which windows version do you use? 

Currently I am testing these batch files in 'Win 10 Build 16232'  and I will use this batch file within WinPE10 Build 1703.

 

How can I echo TIME as '12-58 PM' in batch file ?



#22 Wonko the Sane

Wonko the Sane

    The Finder

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

Posted 22 July 2017 - 11:48 AM

It seems like post #14 didn't get through. :w00t:

 

Actually it is there and actually makes correctly the list of all drives.

 

I will risk an other post that will probably be ignored as well.

 

The "find of free bytes" is language dependent, BTW.

@ECHO OFF
SETLOCAL EnableDelayedExpansion
FOR /F "delims=" %%A in ('fsutil fsinfo drives^|find /v""') do (
SET ThisDrive=%%A
SET AllDrives=!Alldrives! !ThisDrive:~-3,2!
)

SET ALLDrives

FOR %%A IN (%ALLDrives%) DO (
FOR /F "skip=2 tokens=2 delims=:" %%B IN ('fsutil volume diskfree %%A') DO (
ECHO %%A %%B
SET Vol_%%A=%%B
)
)

SET NTFSDrives=
FOR /F "tokens=2 delims=_=" %%A IN ('SET VOL_') DO SET NTFSDRIVES=!NTFSDrives! %%A

SET NTFSDRIVES

FOR %%A IN (%NTFSDrives%) DO CALL :Sort_val %%A !VOL_%%A!

FOR /F "tokens=2 Delims==" %%A IN ('SET Free_') DO SET MostFree=%%A

ECHO Drive with the most free space is drive %MostFree%
ECHO !VOL_%MostFree%!

GOTO :EOF


:Sort_val
SET Num=0000000000000%2
SET Num=%Num:~-14,14%
SET Free_%Num%=%1
GOTO :EOF

:duff:

Wonko



#23 devdevadev

devdevadev

    Frequent Member

  • Advanced user
  • 406 posts
  •  
    India

Posted 22 July 2017 - 12:15 PM

Sorry...I have tried to test post #14 codes. But could not understood results of variable properly. BTW

 

SET ALLDrives

display

Alldrives= \:

 Is something wrong ? or Is it the goal of script in phase 1 ? When I run above script It gives error messages ?



#24 Wonko the Sane

Wonko the Sane

    The Finder

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

Posted 22 July 2017 - 01:55 PM

I don't know.

 

The issue with fsutil fsinfo drives is that it (at least on most OS) prints a 0x00 character after each drive letter, as cdob just explained.

 

Hence the workarounds, like 

fsutil fsinfo drives |find /v"" <- suggested by Wonko

or

fsutil fsinfo drives | more <- suggested by cdob

 

The plain output should be:

Drives: C:\ D:\ E:\ F:\ G:\ H:\ I:\ J:\

when one of the workarounds is used it should become:

 

Drives: C:\
D:\
E:\
F:\
G:\
H:\
I:\
J:\

 

Maybe on your (stupid) Windows 10 something has changed, it is up to you to find how it behaves.

 

:duff:

Wonko



#25 devdevadev

devdevadev

    Frequent Member

  • Advanced user
  • 406 posts
  •  
    India

Posted 22 July 2017 - 05:02 PM

I have tested this code

@echo off
setlocal EnableDelayedExpansion
set first=true
FOR /F "tokens=1-2* delims=\ " %%a in ('fsutil fsinfo drives ^| more') do (
  if not defined first set drive=%%a
  if defined first ( set drive=%%b &set first=)
  echo !drive!
  
  FOR /F "tokens=2 delims=:" %%f in ('fsutil volume diskfree !drive! ^| find "of free bytes" ') do @(
    set mb=%%f
    set mb=!mb:~,-3!
    set /A mb /= 1024 >nul
    echo !drive! !mb!
    if !mb! GTR 2000 set FREEDRIVE=!drive!& set FREEDRIVE
  )
)
ECHO %FREEDRIVE%
ECHO !FREEDRIVE!
robocopy "%USERDIR%\Music" "%FREEDRIVE%\BACKUP_%USER%_%DATETIME%\Music" /E /MT:1
PAUSE

And it gives following result ?

C:
C:  2419
FREEDRIVE=C:
C:
C:

-------------------------------------------------------------------------------
   ROBOCOPY     ::     Robust File Copy for Windows
-------------------------------------------------------------------------------

  Started : Saturday, July 22, 2017 10:33:14 PM
   Source : C:\Music\
     Dest : C:\Data\ \BACKUP__\Music\

    Files : *.*

  Options : *.* /S /E /DCOPY:DA /COPY:DAT /MT:1 /R:1000000 /W:30

------------------------------------------------------------------------------

2017/07/22 22:33:14 ERROR 2 (0x00000002) Accessing Source Directory C:\Music\
The system cannot find the file specified.
Press any key to continue . . .

Why I am getting (0x00000002) error message while I am using cdob fixed code ? Why 'robocopy' is showing wrong Destination path ?







Also tagged with one or more of these keywords: cmd, winpe, boot.wim, usb booting, multibooting

2 user(s) are reading this topic

0 members, 2 guests, 0 anonymous users