Click here to Skip to main content
15,074,668 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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.
e.g.
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 OFF

@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  
Posted
Updated 21-May-21 10:58am
v2

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%
   
v2
Comments
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.
You can get the value in a variable without creating/read a file and in a line..
@echo off

>"c:\demo.txt" ( 

    echo # Title to be displayed 
    echo # Purpose to count the directories in a folder

    for /f %%i in ('dir/b/ad "C:\Users\rjmac\Documents\Code"^|find/c /v ""')do set "count=%%~i"
    echo # FolderCount Value = %count%
)
   

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




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900