Removing space from variable in batch file
5,520
How can I remove the leading space from a variable?
Use the following batch file (test.cmd):
@echo off
setlocal enabledelayedexpansion
FOR /F "tokens=2 delims=:" %%G IN ('netsh wlan show profiles') DO (
SET _temp=%%G
SET wifiname=!_temp:~1!
netsh wlan show profile name="!wifiname!" key=clear
)
endlocal
Example output:
> test
Profile virginmedia3954038 on interface Wireless Network Connection:
=======================================================================
Applied: All User Profile
Profile information
-------------------
Version : 1
Type : Wireless LAN
Name : virginmedia3954038
Control options :
Connection mode : Connect automatically
Network broadcast : Connect only if this network is broadcasting
AutoSwitch : Do not switch to other networks
...
- An A-Z Index of the Windows CMD command line | SS64.com
- Windows CMD Commands (categorized) - Windows CMD - SS64.com
- EnableDelayedExpansion - Windows CMD - SS64.com
- For - Loop through command output - Windows CMD - SS64.com
Related videos on Youtube
Author by
Jerry
Updated on September 18, 2022Comments
-
Jerry 3 months
Why is variable
wifiname
empty inshow profile
command?FOR /F "tokens=2 delims=:" %%G IN ('"netsh wlan show profiles"') DO ( Set myvar=%%G SET wifiname=%myvar:~1% @echo '%%G' @echo '%wifiname%' netsh wlan show profile name="%wifiname%" key=clear >> wp.txt )
If I pass
%%G
directly (without double quotes), it is working excluding networks with multiple words delimited with space. Leading space is ignored.With double quotes, full network name is passed, byt leading space is problem for each network name...
Simplest solution - remove leading space from
%%G
and pass this result in double quotes to the final command... How? -
Jerry almost 4 yearsworks, any clear explanation when use % or %% or %_% or ! and etc.?
-
DavidPostill almost 4 years@Jerry See the last two links I added.