批处理语法错误

时间:2012-05-04 14:23:34

标签: if-statement batch-file for-loop

我正在尝试编写一个批处理,将配置文件文件夹的名称复制到txt文件,然后使用它来获取每一行(配置文件)和查询广告。我似乎遇到的两个问题是:

  1. 它说我写for for loop
  2. 的方式有一个语法错误
  3. 如果我将其分解为调试,循环似乎只是反复设置变量,然后仅使用最终值来执行下标中的命令
  4. @echo off
    
    @setlocal enabledelayedexpansion
    pushd "c:\documents and settings"
    
    mkdir c:\pulls
    copy \\mint\testfolder\forfiles.exe c:\pulls\
    copy \\mint\testfolder\psexec.exe c:\pulls\
    rem call \\mint\testfolder\copy.bat c:\pulls\
    set queryid=
    set checkid=
    
    rem finds all profiles that have been logged into in the last 18 months
    
    c:\pulls\forfiles.exe /p "c:\documents and settings\" /d -540 /c "cmd /c if @ISdir==true deltree @isdir"
    
    dir/b > "c:\documents and settings\profiles.txt"
    
    %queryid% > "c:\documents and settings\tmp.txt"
    
    rem active directory query
    
    :adquery
    
    for /f  "tokens=*" %%n IN ("c:\documents and settings\profiles.txt") do (
        set queryid=%%n
        call :sub
    )
    
    
    goto :end
    
    :sub
    
    c:\pulls\psexec.exe \\computer-u domain\user -p password dsquery user -name %queryid%     > "c:\documents and settings\tmp.txt"
    set /p checkid= < "c:\documents and settings\tmp.txt"
    del "c:\documents and settings\tmp.txt"
    
    
    ::checks to see if variable is an empty string
    
    if [!%checkid%==!] goto :process1
    
    if [%checkid%==] %queryid% >> c:\documents and settings\final.txt
    
    goto :eof
    
    :process1
    
    c:\pulls\psexec.exe \\computer -u domain\user -p password dsquery user -name %queryid%     -desc *termed* > "c:\documents and settings\tmp.txt"
    set /p checkid= < "c:\documents and settings\tmp.txt"
    del "c:\documents and settings\tmp.txt"
    
    ::checks to see if variable is an empty string
    
    if [!%checkid%==!] goto :amending
    if [%checkid%==] goto :process2
    
    goto :eof
    
    :process2
    
    c:\pulls\psexec.exe \\computer -u domain\user -p password dsquery user -name %queryid%     -disabled > "c:\documents and settings\tmp.txt"
    set /p checkid= < "c:\documents and settings\tmp.txt"
    del "c:\documents and settings\tmp.txt"
    
    ::checks to see if variable is an empty string
    
    if [!%checkid%==!] goto :amending
    
    goto :eof
    
    :amending
    
    %checkid% >> "c:\documents and settings\final.txt"
    
     goto :eof
    
    :end
    
     notepad.exe "c:\documents and settings\final.txt"
    
    del c:\pulls /y
    
    del "c:\documents and settings\profiles.txt"
    
    del "c:\documents and settings\final.txt"
    
    rem copies selected profiles
    
    rem c:\pulls\copy.bat
    

1 个答案:

答案 0 :(得分:1)

我没有追查你的整个逻辑,看你是否还有其他问题。但我已经确定了你的FOR / F循环的问题。

您正在尝试读取文件,但IN()子句是双引号,因此它被解释为字符串而不是文件名。 (键入FOR /?以获取有关FOR命令的帮助)。您想使用双引号,因为您的路径包含空格,但您需要将其解释为文件名 - 这就是USEBACKQ选项的用途!

for /f  "usebackq tokens=*" %%n IN ("c:\documents and settings\profiles.txt") do ...