【问题标题】:for statement/file processing in batch script用于批处理脚本中的语句/文件处理
【发布时间】:2014-06-14 14:34:52
【问题描述】:

以此为例 For statement in batch script from SO

尝试了以下

for %%f in (c:\username\desktop\Test\*.log) do (
    echo %%~nf
)

我收到以下错误:%%f was unexpected at this time

我的目标是将 [n] 个文件从一个目录移动到另一个目录,等待 [x] 秒,然后移动下 [n] 个文件

我可以通过对未知站点执行 ping 并强制延迟或使用超时来等待时间 [尚未测试,因为这需要在没有任何用户干预的情况下完成]

如果有更好的方法来做到这一点,我很开放,因为这是我第一次尝试这种类型的脚本。

【问题讨论】:

  • 您的问题“%%f was unexpected...”是由于尝试直接从提示符执行批处理命令引起的。您需要将文本保存在名为 somethingorother.bat 的文件中 - .bat 是关键。然后通过在提示符处输入 somethingorother 来执行 somethingorother.bat;然后选择并运行 .bat 文件。当然比永远重新输入命令行更容易。如果您明确希望直接从提示符运行,请将 元变量(循环控制变量)中的任何 %%x 减少为 %x

标签: windows batch-file command-line


【解决方案1】:

试试这个:

@echo off
setlocal EnableDelayedExpansion

::The number of files to copy
set [n]=10
::The time to wait after copying [n] files
set $tempo=5
::Your source path
set $sourceDir="C:\your\source\path"
::Your destination path
set $DestDir="C:\your\destination\path"

set $c=1
for /f "delims=" %%f in ('dir /b/a-d %$SourceDir% *.log') do (
    if !$c! equ %[n]% (
        timeout %$tempo%
        set $c=1
    )
    copy "%$SourceDir:~1,-1%\%%~nxf" %$Destdir%
    set /a $c+=1
)
echo Done...

假设要测试的扩展名是.log。如果不是,您必须更改它。

【讨论】:

  • 我尝试了这个建议,但是它也选择了非 *.log 文件。
猜你喜欢
  • 2012-05-11
  • 2013-05-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-12-27
相关资源
最近更新 更多