如何在同一个循环中操作字符串和变量

时间:2018-05-28 14:22:26

标签: batch-file

1需要帮助来缩短代码

2.(如果可行)关于映射时区分大小写

Sample.Bat:

@echo off & setlocal EnableDelayedExpansion
set "Data=HELLO"


:Encrypt/Decrypt_Rot13

set Encrypted_Data=!Data!
set Encrypted_Data=!Encrypted_Data:A=A#!
set Encrypted_Data=!Encrypted_Data:B=B#!
set Encrypted_Data=!Encrypted_Data:C=C#!
set Encrypted_Data=!Encrypted_Data:D=D#!
set Encrypted_Data=!Encrypted_Data:E=E#!
set Encrypted_Data=!Encrypted_Data:F=F#!
set Encrypted_Data=!Encrypted_Data:G=G#!
set Encrypted_Data=!Encrypted_Data:H=H#!
set Encrypted_Data=!Encrypted_Data:I=I#!
set Encrypted_Data=!Encrypted_Data:J=J#!
set Encrypted_Data=!Encrypted_Data:K=K#!
set Encrypted_Data=!Encrypted_Data:L=L#!
set Encrypted_Data=!Encrypted_Data:M=M#!
set Encrypted_Data=!Encrypted_Data:N=N#!
set Encrypted_Data=!Encrypted_Data:O=O#!
set Encrypted_Data=!Encrypted_Data:P=P#!
set Encrypted_Data=!Encrypted_Data:Q=Q#!
set Encrypted_Data=!Encrypted_Data:R=R#!
set Encrypted_Data=!Encrypted_Data:S=S#!
set Encrypted_Data=!Encrypted_Data:T=T#!
set Encrypted_Data=!Encrypted_Data:U=U#!
set Encrypted_Data=!Encrypted_Data:V=V#!
set Encrypted_Data=!Encrypted_Data:W=W#!
set Encrypted_Data=!Encrypted_Data:X=X#!
set Encrypted_Data=!Encrypted_Data:Y=Y#!
set Encrypted_Data=!Encrypted_Data:Z=Z#!

set Encrypted_Data=!Encrypted_Data:A#=N!
set Encrypted_Data=!Encrypted_Data:B#=O!
set Encrypted_Data=!Encrypted_Data:C#=P!
set Encrypted_Data=!Encrypted_Data:D#=Q!
set Encrypted_Data=!Encrypted_Data:E#=R!
set Encrypted_Data=!Encrypted_Data:F#=S!
set Encrypted_Data=!Encrypted_Data:G#=T!
set Encrypted_Data=!Encrypted_Data:H#=U!
set Encrypted_Data=!Encrypted_Data:I#=V!
set Encrypted_Data=!Encrypted_Data:J#=W!
set Encrypted_Data=!Encrypted_Data:K#=X!
set Encrypted_Data=!Encrypted_Data:L#=Y!
set Encrypted_Data=!Encrypted_Data:M#=Z!
set Encrypted_Data=!Encrypted_Data:N#=A!
set Encrypted_Data=!Encrypted_Data:O#=B!
set Encrypted_Data=!Encrypted_Data:P#=C!
set Encrypted_Data=!Encrypted_Data:Q#=D!
set Encrypted_Data=!Encrypted_Data:R#=E!
set Encrypted_Data=!Encrypted_Data:S#=F!
set Encrypted_Data=!Encrypted_Data:T#=G!
set Encrypted_Data=!Encrypted_Data:U#=H!
set Encrypted_Data=!Encrypted_Data:V#=I!
set Encrypted_Data=!Encrypted_Data:W#=J!
set Encrypted_Data=!Encrypted_Data:X#=K!
set Encrypted_Data=!Encrypted_Data:Y#=L!
set Encrypted_Data=!Encrypted_Data:Z#=M!

echo. 
echo. Original Data:%data% 
echo. 
echo. Encrypted/Decrypted Data:%Encrypted_Data%
echo. 
pause

正如你所看到的,我使用的是映射方法,它工作得很好(输入= HELLO,输出= URYYB),但我真的很累,如果需要的话可以逐个修改它,或者把它变成微型。

但是当我尝试使用Loop并使用相同的方法缩短代码时。

Sample.Bat:

@echo off & setlocal EnableDelayedExpansion

set "Data=HELLO"

set "Process=Rot13"

set "Rot13_input= ABCDEFGHIJKLMNOPQRSTUVWXYZ"
set "Rot13_output= NOPQRSTUVWXYZABCDEFGHIJKLM"

::set "xxxxxx_input= xxxxxxxxxxxxxxxxxxx"
::set "xxxxxx_output= xxxxxxxxxxxxxxxxxxx"

if %Process% equ Rot13 set "Maxdigit=27"

for /l %%n in (1,1,%Maxdigit%) do ^
set "%Process%_input_%%n=!Rot13_input:~%%n,1!" & ^
set "%Process%_output_%%n=!Rot13_output:~%%n,1!"

::set Rot13_input_
::set Rot13_output_

set Encrypted_Data=!Data!

:: STUCK BELOW ::

for /l %%n in (1,1,%Maxdigit%) do ^
set Encrypted_Data=!Encrypted_Data:%Rot13_input_%%n%=%Rot13_input_%%n%#!

echo %Encrypted_Data%

:: STUCK ABOVE ::

pause

echo. 
echo. Original Data:%data% 
echo. 
echo. Encrypted/Decrypted Data:%Encrypted_Data%
echo. 
pause

我在第二次循环时被卡住(错误输出),任何人都可以修复此问题或者可以采用其他方法吗?

1 个答案:

答案 0 :(得分:1)

这就是我这样做的方式:

@echo off & setlocal EnableDelayedExpansion

rem ---------------

set "Process=Rot13"

set "Rot13_input= ABCDEFGHIJKLMNOPQRSTUVWXYZ"
set "Rot13_output= NOPQRSTUVWXYZABCDEFGHIJKLM"

if %Process% equ Rot13 set "Maxdigit=26"

for /L %%n in (1,1,%Maxdigit%) do for /F %%c in ("!%Process%_input:~%%n,1!") do (
   set "%Process%_input_%%c=%%n"
)

rem ---------------

set "Data=HELLO"

set "DataLen=0"
for /L %%a in (10,-1,0) do (
   set /A "newLen=DataLen+(1<<%%a)"
   for %%b in (!newLen!) do if "!Data:~%%b,1!" neq "" set "DataLen=%%b"
)

set "Encrypted_Data="
for /L %%i in (0,1,%DataLen%) do for /F %%c in ("!Data:~%%i,1!") do for /F %%j in ("!%Process%_input_%%c!") do (
   set "Encrypted_Data=!Encrypted_Data!!Rot13_output:~%%j,1!"
)


echo Original Data: %data% 
echo/ 
echo Encrypted/Decrypted Data: %Encrypted_Data%
echo/ 
pause

这是区分大小写的版本:

@echo off & setlocal EnableDelayedExpansion

rem ---------------

set "Process=Rot13"

set "Rot13_input= ABCDEFGHIJKLMNOPQRSTUVWXYZ"
set "Rot13_output= NOPQRSTUVWXYZABCDEFGHIJKLMnopqrstuvwxyzabcdefghijklm"

if %Process% equ Rot13 set "Maxdigit=26"

for /L %%n in (0,1,%Maxdigit%) do for /F "delims=" %%c in ("!%Process%_input:~%%n,1!") do (
   set "%Process%_input_%%c=%%n"
)

rem ---------------

set "Data=Hello World"

set "DataLen=0"
for /L %%a in (10,-1,0) do (
   set /A "newLen=DataLen+(1<<%%a)"
   for %%b in (!newLen!) do if "!Data:~%%b,1!" neq "" set "DataLen=%%b"
)

set "Encrypted_Data="
for /L %%i in (0,1,%DataLen%) do for /F "delims=" %%c in ("!Data:~%%i,1!") do (
   set "index=!%Process%_input_%%c!"
   if "!Rot13_input:%%c=%%c!" neq "%Rot13_input%" set /A "index+=Maxdigit"
   for /F %%j in ("!index!") do set "Encrypted_Data=!Encrypted_Data!!Rot13_output:~%%j,1!"
)


echo Original Data: %data% 
echo/ 
echo Encrypted/Decrypted Data: %Encrypted_Data%
echo/ 
pause