Computing.Net > Forums > Programming > Compare two text files with spaces

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

Compare two text files with spaces

Reply to Message Icon

Name: PM_RTF
Date: August 25, 2009 at 18:57:10 Pacific
OS: Windows XP
Subcategory: Batch
Comment:

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



Sponsored Link
Ads by Google

Response Number 1
Name: Mechanix2Go
Date: August 27, 2009 at 06:55:58 Pacific
Reply:

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


0

Response Number 2
Name: PM_RTF
Date: August 27, 2009 at 17:51:55 Pacific
Reply:

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.


0

Response Number 3
Name: PM_RTF
Date: August 27, 2009 at 22:43:29 Pacific
Reply:

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


0

Response Number 4
Name: Mechanix2Go
Date: August 28, 2009 at 04:42:44 Pacific
Reply:

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


0

Response Number 5
Name: Judago
Date: August 28, 2009 at 04:50:54 Pacific
Reply:

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.


0

Related Posts

See More



Response Number 6
Name: Mechanix2Go
Date: August 28, 2009 at 06:35:19 Pacific
Reply:

Hi Judago,

yep


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

M2


0

Response Number 7
Name: PM_RTF
Date: August 30, 2009 at 17:44:59 Pacific
Reply:

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


0

Response Number 8
Name: Mechanix2Go
Date: August 30, 2009 at 20:27:27 Pacific
Reply:

Try using this in sub:

"%*"


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

M2


0

Response Number 9
Name: PM_RTF
Date: August 30, 2009 at 23:24:19 Pacific
Reply:

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


0

Response Number 10
Name: Mechanix2Go
Date: August 31, 2009 at 03:28:09 Pacific
Reply:

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


0

Response Number 11
Name: PM_RTF
Date: August 31, 2009 at 15:47:03 Pacific
Reply:

I'll have a go...

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


0

Sponsored Link
Ads by Google
Reply to Message Icon






Post Locked

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.


Go to Programming Forum Home


Sponsored links

Ads by Google


Results for: Compare two text files with spaces

VB Script Compare two Text files for Duplicat www.computing.net/answers/programming/vb-script-compare-two-text-files-for-duplicat/20247.html

Merging two text files, alternating order. www.computing.net/answers/programming/merging-two-text-files-alternating-order/19292.html

Compare two text files, output diff to 3rd www.computing.net/answers/programming/compare-two-text-files-output-diff-to-3rd/19516.html