FOR /F usage tricks?

Microsoft Windows
Post Reply
User avatar
MigrationUser
Posts: 336
Joined: 2021-Jul-12, 1:37 pm
Contact:

FOR /F usage tricks?

Post by MigrationUser »

15 Aug 2006 05:28
NoobTube

Code: Select all

@ECHO off
@ECHO.
@ECHO PLEASE ENTER THE FULL NAME OF THIS COMPUTERS MAIN USER
@ECHO.
@SET /P OWNERNAME=
@FOR /F "tokens=1 delims=," %%G IN ('"%SYSTEMROOT%\system32\getmac.exe" /FO CSV /NH') DO echo %%G >> .\full.txt
@ECHO %OWNERNAME%,%COMPUTERNAME%, >> .\full.txt
Current output
"72-19-E4-AA-AA-AA"
"00-11-D8-AA-AA-AA"
Owner Name,Computername,

Desired output
Owner Name,Computername,"00-11-D8-4A-07-5C","72-19-E4-51-37-DD"

Please forgive my stupid question. I know the answer is dirt simple, I just don't know it.

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

#2 15 Aug 2006 16:55
//[T.0.P]//


Here is one way of solving your problem, although it will require an extra sub-routine.

Code: Select all

FOR /F "tokens=1 delims," %%g in ("%SYSTEMROOT%\system32\getmac.exe" /FO CSV /NH') DO call :ADD_TO_FILE %%g
echo. %OWNERNAME%,%COMPUTERNAME%,%_fileline%>>.\full.txt

:ADD_TO_FILE
if DEFINED _fileline set _fileline=%_fileline%%*
if NOT DEFINED _fileline set _fileline=%*,
goto :eof
----------------------------

#3 29 Dec 2006 17:10
bluesxman


Yeah I'm a tad late, but this is how I would do it:

Code: Select all

@echo off
setlocal enabledelayedexpansion

echo:PLEASE ENTER THE FULL NAME OF THIS COMPUTERS MAIN USER
set /P OWNERNAME=^>^> 

set ownername=%ownername%,%computername%

for /F "tokens=1 delims=," %%G IN ('"%SYSTEMROOT%\system32\getmac.exe" /FO CSV /NH') DO set ownername=!ownername!,%%G

echo:%OWNERNAME%>> .\full.txt
- OR -

If you don't want to use delayed expansion for some reason:

Code: Select all

@echo off

echo PLEASE ENTER THE FULL NAME OF THIS COMPUTERS MAIN USER
set /P OWNERNAME=^>^> 

set ownername=%ownername%,%computername%

for /F "tokens=1 delims=," %%G IN ('"%SYSTEMROOT%\system32\getmac.exe" /FO CSV /NH') DO call :addvalue %%G

echo %OWNERNAME%>> .\full.txt

goto :EOF

:addvalue

set ownername=%ownername%,%1

goto :EOF
BTW: All your @ symbols (excluding the one on the "@echo off" line) are redundant.
Post Reply