Compare two text files with spaces

August 25, 2009 at 18:57:10
Specs: Windows XP
Hi

I have a problem with a script I'm trying to
write. I want to compare two text files which
have a list of directories and then extract the
added (new) or missing (deleted) ones. The
issue I have seems to be extracting long folder
names that have spaces in them. I've tried
using FC but it doesn't seem to work too
efficiently (i.e. results with folders above or
below new folders).

The two text files to be compared are:
<Previous.txt>
ABC
DEF
Long Folder Name 1
LongFolderName2
TEST1
TEST 2

and <Current.txt>
ABC
DEF
NEW1
LongFolderName2
Long Folder Name 3
TEST1
TEST 2

Here's my faulty script:
@ECHO OFF
:: This script compares two text files which
contain a list of folders.
:: Any new folders that are found or old folders
found to be deleted
:: are saved to temporary text files.

:: The two files to be compared.
SET PREVFOLDERLIST=Previous.txt
SET NEWFOLDERLIST=Current.txt
:: Two created files showing new or deleted
folders
SET NEWDIRLIST=NewDirList.txt
SET DELDIRLIST=DelDirList.txt
ECHO ; NEW folders found list >
%NEWDIRLIST%
ECHO ; DELETED folders found list >
%DELDIRLIST%
ECHO.

:: Check if any new directories created
:FINDNEW
:: This subroutine extracts each CURRENT
folder name
SET /A FOUNDCOUNT=0
for /F "tokens=*" %%A in
(%NEWFOLDERLIST%) do (
ECHO %NEWFOLDERLIST% Output is %%A
CALL :CURR_PREV %%A
)
GOTO FINDDEL

:CURR_PREV
:: Checking current folders to previous folders
to find any new directories.
IF %1=="" GOTO :EOF
SET COMPDIR=%1
:: Below records the number of folder
instances found.
Echo Will look for "%COMPDIR%" in
%PREVFOLDERLIST%
FIND /C "%COMPDIR%"
%PREVFOLDERLIST% > delsoon1.txt
FOR /F "eol=; tokens=2,3* delims=: " %%i in
(delsoon1.txt) do (SET /A
FOUNDCOUNT=%%j)
IF %FOUNDCOUNT% GEQ 1 (ECHO
%COMPDIR% existed before > NUL) ELSE
(ECHO %COMPDIR% >> %NEWDIRLIST%)
GOTO :EOF

:FINDDEL
:: This subroutine extracts each PREVIOUS
folder name
SET COMPDIR=NIL
SET /A FOUNDCOUNT=0
for /F "tokens=*" %%A in
(%PREVFOLDERLIST%) do (
ECHO %PREVFOLDERLIST% Output is
%%A
CALL :PREV_CURR %%A
)
GOTO END

:PREV_CURR
:: Checking previous folders to current folders
to find any deleted directories.
IF %1=="" GOTO :EOF
SET COMPDIR=%1
:: Below records the number of folder
instances found.
FIND /C "%COMPDIR%"
%NEWFOLDERLIST% > delsoon2.txt
FOR /F "eol=; tokens=2,3* delims=: " %%i in
(delsoon2.txt) do (SET /A
FOUNDCOUNT=%%j)
IF %FOUNDCOUNT% GEQ 1 (ECHO
%COMPDIR% still exists > NUL) ELSE
(ECHO %COMPDIR% >> %DELDIRLIST%)
GOTO :EOF

:END
Echo New folders found are:
type %NEWDIRLIST%

Echo Deleted folders found are:
type %DELDIRLIST%


The result I'm after is:

New folders found are:
; NEW folders found list
NEW1
Long Folder Name 3
Deleted folders found are:
; DELETED folders found list
Long Folder Name 1

Which I'm not getting.

Hoping someone can fix my script.

Thanks


See More: Compare two text files with spaces

Report •


#1
August 27, 2009 at 06:55:58
:: To get the lines from current.txt which are not in previous.txt
:: into newfile
:: currprev.bat Thu 27-08-2009 20:51:51.29

@echo off > newfile & setLocal enableDELAYedexpansion

for /f "tokens=* delims= " %%a in (current.txt) do (
find "%%a" < previous.txt > nul
if errorlevel 1 echo %%a >> newfile
)


=====================================
If at first you don't succeed, you're about average.

M2


Report •

#2
August 27, 2009 at 17:51:55
Thanks M2. This works well. I'm adding this subroutine to a
larger script which gets folder sizes, etc. I'll come back for more
help if I hit another snag.

Report •

#3
August 27, 2009 at 22:43:29
I now need to parse each line of the newfile (%NEWvOLD%=NEWvOLD.txt) in a subroutine to calculate the size of that folder. However when I
call the size routine, the variable is shortened to the first word if it has spaces in its name
i.e. "Test Folder 4" gets passed as "Test"
How can I overcome this?
...
for /f "tokens=* delims= " %%a in (%NEWvOLD%) do (
call :sizedir %%a
)
:SIZEDIR
IF "%1"=="" GOTO :EOF
SET EXTDIR=%1
ECHO About to check size of %ROOTDIR%\%EXTDIR%
CALL NewCalc.bat
ECHO New root subfolder %EXTDIR% is %MBSIZE%MB in
size.
GOTO :EOF
....
%ROOTDIR%\%EXTDIR% shows as C:\Test when it should
be C:\Test Folder 4

Thanks


Report •

Related Solutions

#4
August 28, 2009 at 04:42:44
In this section:
=======================
call :sizedir %%a
)
:SIZEDIR
IF "%1"=="" GOTO :EOF
SET EXTDIR=%1
=====================
Keep in mind that the subroutine, just like a separate bat, Takes parameters as defined by 'white space' delimiters.

So if %%a is:

many mo jack

then %1 is: many


=====================================
If at first you don't succeed, you're about average.

M2


Report •

#5
August 28, 2009 at 04:50:54
Hi M2,

Just to add to you description(not to step on your toes of course) the delimiters can also be "="(equals), ";"(semi-colon), ","(comma) or a tab in addition to spaces, a standard for loop can also use any of them.


Report •

#6
August 28, 2009 at 06:35:19
Hi Judago,

yep


=====================================
If at first you don't succeed, you're about average.

M2


Report •

#7
August 30, 2009 at 17:44:59
So... could I add quotation marks around the path (variable),
pass this to the subroutine, strip off any quotation marks (if
any) in that subroutine, then do the size calculation.

NewCalc.bat uses DIR /A /S %ROOTDIR%\%SUBDIR% for
its calculation, so I need for the following to happen for
example:

1. Test Folder 4 is found to be a new directory.
2. Convert to variable SET EXTDIR="Test Folder 4"
3. Run subroutine NewCalc.bat which converts "Test Folder 4"
to "C:\Test Folder 4" (i.e. "%ROOTDIR%\%SUBDIR%")
4. Size calculation is done and ECHO Size of %EXTDIR% =
nMB

Any good way to do this?

Another option might be to break up a string with spaces into
multiple strings and then join them back together again
(hoping that there were no double spaces in the original
name).

I found the following script to trim quotes:
Trim Quotes - Remove surrounding quotes via FOR command
set str="name with spaces"
echo.%str%
for /f "useback tokens=*" %%a in ('%str%') do set str=%%~a
echo.%str%

Becoming a puzzle this one...


Report •

#8
August 30, 2009 at 20:27:27
Try using this in sub:

"%*"


=====================================
If at first you don't succeed, you're about average.

M2


Report •

#9
August 30, 2009 at 23:24:19
Yes, this worked. Was now able to size a folder with spaces
in the name. Will see how the rest of my script runs now.

BTW the 'called' subscript NewCalc.bat creates a variable
%MBSIZE% however I'm not sure how to pass this back to
the main script - an echo of this variable afterwards is blank.

I'm getting around this by writing %MBSIZE% to a file and
then reading that file, however a variable that I can pass back
would be more efficient. Any ideas?

Cheers


Report •

#10
August 31, 2009 at 03:28:09
My brain is cooked from running errands this PM in the scorching sun. LOL

But try using the var as !MBSIZE! instead of %MBSIZE%.


=====================================
If at first you don't succeed, you're about average.

M2


Report •

#11
August 31, 2009 at 15:47:03
I'll have a go...

You'll make a decent programmer out of me yet!


Report •


Ask Question