Arrays 如何扫描文件夹并将所有文件名存储在数组变量中,然后在数组中循环?
我是批处理新手,我正在尝试执行以下操作:Arrays 如何扫描文件夹并将所有文件名存储在数组变量中,然后在数组中循环?,arrays,file,batch-file,if-statement,cmd,Arrays,File,Batch File,If Statement,Cmd,我是批处理新手,我正在尝试执行以下操作: 读取或扫描文件夹 在数组变量中保存文件夹中的所有文件名(需要保存带或不带扩展名的文件名) 循环遍历该数组,并使用IF或CASE语句条件根据文件类型创建对特定文件/bat的调用。示例:如果文件名中包含单词person,请调用特定的文件/bat 这就是我到目前为止所做的: @echo off setlocal EnableDelayedExpansion rem Populate the array with existent files in fo
- 读取或扫描文件夹
- 在数组变量中保存文件夹中的所有文件名(需要保存带或不带扩展名的文件名)
- 循环遍历该数组,并使用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]!
回声!列表[%%i]!|查找/i“person”:查找单词>nul
:忽略输出(我们不需要它,只需要errorlevel)&&
:如果上一个命令成功(找到了单词),则
你真的需要那个阵列吗?您可以“即时”完成:
仅对于文件名,请使用%%~nb
;对于全名(包括路径),请使用%%~fb
(有关更多选项,请参阅获取/?
)
回声!列表[%%i]!|查找/i“person”:查找单词>nul
:忽略输出(我们不需要它,只需要errorlevel)&&
:如果上一个命令成功(找到了单词),则
你真的需要那个阵列吗?您可以“即时”完成:
仅对于文件名,请使用
%%~nb
;对于全名(包括路径),请使用%%~fb
(有关更多选项,请参阅获取/?
)这是一个快速修改的版本,来自:
它将在您的桌面上扫描包含单词: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
这是一个快速修改的版本,来自: 它将在您的桌面上扫描包含单词: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
请用正确的格式书写。你可以看看这个和这个==>请用正确的格式书写。你可以看看这个和这个==>
@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