【问题标题】:FOR /F from command, command is not recognized as an internal or external commandFOR /F from command, command 不被识别为内部或外部命令
【发布时间】:2018-02-05 14:43:53
【问题描述】:

我写了一个在文件名流中循环的简单脚本,我得到了 从 dir 命令通过管道传输,后跟此 url For /F command:

FOR /F "tokens=*" %%G IN ('dir /b ^"???? *.sql^"') do (
    call :runscript "%%G" %dbname% %op% %dbinstance%
    rem stop further processing if execution failed and return error code
    if errorlevel 1 exit /b 1
)

不幸的是,我收到了这个错误:

错误 'dir /b "???? *.sql"' 不被识别为内部或 外部命令、可运行程序或批处理文件。

【问题讨论】:

  • ^ 左移至"。仅|&> 必须在 FOR 在以cmd.exe /C 和指定命令行启动的后台进程中执行的 DIR 命令行中进行转义.
  • 在这种情况下不需要使用FOR /F。您可以只使用基本的FOR 命令。 FOR %%G IN ("???? *.sql") do (

标签: batch-file for-loop cmd directory


【解决方案1】:

简单示例

@echo. > ..\Downloads\Tables.sql
@echo. > ..\Downloads\Columns.sql
dir ..\Downloads\*.sql
 Volume in drive C is Windows

Directory of C:\Users\anorh\Downloads

2018-02-17  06:01 PM                 3 Columns.sql    
2018-02-17  06:01 PM                 3 Tables.sql

               2 File(s)              6 bytes
               0 Dir(s)  327,567,941,632 bytes free

使用“for”命令语法处理文件(因此括号中的值是路径和过滤器)。

for %A in (..\Downloads\*.sql) do ( @echo %~fA )
C:\Users\anorh\Downloads\Columns.sql
C:\Users\anorh\Downloads\Tables.sql

用于主要在文件中工作的语法(但在这种情况下,文件内容是命令的输出)。

for /f "usebackq tokens=1* delims=" %A in (`dir /ON /B C:\Users\anorh\Downloads\*.sql`) do ( @echo %~fA )
C:\Users\anorh\Documents\Columns.sql
C:\Users\anorh\Documents\Tables.sql

【讨论】:

    猜你喜欢
    • 2020-01-13
    • 2021-09-15
    • 2021-02-08
    • 1970-01-01
    • 1970-01-01
    • 2014-10-21
    • 2014-02-08
    • 2017-08-22
    • 1970-01-01
    相关资源
    最近更新 更多