【问题标题】:How to write multiple path and file of a folder in once command line argument dos batch如何在一次命令行参数dos批处理中写入文件夹的多个路径和文件
【发布时间】:2015-12-28 14:54:26
【问题描述】:

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

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

我需要我想:

"FORFILES -5" 也许是“打电话”

我以:

开头
@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 字符或空间。

你能举个例子吗?

谢谢。

【问题讨论】:

  • 你写的是“最后一个文件”,但你的例子说三个文件加上“等等......”。您是指所有 5 天以下的文件吗?
  • 谢谢 Stephan 是的,我的意思是目录中最年轻的文件或在此目录中创建的最后文件。

标签: windows loops batch-file cmd arguments


【解决方案1】:

echo 产生一个换行,你不能阻止它这样做。作为替代方案,首先组装完整的生产线:

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%

【讨论】:

  • 工作正常,现在我需要以相反的顺序排序尝试 dir | sort /R 不适用于 forfiles /p path /m %extfilters% /d +%yLocal% 如何排序我有其他订单?我尝试设置 %%i=dir | sort /r > dir -s nothink 工作...
  • 它们按日期时间排序。这是获得最新的五个所必需的。如果你想颠倒最后五个的顺序,只需构建从尾部到鼻子的命令行。查看我的编辑。
猜你喜欢
  • 1970-01-01
  • 2011-04-08
  • 2012-01-27
  • 1970-01-01
  • 2011-05-17
  • 1970-01-01
  • 2023-03-11
  • 2011-12-15
  • 1970-01-01
相关资源
最近更新 更多