Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

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
pause
exit

:news
CALL :save
echo news
pause
exit

:: Subs

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

Edit:

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

.
.
.

:TRIM
SET %2=%1
GOTO :EOF
share|improve this question

2 Answers 2

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
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:

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

Result:

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 ==

see: http://ss64.com/nt/if.html

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
1  
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

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.