Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6840699
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T23:52:32+00:00 2026-05-26T23:52:32+00:00

In a batch file, I am trying to fetch the output of a command

  • 0

In a batch file, I am trying to fetch the output of a command and save it to a variable.

The goal of my command is to count the number of folders in a certain folder.

  • I can’t use the trick provided in this accepted answer because I would have to do cd path\to\my\folder to get to the current directory. Unfortunately, I can’t do this command because path\to\my\folder is in fact a UNC path (\\path\to\my\folder), and cd \\some\UNC\path is not supported by the windows cmd.
  • I am aware of this answer but I don’t want to use a temporary file.

So I tried to do the following:

  • To obtain the number of folders, I use:

    dir \\path\to\my\folder | find /c "<REP>"
    

    This works fine and returning me a number as I would expect.

  • To retrieve the output of this command in a batch variable, I tried:

    FOR /F "TOKENS=*" %%i IN ('\\path\to\my\folder | find /c "<REP>"') DO 
    SET value = %%i
    

    But without success, the error message being…

    | was unexpected.

    …when I execute the batch file and…

    %%i was unexpected.

    when I try to execute the command directly in a command window. I tried to escape the quotes around the <REP> string (...find /c ""<REP>""') DO...), got me the same error.

Am I on the right path to retrieve the output in a variable? What should I do to resolve the error message?
Or maybe there is a simpler way to set a command output in a variable?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T23:52:33+00:00Added an answer on May 26, 2026 at 11:52 pm

    You can use the answer you first mentioned. You don’t have to cd there, but you can use pushd which will allocate a temporary drive letter for UNC paths which will be released when you do a popd.

    So in essence:

    pushd \\server\path
    set count=0
    for /d %%x in (*) do set /a count+=1
    popd
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to write a batch file so I can use TortoiseHg Annotate
I am trying to create a batch file that removes tel: from a number
I'm trying to write a batch file to xcopy a folder to a removable
I'm trying to make a batch file that executes a simple command: shutdown -h
I'm trying to write a windows batch file that can delete files from subdirectories.
I'm trying to build a generic batch file that can tell errors with a
I'm trying to run a batch file that exists in one folder, from a
I'm trying to run a batch file through a folder and delete files if
I'm trying to set up a batch file to automatically deploy a php app
i am trying to delete some files using a batch file.. (winxp) my problem

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.