2016-08-24 75 views
1

我是新来的一批,我想做到以下几点:如何扫描文件夹并将所有文件名存储在数组变量中,然后遍历数组?

  • 读取或扫描的文件夹
  • 保持在文件夹中的所有文件名中的一个数组变量(需要带或不带扩展名持有文件名)
  • 循环访问该数组,并使用IF或CASE语句条件根据文件类型创建对特定文件/ bat的调用。例如:如果文件名中包含person一词,请调用特定文件/ bat。

这是我到目前为止有:

@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]! 
+0

请使用适当的格式编写。 –

+0

你可以看看这个[批量文件阵列创建/修改](http://stackoverflow.com/questions/38678498/batch-file-array-creation-modification/38680369#38680369) 这个也是= => [通过CMD打开一个文件,并显示在特定的编辑器中选择](http://stackoverflow.com/questions/38524510/open-a-file-through-cmd-and-display-the-selected-in-specific -editor/38525929#38525929) – Hackoo

回答

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

echo !list[%%i]! | find /i "person":查找单词
>nul:忽略输出(我们并不需要它,只是在错误级别)
&&:如果先前的命令成功(找到该单词),则...

你是真实的我需要那个数组吗?你可以这样做“对飞”:

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

为文件名只,用%%~nb,全名(包括路径),使用%%~fb(见for /?更多选项)

0

这是一个快速的修改版本来自:Open a file through cmd and display the selected in specific editor

它会扫描你的桌面上包含单词任何批处理文件:

@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 
相关问题