【问题标题】:return the name of a file using a batch script使用批处理脚本返回文件名
【发布时间】:2015-05-06 01:42:51
【问题描述】:

我正在尝试编写一个脚本,它将搜索给定目录,查找以 .ready_go 扩展名结尾的文件,将它们 rar(将 rar 保存为文件名.rar),然后移动到具有相同扩展名的下一个文件并做同样的事情。

我没有运气将变量“文件名”分配给循环中找到的文件名。此变量将用于命名 rar 存档

到目前为止,这是我所拥有的:

set dSource=C:\users\admin\desktop\one
set dTarget=\\filesrv1\archives
set fType=*.ready_go

for /f "delims=" %%f in ('dir /a-d /b /s "%dSource%\%fType%"') do (

    set filename=%%f
    echo %filename%
    echo %dsource%
    pause
)

它可以很好地回显 dsources 变量,但不会回显文件名变量。

我也尝试了“echo %%f”,但也没有用。

我们将不胜感激。谢谢。

【问题讨论】:

    标签: batch-file command-line cmd command-line-arguments command-prompt


    【解决方案1】:

    您需要使用延迟扩展,因为您要在循环内设置变量。

    试试这个:

    setlocal enabledelayedexpansion
    set dSource=C:\users\admin\desktop\one
    set dTarget=\\filesrv1\archives
    set fType=*.ready_go
    
    for /f "delims=" %%f in ('dir /a-d /b /s "%dSource%\%fType%"') do (
    
            set filename=%%f
            echo !filename!
            echo %dsource%
            pause
    )
    

    【讨论】:

      猜你喜欢
      • 2016-08-16
      • 2010-09-20
      • 1970-01-01
      • 1970-01-01
      • 2013-01-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多