1

I am new to batch and I am trying to do the following:

  • reading or scanning a folder
  • hold all file names in folder in an array variable (need to hold file name with or without extension)
  • loop through that array and create a call to a specific file/bat based on the type of file using an IF or CASE statement condition. example: if the filename has the word person in it, call a specific file/bat.

This is what I have so far:

@echo off

setlocal EnableDelayedExpansion

rem Populate the array with existent files in folder
set i=0
for %%b in (*.*) do (
   set /A   i+=1
   set list[!i!]=%%b

)

set Filesx=%i%

rem Display array elements
for /L %%i in (1,1,%Filesx%) do echo !list[%%i]!
Hackoo
  • 18,337
  • 3
  • 40
  • 70
  • please write with proper formatting. – Vinay Prajapati Aug 24 '16 at 06:18
  • You can take a look at this [Batch file Array creation/modification](http://stackoverflow.com/questions/38678498/batch-file-array-creation-modification/38680369#38680369) and this one too ==> [Open a file through cmd and display the selected in specific editor](http://stackoverflow.com/questions/38524510/open-a-file-through-cmd-and-display-the-selected-in-specific-editor/38525929#38525929) – Hackoo Aug 24 '16 at 07:52

2 Answers2

0
... do (
   echo !list[%%i]! | find /i "person" >nul && call specific.bat !list[%%i]!
)

echo !list[%%i]! | find /i "person": find the word
>nul: disregard the output (we don't need it, just the errorlevel)
&&: if previous command was successful (the word was found), then...

Do you really need that array? You can do it "on the fly":

for %%b in (*.*) do (
  echo %%b | find /i "person" >nul && call specific.bat "%%b"
)

for filename only, use %%~nb, for full name (including path), use %%~fb (see for /? for more options)

Stephan
  • 53,940
  • 10
  • 58
  • 91
0

This a quick modified version from : Open a file through cmd and display the selected in specific editor

It will scan on your desktop for any batch files which contains the word : person

@ECHO OFF
Title Scan a folder and store all files names in an array variables
:MenuLoop
Cls & Color 0A
SETLOCAL 
SET "ROOT=%userprofile%\Desktop\"
SET "EXT=*.bat"
SET "Count=0"
Set "Word2Search=Person"
SETLOCAL enabledelayedexpansion
REM Iterates throw the files on this current folder.
REM And Populate the array with existent files in folder
FOR /f "delims=" %%f IN ('dir /b /s "%ROOT%\%EXT%"') DO (
    find /I "%Word2Search%" "%%f" >nul 2>&1  && (
    SET /a "Count+=1"
    set "list[!Count!]=%%~nxf"
    set "listpath[!Count!]=%%~dpFf"
    ) || (
        Call :Scanning
    )
)

echo wscript.echo Len("%ROOT%"^) + 20 >"%tmp%\length.vbs"
for /f %%a in ('Cscript /nologo "%tmp%\length.vbs"') do ( set "cols=%%a")
If %cols% LSS 50 set /a cols=%cols% + 15
set Files=%Count%
set /a lines=%Count% + 10
Mode con cols=%cols% lines=%lines%
ECHO  *******************************************************
ECHO   Folder : "%ROOT%"
ECHO  *******************************************************
echo(
rem Display array elements
for /L %%i in (1,1,%Files%) do echo [%%i] : !list[%%i]!

SET /a "COUNT_TOT=%Count%"
ECHO.
ECHO Total of [%EXT%] files(s) : %Count% file(s)
echo(
echo Type the number of what file did you want to edit ?
set /p "Input="
set "sublimeEXE=%programfiles%\Sublime Text 3\sublime_text.exe"
For /L %%i in (1,1,%Count%) Do (
    If "%INPUT%" EQU "%%i" (
        Rem Testing if sublime_text.exe exist to open with it the text file
        If Exist "%sublimeEXE%" (
            Start "Sublime" "%sublimeEXE%" "!listpath[%%i]!"
            Rem Otherwise we open the text file with defalut application like notepad
            ) else (
            Start "" Notepad.exe "!listpath[%%i]!"
        )   
    )
)   
EndLocal
Goto:MenuLoop 
:Scanning
mode con cols=75 lines=3
Cls & Color 0A
echo(
echo                             Scanning in progress ...
goto :eof
Community
  • 1
  • 1
Hackoo
  • 18,337
  • 3
  • 40
  • 70