我有%cd%这是一个路径,例如c:\ docu 我需要在一个新的环境变量c:\ d o c u中生成 我能怎么做?感谢
答案 0 :(得分:1)
@echo off
call :strlen0 cd len
echo %len%
set "final="
setlocal EnableDelayedExpansion
for /l %%# in (0,1,%len%) do (
set "letter_with_space=!cd:~%%#,1! "
set final=!final!!letter_with_space!
)
endlocal && (
set "final=%final%"
)
echo %final%
exit /b 0
:strlen0 StrVar [RtnVar]
setlocal EnableDelayedExpansion
set "s=#!%~1!"
set "len=0"
for %%N in (4096 2048 1024 512 256 128 64 32 16 8 4 2 1) do (
if "!s:~%%N,1!" neq "" (
set /a "len+=%%N"
set "s=!s:~%%N!"
)
)
endlocal&if "%~2" neq "" (set %~2=%len%) else echo %len%
exit /b
已完成%cd%
,因为这是您所要求的,但可以通过任何变量完成。strlen
子程序取自此处:http://ss64.org/viewtopic.php?id=424
答案 1 :(得分:0)
创建一个名为AddSpace.bat的bat文件:
@echo off
setlocal enabledelayedexpansion
set v=%*
set var=
set /a l=0
:loop
set c=!v:~%l%,1!
if "%c%"=="" goto end
set "var=%var%%c% "
set /a l+=1
goto loop
:end
Echo %var%
:: You can do whatever you want with the variable here.
endlocal
C:\>>AddSpace.bat abcdef
a b c d e f
C:\>>AddSpace.bat C:\Program Files
C : \ P r o g r a m F i l e s
C:\>>AddSpace.bat Hi This Message Has Spaces
H i T h i s M e s s a g e H a s S p a c e s
C:\>>AddSpace.bat Lots of Spaces here -
L o t s o f S p a c e s h e r e -
C:\>>AddSpace.bat C:\docu
C : \ d o c u
C:\>>
NB 正如您所看到的,它也会加倍空格。这很容易修复,只需添加:
if "%c%"==" " goto loop
在if "%c%"=="" goto end