| 1. |
Solve : how to read the number of files in a folder? |
|
Answer» hello Then how to make a loop command for doing 7 times in batch? Like other programming: You use the FOR command with the /L switch FOR /L %%[single letter a-z,A-Z] IN (start,step,end) DO command e.g. Code: [Select]FOR /L %%i IN (1,1,7) DO echo %%i or you can have more than one line in the loop using parentheses Code: [Select]FOR /L %%i IN (1,1,7) DO ( echo This is loop number %%i echo %time% ) or Code: [Select]set start=3 set step=2 set limit=27 echo Here are some odd numbers... FOR /L %%i IN (%start%,%step%,%limit%) DO ( echo %%i ) to see full details type FOR /? at the prompt. If you are used to other languages, you may find that you cannot set and read variables INSIDE the loop as you could in other languages, as cmd.exe expands all variables at runtime, and variables contained in parenthetical expressions like these are blank at runtime. Code: [Select]IF "%variable1%=="%variable2%" ( set message=EQUAL echo %message% ) FOR /L %%i IN (1,1,7) DO ( if "%%i"=="5" set message=FIVE echo %message% ) In those two examples, %message% will be blank inside the parentheses [but not afterwards]. You need to use delayed expansion. Enable it like so setlocal enabledelayedexpansion and the variables use ! instead of % so these would work Code: [Select]setlocal enabledelayedexpansion IF "%variable1%=="%variable2%" ( set message=EQUAL echo !message! ) setlocal enabledelayedexpansion FOR /L %%i IN (1,1,7) DO ( if "%%i"=="5" set message=FIVE echo !message! ) |
|