none
Unable to run a command from .bat RRS feed

  • Question

  • Hello

    I created a batch file, but the lines below do not work.

    If i open CMD and enter *** for /F tokens=1,2 delims= " %G in ('cmdkey /list ^| findstr Target') do  cmdkey /delete %H *** It works without issues.

    But if I use the .BAT script, it does not work.

    @echo off
    ECHO Cleaning Credential Manager
    for /F tokens=1,2 delims= " %G in ('cmdkey /list ^| findstr Target') do  cmdkey /delete %H
    PAUSE
    
    I get the following message "( was unexpected at this time."

    Monday, August 12, 2019 8:45 PM

Answers

  • Thank you JRV.

    I resolved the issue, i think i was using the wrong " "

    I am going to try your way, but it is working for now, I even added to reopen outlook and self remove after the batch file has run :D

    @echo off
    ECHO Cleaning Credential Manager
    ECHO ===========================
    TASKKILL /F /IM OUTLOOK.EXE
    TIMEOUT 2
    FOR /F "tokens=1,2 delims= " %%G in ('cmdkey /list ^| findstr Target') do  cmdkey /delete %%H
    TIMEOUT 2
    ECHO ===============================
    ECHO === MS Outlook is reopening ===
    ECHO ===============================
    START C:\"Program Files (x86)"\"Microsoft Office"\root\Office16\OUTLOOK.EXE
    PAUSE
    ECHO "This script will now self-destruct. Please ignore the next error message"
    DEL "%~f0"

    • Marked as answer by mcubac Tuesday, August 13, 2019 2:03 AM
    Tuesday, August 13, 2019 2:02 AM

All replies

  • Read the for /? help carefully. There's a sentence in there:

    To use the FOR command in a batch program, specify %%variable instead of %variable.

    -- Bill Stewart [Bill_Stewart]

    Monday, August 12, 2019 9:05 PM
    Moderator
  • I am still getting the same error adding %%

    Now the error is "1 was unexpected at this time."

    @echo off
    ECHO Cleaning Credential Manager
    for /F tokens=1,2 delims= " %%G in ('cmdkey /list ^| findstr Target') do  cmdkey /delete %H
    PAUSE

    Tuesday, August 13, 2019 1:30 AM
  • Now, after adding " " in "tokens=1,2" i got the following, i guess i am getting close


    delims was unexpected at this time.

    Tuesday, August 13, 2019 1:34 AM
  • This will be easier to manage:

    cmdkey /list | 
        Where{$_ -match 'Target:'} | 
        ForEach{
            cmdkey /delete ($_ -replace 'Target:')
        }
    


    \_(ツ)_/

    Tuesday, August 13, 2019 1:46 AM
  • Thank you JRV.

    I resolved the issue, i think i was using the wrong " "

    I am going to try your way, but it is working for now, I even added to reopen outlook and self remove after the batch file has run :D

    @echo off
    ECHO Cleaning Credential Manager
    ECHO ===========================
    TASKKILL /F /IM OUTLOOK.EXE
    TIMEOUT 2
    FOR /F "tokens=1,2 delims= " %%G in ('cmdkey /list ^| findstr Target') do  cmdkey /delete %%H
    TIMEOUT 2
    ECHO ===============================
    ECHO === MS Outlook is reopening ===
    ECHO ===============================
    START C:\"Program Files (x86)"\"Microsoft Office"\root\Office16\OUTLOOK.EXE
    PAUSE
    ECHO "This script will now self-destruct. Please ignore the next error message"
    DEL "%~f0"

    • Marked as answer by mcubac Tuesday, August 13, 2019 2:03 AM
    Tuesday, August 13, 2019 2:02 AM
  • 1. I definitely do not recommend killing Outlook.exe without closing it properly. This can cause data corruption (and has, in numerous cases that I have seen).

    2. There is no need to individually quote the elements in a path that contain spaces:

    START C:\"Program Files (x86)"\"Microsoft Office"\root\Office16\OUTLOOK.EXE

    All you need is to quote the entire path, including the spaces:

    START "" "C:\Program Files (x86)\Microsoft Office\root\Office16\OUTLOOK.EXE"

    (The first set of empty quotes are required by the start command because, it interprets the first quoted argument on its command line as a window title, which is not used with a GUI program. Including them in this way conforms to the start command's syntax description and reduces confusion.)

    3. This script will fail if the Office installation is not in that exact path. It is not recommended to hard-code the path in this way.


    -- Bill Stewart [Bill_Stewart]

    Wednesday, August 14, 2019 2:47 PM
    Moderator