Page 1 of 1

Change position of environment variables output!

Posted: 09 Jun 2020 04:53
by PAB
Good morning,

I would like to adjust the position of two of the outputs of environment variables, if possible that is!

[1] The first is fsutil fsinfo drives, which outputs on a seperate line Drives: C:\ D:\ F:\ for example.
I would like it to output on the same line as the text below . . .

Code: Select all

echo The drives available are: Drives: C:\ D:\ F:\
[2] The second is wmic logicaldisk get caption, volumename, which outputs on a seperate lines . . .

Code: Select all

Caption  VolumeName
C:
D:
F:       Image
I would like it to output the same but with a space on the left of each . . .

Code: Select all

 Caption  VolumeName
 C:
 D:
 F:       Image
Is this possible please?

I have tried using ! instead of %, setlocal EnableDelayedExpansion, and the pipe command, all to no avail.

Thanks in advance!

Re: Change position of environment variables output!

Posted: 09 Jun 2020 08:48
by T3RRY
The typical method to achieve output like this is to use a for /F loop executing on the command to capture the output of the command into a variable, with the necessary definition of tokens and delims required for the desired output - which varies depending on the structure of the commands output and the specific elements of the output you need to capture. The result of each command can be appended to the variable.

An example of using this methodology to build a string:

Code: Select all

Setlocal EnableDelayedExpansion
Set "AvDrives=Available Drives are: "
For /F "Tokens=2 Delims==" %%a in ('wmic logicaldisk get deviceid /value') do Set "AvDrives=!AvDrives! %%a"
The simpler FSutil version:

Code: Select all

For /F "Delims=" %a in ('FsUtil FSInfo drives') do Set "AvDrives=Available %a"

Re: Change position of environment variables output!

Posted: 09 Jun 2020 09:59
by PAB
Thanks for the reply T3RRY, it is appreciated!

The FsUtil FSInfo drives works brilliantly.

As far as the wmic logicaldisk get caption, volumename is concerned, I waant the output as it is but with a space in front of each line of the output . . .

Code: Select all

 Caption  VolumeName
 C:
 D:
 F:       Image
Thanks in advance.

Re: Change position of environment variables output!

Posted: 09 Jun 2020 12:50
by T3RRY
wmic output takes a little more work, refer to the remarks for more.

Code: Select all

@Echo off & Setlocal ENABLEdelayedexpansion
rem // Line feed variable used to define multi line variable
	(Set LF=^


	%= NewLine variable =%)
rem // space string that can be used to format concatenated variables in table form using substring modification.
Set "SPACING=                                                                             "
rem // Usebackq used to maintain complete for set with presence of `,` delimiter
For /F "UsebackQ Delims=" %%a in (`"wmic logicaldisk get caption, volumename"`) Do (
rem // secondary for loop required to strip extra CR returned by wmic
	For /f "delims=" %%b in ("%%a") Do (
rem // delayed expansion used to concatenate variable within code block
		If not "%%b" == "" Set "wmicOut=!wmicOut!!SPACING:~0,1!%%b!LF!"
	)
)
rem // Delayed expansion required to prevent command parsing stopping when LF variable encountered during expansion of the variable.
Echo/!wmicOut!
An example to elaborate on the output that can be constucted using substring modification with spacing and string concatenation.

Code: Select all

"C:\Users\tcdou\Desktop\Matrix_Components\"

BatchMonitor.vbs                              1               Days Newer.         Modified: 09 06 2020 Size: 427             Bytes
BatchMusicPlayer.bat                          1               Days Newer.         Modified: 09 06 2020 Size: 494             Bytes
BG.EXE                                        10              Days Older.         Modified: 29 05 2020 Size: 9728            Bytes
cmdbkg.exe                                    25              Days Older.         Modified: 14 05 2020 Size: 21518           Bytes
endtimes.bmp                                  25              Days Older.         Modified: 14 05 2020 Size: 53747766        Bytes
GetFontSize.exe                               10              Days Older.         Modified: 29 05 2020 Size: 1536            Bytes
GetKey.exe                                    38              Days Older.         Modified: 01 05 2020 Size: 1536            Bytes
Kai_Engel_-_05_-_When_the_World_Falls_Down.mp 44              Days Older.         Modified: 25 04 2020 Size: 7661953         Bytes
Kai_Engel_-_07_-_Downpour_Pon_X.mp3           44              Days Older.         Modified: 25 04 2020 Size: 6056258         Bytes
Kai_Engel_-_07_-_modum.mp3                    44              Days Older.         Modified: 25 04 2020 Size: 6669509         Bytes
Kai_Engel_-_08_-_daemones.mp3                 44              Days Older.         Modified: 25 04 2020 Size: 6949541         Bytes
Kai_Engel_-_08_-_Downfall.mp3                 44              Days Older.         Modified: 25 04 2020 Size: 5152421         Bytes
Matrix GETKEY.bat                             16              Days Older.         Modified: 23 05 2020 Size: 23456           Bytes
PlayMusic.vbs                                 1               Days Newer.         Modified: 09 06 2020 Size: 282             Bytes
StopMusic.bat                                 1               Days Newer.         Modified: 09 06 2020 Size: 60              Bytes

"C:\Users\tcdou\Desktop\ShadowLand\"

CopyrightTDoust2020allrightsreserved.jpg      195             Days Older.         Modified: 26 11 2019 Size: 268391          Bytes
desktop.ini                                   52              Days Older.         Modified: 17 04 2020 Size: 181             Bytes
Flowchart.drawio                              181             Days Older.         Modified: 10 12 2019 Size: 4024            Bytes
GetKey.exe                                    38              Days Older.         Modified: 01 05 2020 Size: 1536            Bytes
matrix - Copy.bat                             51              Days Older.         Modified: 18 04 2020 Size: 7095            Bytes
Rpg progress and Goals.txt                    165             Days Older.         Modified: 26 12 2019 Size: 23939           Bytes
ShadowLands.bat                               33              Days Older.         Modified: 06 05 2020 Size: 41452           Bytes


Re: Change position of environment variables output!

Posted: 26 Aug 2020 16:12
by PAB
T3RRY wrote:
09 Jun 2020 12:50
wmic output takes a little more work, refer to the remarks for more.
Well, thanks to your code and explanations I was able to achieve what I needed, thank you.
In fact I have extended it to provide two more outputs as below.
The first one [ CODE 1 ] is your one and works perfectly.
My question is please, is there a way to condense the FOR loops or is the way I have done it correct?

Code: Select all

:: ------------ SETUP LF & SPACING

(set LF=^


%= newline variable =%)
set "SPACING=                                                                             "

:: ------------ CODE 1

for /f "usebackq delims=" %%a in (`"wmic logicaldisk get caption, volumename"`) do (
  for /f "delims=" %%b in ("%%a") do (
    if not "%%b"=="" set "Volume_Name=!Volume_Name!!SPACING:~0,1!%%b!LF!"
  )
)

:: ------------ CODE 2

for /f "usebackq delims=" %%a in (`"wmic diskdrive get Model, MediaType, SIze, Partitions, Index, InterfaceType"`) do (
  for /f "delims=" %%b in ("%%a") do (
    if not "%%b"=="" set "Disk=!Disk!!SPACING:~0,1!%%b!LF!"
  )
)

:: ------------ CODE 1

for /f "usebackq delims=" %%a in (`"wmic /namespace:\\root\wmi path MSStorageDriver_FailurePredictStatus"`) do (
  for /f "delims=" %%b in ("%%a") do (
    if not "%%b"=="" set "Fail_Predict=!Fail_Predict!!SPACING:~0,1!%%b!LF!"
  )
)
Then I use these variables . . .

Code: Select all

!Volume_Name!
!Disk!
!Fail_Predict!
They all work but I wondered if there was a way to nest the FOR loops so there is not so much code in case I want to add one or two at a later date?

Thanks in advance.