1.

Solve : how to read the number of files in a folder?

Answer»

hello
If I want to import 10 files in a folder, how to write a batch SCRIPT in order to import those 10 files? I mean how to read the number of files in that folder and write a loop for doing that?

thanks!!I am not sure exactly what you are asking, but I right click on item, click properities and click on general tab to see how many folder and files.in a batch:
Code: [Select]for %%P in (*) do <command>
I mean if there are 5 files in a folder, the dos batch command can return 5. It knows how to count the number of files in a folder

Thanks!Method 1

Code: [Select]echo off
setlocal enabledelayedexpansion
set /a files=0
for /F %%A in ('dir /b /a-d') do (
    set /a files=!files!+1
    )
echo There are %files% in this folder.
method 2

Code: [Select]for /f "tokens=1 delims= " %%A in ('dir /a-d ^| find "File(s)"') do echo %%A files thanks!actually in method 2 you don't need the "tokens=" stuff, this will do

Code: [Select]for /f %%A in ('dir /a-d ^| find "File(s)"') do echo %%A files
or if you want the number in a variable

Code: [Select]for /f %%A in ('dir /a-d ^| find "File(s)"') do set /a files=%%A>nul
echo %files%
thanks a lot for yr both help!Then how to make a loop command for doing 7 TIMES in batch? Like other programming:
for i < 7 then do (process), i++

can batch command make it?

thanks Quote from: garyypk on JULY 21, 2008, 01:26:56 AM

Then how to make a loop command for doing 7 times in batch? Like other programming:
for i < 7 then do (process), i++

can batch command make it?

thanks

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!
     )



Discussion

No Comment Found