如何在一次命令行参数dos批处理中写入文件夹的多个路径和文件

How to write multiple path and file of a folder in once command line argument dos batch

在 windows 7 x64 上,带有批处理文件 (*.bat):

我需要检查文件夹中的最后一个文件(少于 5 天)和路径以作为参数添加到 Mycommand 中。

我需要我想:

"FORFILES -5" 也许 "Call"

我开始于:

@echo off
REM

set folder="D:\temp"
set vararg="-a"
cd /d %folder%
for /F "delims=" %%i in ('dir /b') do (D:\stockage\Mycommand %vararg% %folder%\"%%i" -v > c:\temp\test_log.txt)

如何输出:

D:\stockage\Mycommand -a D:\temp\file.jpg -a D:\temp\file2.jpg -a D:\temp\file3.jpg等...

不要放置不存在的文件或空白参数,我想我必须循环参数“-a D:\temp\filexxx.jpg”,但我不知道该怎么做。

并且如果可能的话转义超过 x 个字符或 space 的文件名问题。

你能举个例子吗?

谢谢。

echo 产生一个换行符,你无法阻止它这样做。或者,首先 assemble 完整的行:

setlocal enabledelayedexpansion
set commandline="D:\stockage\Mycommand"
set maxfiles=5
for /f "delims=" %%i in ('dir /b /o-d') do (
  set /a maxfiles-=1
  set commandline=!commandline! -a "%%i"
  if !maxfiles! leq 0 goto :enough
)
:enough
echo %commandline%

这会为您提供最新的 5 个文件(或更少,如果文件夹中没有足够的文件)

编辑 以颠倒参数的顺序:

setlocal enabledelayedexpansion
set "commandline="
set maxfiles=5
for /f "delims=" %%i in ('dir /b /o-d') do (
  set /a maxfiles-=1
  set commandline=-a "%%i" !commandline!
  if !maxfiles! leq 0 goto :enough
)
:enough
set commandline="D:\stockage\Mycommand" %commandline%
echo %commandline%