次のようなバッチファイルにforループがあります。
for %%y in (100 200 300 400 500) do (
set /a x = y/25
echo %x%
)
この線:
set /a x = y/25
除算を行っていないようです。各yを25で割る正しい構文は何ですか?この除算からの整数の結果のみが必要です。
次のようなバッチファイルにforループがあります。
for %%y in (100 200 300 400 500) do (
set /a x = y/25
echo %x%
)
この線:
set /a x = y/25
除算を行っていないようです。各yを25で割る正しい構文は何ですか?この除算からの整数の結果のみが必要です。
SET / Aステートメントで使用するために、環境変数を拡張する必要はありません。ただし、FOR変数は展開する必要があります。
また、計算が機能した場合でも、ステートメントの解析時にパーセント展開が行われ、FOR構造全体が一度に解析されるため、ECHOは失敗します。したがって、%x%の値は、ループが実行される前に存在していた値になります。ループ内で設定された値を取得するには、遅延拡張を使用する必要があります。
また、代入演算子の前のスペースを削除する必要があります。名前にスペースが含まれる変数を宣言しています。
@echo off
setlocal enableDelayedExpansion
for %%A in (100 200 300 400 500) do (
set n=%%A
REM a FOR variable must be expanded
set /a x=%%A/25
REM an environment variable need not be expanded
set /a y=n/25
REM variables that were set within a block must be expanded using delayed expansion
echo x=!x!, y=!y!
REM another technique is to use CALL with doubled percents, but it is slower and less reliable
call echo x=%%x%%, y=%%y%%
)
「y」は単なる文字であるため、何もしていません。変数を参照するにはパーセント記号が必要です。
set /a x = %%y/25
私は同じ問題を抱えていましたが、整数の問題であることが判明しました。割り算した後、掛け算をしていましたが、前に掛ける必要があります。起こっていたのは次のようなものです:1 \ 100=0のように動作する1/100x100、次に0x100=0私はそれを1x100=100のように動作する1x100/100、次に100/100=1のように変更しました
@echo off
setlocal ENABLEDELAYEDEXPANSION
for /f "usebackq" %%b in (`type List.txt ^| find "" /v /c`) do (
set Count=%%b
)
)
REM Echo !Count! -->Returns the correct number of lines in the file
for /F "tokens=*" %%A in (List.txt) do (
set cName=%%A
set /a Number+=1
REM Echo !Number! -->Returns the correct increment of the loop
set /a Percentage=100*!Number!/!Count!
REM Echo !Percentage! -->Returns 1 when on the first line of a 100 line file
set a=1
set b=1000
set /a c=100*1/100
Rem -->echo c = !c! --Returns "C = 1"
)