2016-08-24 73 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 
相關問題