Batch For loop

Posted on 2010-01-07
Last Modified: 2012-05-08
I am trying to do a simple batch FOR loop, but I can't figure out how to use a | in the command.

For instance,

FOR /F %G IN ('dir /b') DO echo %G    works


FOR /F %G IN ('dir /b | findstr /v xml') DO echo %G    doesn't not work
(| was unexpected at this time.)

I need to do a directory listing but exclude at least one specific file from this loop.  If I run: (dir /b | findstr /v xml) then I get the desired list of files; however, I can't figure out how to use it in the FOR loop
Question by:xiaoyunwu
    1 Comment
    LVL 95

    Accepted Solution

    Escape it with ^
    FOR /F %G IN ('dir /b ^| findstr /v xml') DO echo %G

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Why You Should Analyze Threat Actor TTPs

    After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

    TOMORROW TOMORROW.BAT is inspired by a question I get asked over and over again; that is, "How can I use batch file commands to obtain tomorrow's date?" The crux of this batch file revolves around the XCOPY command - a technique I discovered w…
    When you receive another warning that your shared drive is almost full and you have asked your users to clean out old files again and again, here is a single command that may help. This command will place all the files that have not been used rec…
    This video is in connection to the article "The case of a missing mobile phone (". It will help one to understand clearly the steps to track a lost android phone.
    Sending a Secure fax is easy with eFax Corporate ( First, Just open a new email message.  In the To field, type your recipient's fax number You can even send a secure international fax — just include t…

    760 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    7 Experts available now in Live!

    Get 1:1 Help Now