Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

The following script is supposed to check if the number of files and folders has changed since the last time the batch-file was run. If so, it prints a message and updates the counts.

Unfortunately it doesn't quite work. I think I have a problem with the if statements in my string comparisons.

Does anyone know how I can get the script to work?

@echo off

::Check number of dir's and files in last run    
set /p filecounta=<"countfile.log"
set /p dircounta=<"countdir.log"

::Check number number of dir's and files currently
for /f %%A in ('dir /a-d-s-h /b ^| find /v /c ""') do set filecountb=%%A
for /d %%G in (*) do set /a dircountb=dircountb+1

::Compares the number of counts in the past with the present
if not "%filecountb%"=="%filecounta%" goto :news
if not "%dircountb%"=="%dircounta%" goto :news

CALL :save
echo no news

CALL :save
echo news

:: Subs

:: Put number of dir and files in log file
echo %filecountb% >"countfile.log"
echo %dircountb% >"countdir.log"


After @Rik's suggestion I tried removing the space before > in
echo %filecountb%>"countfile.log" and echo %dircountb%>"countdir.log" but that didn't work in my OS. The solution was adding a TRIM-function to remove the spaces after reading them from the log-files.

::Check number of dir's and files in last run    
set /p filecounta=<"countfile.log"
set /p dircounta=<"countdir.log"
CALL :TRIM %filecounta% filecounta
CALL :TRIM %dircounta% dircounta


SET %2=%1
share|improve this question
up vote 2 down vote accepted

If you place an echo before the if statements you'll see where the error is:

echo if NOT "%filecountb%" == "%filecounta%" goto :news
echo if NOT "%dircountb%" == "%dircounta%" goto :news
if NOT "%filecountb%" == "%filecounta%" goto :news
if NOT "%dircountb%" == "%dircounta%" goto :news

This is the result i got with the echo:

if NOT "10" == "10 " goto :news
if NOT "22" == "22 " goto :news
Press any key to continue . . .

Your a-results have spaces behind them (the ones you read from the log-files).

If you change your save routine to echo the variable without the space before > then it will work:

echo %filecountb%>"countfile.log"
echo %dircountb%>"countdir.log"


if NOT "10" == "10" goto :news
if NOT "22" == "22" goto :news
no news
Press any key to continue . . .
share|improve this answer
edited 1 post with solution – Rui Branco Dec 14 '13 at 18:15
@RuiBranco Mmm, weird that removing the space before > didn't work for you (for me it does, under Windows 7). You do need to run it twice before you can see the effect because your existing .logs still had spaces in it. But your TRIM-function will work too. – Rik Dec 14 '13 at 18:41

Try NEQ rather than NOT and ==


share|improve this answer
The essential part I was attempting to pick out was to try NEQ rather than == – sahmeepee Dec 14 '13 at 12:38
Ah - sorry, my mistake. – ChrisF Dec 14 '13 at 12:40
No problem... My answer was admittedly too brief and I think Rik has nailed it anyway :/ – sahmeepee Dec 14 '13 at 12:43

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .