Click here to Skip to main content
14,665,569 members
Rate this:
Please Sign up or sign in to vote.
I have a batch file that is scheduled to run after every half hour in the windows scheduler. The batch file will create a text file and it will write some data into a text file. In that, I want to run a command line to count the number of folders and write that count into the text file.

In the given code which I tried, it's not getting the desired result. I want the value of folder count in front of the "FolderCount Value" field mentioned in the code.
My desired output in demo.txt

# Title to be displayed
# Purpose to count the directories in a folder
# FolderCount Value 202456

Can anyone please help me?

What I have tried:


@echo # Title to be displayed > c:\demo.txt
@echo # Purpose to count the directories in a folder >> c:\demo.txt
@echo # FolderCount Value >> dir /b /ad "c:\demo" | find /c /v"" >> c:\demo.txt  
Updated 11-May-20 22:21pm

1 solution

Rate this:
Please Sign up or sign in to vote.

Solution 1

Try this :
@echo off
echo # Title to be displayed
echo # Purpose to count the directories in a folder
rem save the count in a variable to use below
dir /b /ad "C:\Users\rjmac\Documents\Code" | find /c /v"" > zz
set /p count=<zz
echo # FolderCount Value = %count%
webmail123 12-May-20 9:31am
Thanks Richard. I tried this one. But the thing is, it writes count to the next line. I want the count on the same line in front of "# FolderCount Value".
Any suggestions?
Richard MacCutchan 12-May-20 9:48am
Unfortunately the echo command always adds a newline character at the end. I do not know any way to prevent that.
Richard MacCutchan 12-May-20 10:08am
I found a way, see my updated solution.
webmail123 15-Jun-20 18:53pm
That's Great. Sorry for the late reply but it worked. Thanks.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100