-2

I have to check whether a disk partition is ntfs or exFAT with cmd commands and assign the output to a variable.

If it is exFAT it will go to the formatting tag and will be formatted as NTFS but if it is NTFS it will go to the copyfile tag and some files will be copied to that partition.

I've been searching the internet for 3 days and I couldn't find it, maybe you can help or guide me on this issue.

Thank you in advance.

@echo off
call :NTFSLetter
for /f "tokens=5" %%a in ('fsutil fsinfo volumeinfo "%ntfsdrive%:"^|findstr /B "File System Name : "') do set DriveType=%%a
if "%DriveType%"=="exFAT" (
echo %DriveType%
     GOTO :NTFSFormat 
) ELSE (
     GOTO :copyfile
)
:NTFSFormat
mycode...
pause
:copyfile
mycode...
pause

:NTFSLetter
    set "ntfsdrive="
    for %%a in (Z Y X W V U T S R Q P O N M L K J I H G F E D C) do cd %%a: 1>>nul 2>&1 & if errorlevel 1 set ntfsdrive=%%a
    exit /b 0

Explanation:
I give the NTFS partition the drive letter automatically thanks to the :NTFSLetter tag.

So I have to use the %ntfsdrive% variable...

But this code structure is not working...

  • I recommend to read [Where does GOTO :EOF return to?](https://stackoverflow.com/a/37518000/3074564) after execution of `GOTO :NTFSFormat` and the error message output by first `mycode...` and execution of `pause` the execution of the batch file continues with the code below the awfully named label `:copy` resulting in one more error message because of second `mycode...` and one more `pause` and then `cmd.exe` processes once more the code in the subroutine `NTFSLetter`. So that is not really a [minimal, reproducible example?](https://stackoverflow.com/help/minimal-reproducible-example) – Mofi Jun 04 '22 at 19:01
  • The main problem is `for %%a in (Z Y X W V U T S R Q P O N M L K J I H G F E D C) do cd %%a: 1>>nul 2>&1 & if errorlevel 1 set ntfsdrive=%%a`. I suppose, that it should assign the drive letter of last existing drive with a storage media (USB memory stick or SD card) to the environment variable `ntfsdrive`. But in real it assigns the drive letter of last __not__ existing drive to the environment variable. I have the drives `Y:` (NTFS), `R:` (DVD drive), `F:` (FAT32), `E:` (FAT32), `D:` (NTFS) and `C:` (NTFS) and so `ntfsdrive` is defined with letter `G`. – Mofi Jun 04 '22 at 19:11
  • The execution of `fsutil fsinfo volumeinfo "G:"` results in the error message `Error: The system cannot find the path specified.` to handle __STDERR__ and nothing to __STDOUT__. So __FINDSTR__ searching case-sensitive at beginning on the lines for either the word `File` __OR___ the word `System` __OR__ the word `Name` __OR__ the character `:` cannot find any line with a matching string and outputs nothing. For that reason `for /f` cannot process any line and therefore `if "" == "exFAT"` is executed always if the environment variable `DriveType` is not by chance defined outside of bath file. – Mofi Jun 04 '22 at 19:12
  • The working batch file could have the following thirteen lines: __1.__ `@echo off` __2.__ `setlocal EnableExtensions DisableDelayedExpansion` __3.__ `for %%I in (Z Y X W V U T S R Q P O N M L K J I H G F E D C) do cd %%I: 1>nul 2>nul && (set "LastDrive=%%I:" & goto FoundLastDrive)` __4.__ `echo ERROR: Could not find any drive!& goto EndBatch` __5.__ `:FoundLastDrive` __6.__ `for /F "tokens=5" %%I in ('%SystemRoot%\System32\fsutil.exe fsinfo volumeinfo %LastDrive% ^| %SystemRoot%\System32\find.exe /I "File System Name : "') do set "FileSystem=%%I"` – Mofi Jun 04 '22 at 19:28
  • __7.__ `if /I not "%FileSystem%" == "exFAT" goto CopyFiles` __8.__ `echo Drive %LastDrive% is an exFAT drive.& goto EndBatch` __9.__ `:CopyFiles` __10.__ `echo Copy files to drive %LastDrive% ...` __11.__ `:EndBatch` __12.__ `endlocal` __13.__ `pause` I don't post an answer with this code with a description because of the missing description on what the posted code in question should do at all, i.e. how it should work. – Mofi Jun 04 '22 at 20:13

0 Answers0