【问题标题】:How to scan a folder and store all file names in a array variable and then loop through the array?如何扫描文件夹并将所有文件名存储在数组变量中,然后循环遍历数组?
【发布时间】:2016-08-24 06:12:31
【问题描述】:

我是批处理新手,我正在尝试执行以下操作:

  • 读取或扫描文件夹
  • 将文件夹中的所有文件名保存在数组变量中(需要保存带扩展名或不带扩展名的文件名)
  • 循环遍历该数组并使用 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]!

【问题讨论】:

标签: arrays file batch-file if-statement cmd


【解决方案1】:
... 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 /?

【讨论】:

    【解决方案2】:

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

    它将在您的桌面上扫描任何包含以下单词的批处理文件: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
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多