如何制作循环N个文件或整个文件夹的批处理文件?

时间:2016-08-08 00:35:10

标签: batch-file

我需要制作一个以这种方式工作的Windows批处理:

  • 您可以拖动 X 文件或
  • 您可以在其上拖动包含 X 文件
  • 的文件夹

批处理将循环播放所有文件并写入一个文件(在传递的文件夹中或第一个传递的文件的文件夹中),其中包含以下内容:

select ?subject (count(?object) as ?numObjects) {
    ?subject ?predicate ?object
}
group by ?subject

我在这里找到了关于计算和编写文件的各种示例,但我无法管理变量数量或参数类型。

有可能吗? 如果没有,我会很高兴有两个单独的批处理文件,一个用于文件,一个用于文件夹,但单个文件是最好的。 有人可以举个例子吗?

4 个答案:

答案 0 :(得分:2)

此批处理文件可以执行您想要的操作:

@echo off
setlocal EnableDelayedExpansion

set "list=%*"
for %%a in (%1) do set "attr=%%~Aa" & set "dir=%%~DPa"
if "%attr:~0,1%" equ "d" set "list=*.*" & set "dir=%1"
cd "%dir%"
set "i=0"
(for %%a in (%list%) do (
   set /A i+=1
   echo file!i!=%%~NXa
)) > aFile.txt

答案 1 :(得分:1)

在这里,我想向您展示一种处理批处理文件的多个文件和目录参数的方法(通过命令行或通过拖放方式提供)。对于每个提供的目录参数,枚举其内容;如果将变量RECURSIVE设置为非空值,则以递归方式处理目录:

@echo off
rem /* Define recursive mode globally: */
set "RECURSIVE="

rem // Loop through all arguments:
for %%A in (%*) do (
    rem // Call sub-routine for each argument:
    if not "%%~A"=="" call :RESOLVE "%%~fA"
)
pause
exit /B

:RESOLVE
rem // Check if item is file or directory:
set "ATTR=%~a1"
if not "%ATTR:~,1%"=="d" (
    rem // Process a single file:
    call :PROCESS "%~1"
) else (
    rem // Check for recursive mode:
    if not defined RECURSIVE (
        rem // Process all files in a directory:
        for %%B in ("%~1\*.*") do (
            call :PROCESS "%%~B"
        )
    ) else (
        rem // Process all files in a directory recursively:
        for /R "%~1" %%B in ("*.*") do (
            call :PROCESS "%%~B"
        )
    )
)
exit /B

:PROCESS
rem // Process a single file (simply echo it):
echo %~1
exit /B

要将此应用于您的任务,您可以使用以下脚本:

@echo off
rem /* Define recursive mode globally: */
set "RECURSIVE=" & rem // (empty means off, so non-recursive)
rem /* Define list file name: */
set "LISTFILE=%~n0.lst" & rem // (use batch script name + `.lst`)

rem // Determine target location, depending on first argument:
set "ATTR=%~a1"
if defined ATTR (
    if "%ATTR:~,1%"=="d" (
        set "LOCATION=%~f1\%LISTFILE%"
    ) else (
        set "LOCATION=%~dp1%LISTFILE%"
    )
)

set /A "COUNTER=0"
rem // Loop through all arguments:
for %%A in (%*) do (
    rem // Call sub-routine for each argument:
    if not "%%~A"=="" call :RESOLVE "%%~fA"
)
exit /B

:RESOLVE
rem // Check if item is file or directory:
set "ATTR=%~a1"
if not "%ATTR:~,1%"=="d" (
    rem // Process a single file:
    call :PROCESS "%~1"
) else (
    rem // Check for recursive mode:
    if not defined RECURSIVE (
        rem // Process all files in a directory:
        for %%B in ("%~1\*.*") do (
            call :PROCESS "%%~B"
        )
    ) else (
        rem // Process all files in a directory recursively:
        for /R "%~1" %%B in ("*.*") do (
            call :PROCESS "%%~B"
        )
    )
)
exit /B

:PROCESS
rem // Process a single file:
set /A "COUNTER+=1"
if %COUNTER% EQU 1 (
    >  "%LOCATION%" echo file%COUNTER%=%~nx1
) else (
    >> "%LOCATION%" echo file%COUNTER%=%~nx1
)
exit /B

答案 2 :(得分:0)

floop1.bat为目录中的(* .ext)文件的每个实例运行floop2.bat。

floop1.bat:

for %%f in (*.ext) do @call floop2.bat "%%f"

floop2.bat:

@echo %1

答案 3 :(得分:0)

将此脚本另存为 test.bat ,从其文件夹中打开 Cmd Prompt ,然后以目标文件夹路径作为参数运行批处理。它会将目标文件夹中存储的所有文件的名称保存到 files.txt

@echo off
set "dir=%~1"
pushd "%dir%"
For /R "%dir%" %%G IN (*.*) do (
 echo %%G >> files.txt )
popd
exit /b

c:\\batches\test.bat "K:\Files"

您可以将以上命令添加到批处理桌面快捷方式 属性 - 目标字段,因此下次双击快捷方式时,批处理将会运行。但是,我怀疑Cmd.exe支持拖放操作。 :)