2

I have been working on a batch file that creates a grid of variables like this:

%G1%%G2%%G3%%G4%%G5% 
%G6%%G7%%G8%%G9%%G10% 
%G11%%G12%%G13%%G14%%G15% 

but when I try running my batch file it just writes this:

%G1%%G2%%G3%%G4%%G5%
Echo is off.
Echo is off.
Echo is off.
Echo is off.

I have added the code here please could someone tell me what I am doing wrong?

@echo off
title GirdMaker
setlocal enabledelayedexpansion

set lineid=0
set COLS=0
set LINES=0
set /p LINES=Enter number of lines:
set /p COLS=Enter the number of colums:
cls
set START=1
set linecount=0
:A
set LINE=
for /l %%I in (%START%,1,%COLS%) do (
set LINE=!LINE!%%G%%I%%
)
set /a linecount=%linecount%+1
echo %LINE% 
set /a lineid=%lineid%+%COLS%+1
set START=%lineid%

if %LINES% EQU %linecount% (
pause >nul
exit
) 
goto :A
4

2 に答える 2

2

この部分を分析してみましょう:

:A
set LINE=
for /l %%I in (%START%,1,%COLS%) do (
set LINE=!LINE!%%G%%I%%
)

最初のループ反復はこれを作成します:set LINE=%G1%そうですか?2番目のループの反復:set LINE=%G1%%G2%など、そうなるまで続きset LINE=%G1%%G2%%G3%%G4%%G5%ますか?

さて、この行:

echo %LINE% 

最初にこのように拡張されます:

echo %G1%%G2%%G3%%G4%%G5%

%展開は左から右に1回だけ実行されるため、再度展開されることはありません。結果は次のとおりです。

%G1%%G2%%G3%%G4%%G5%

すみませんが、プログラムから何が得られるかを教えてくれませんでしたが、変数G1からG5の値を表示する場合は、次のように%展開を再開する必要があります。

call echo %LINE%

他の回線については、制限を注意深く確認する必要があります。そのような場合、LINE変数が空​​であることは明らかです。

編集:おそらくこれはあなたが望むものですか?

set /p LINES=Enter number of lines: 
set /p COLS=Enter the number of colums: 
set index=0
for /L %%I in (1,1,%LINES%) do (
   set LINE=
   for /L %%J in (1,1,%COLS%) do (
      set /A index+=1
      set LINE=!LINE!%%G!index!%%
   )
   call echo !LINE!
)
于 2012-07-05T01:24:15.817 に答える
0

You aren't adjusting your end position in the for loop, so the contents are never running after the first line (START is already greater than COLS), consequently LINE is left empty and your call to echo is empty.

You need to change this:

for /l %%I in (%START%,1,%COLS%) do (

Which is trying to loop from (1-5, 6-5, 11-5). To something like this:

set /a ENDCOL = %START% + %COLS% - 1
for /l %%I in (%START%,1,%ENDCOL%) do (

That way, it will loop from 1-5, 6-10, 11-15 etc...

于 2012-07-04T11:06:37.513 に答える