Link to home
Start Free TrialLog in
Avatar of fboateng
fboateng

asked on

Compare a list of names a txt file to a list of folders in a directory

I have a file text file containing a list of names which corresponds to a list of folders in a directory.
Can someone please help me with a script that will compare the list of names in the txt file to the list of folders in the directory and out put the match to a new file.
I am using Windows. Thank you
Avatar of becraig
becraig
Flag of United States of America image

gc textfile.txt | % {if (Test-Path $_) {"$_ is a valid directory" | out-file report.txt -append}}

Open in new window


Something like above should work.

I will test.
Are the paths in the text file full paths?  Is it in the same format you get if you do
dir /s /ad C:\Folder > C:\Temp\Output.txt
If there a r names in the name list, N(n), and r folders in the folder list, F(f), a double IF  statement should do it
r = number of items
n = 0
f = 0
(1)     For n from 1 to r        n = n +1
              ntest(n) = N(n)
(2)                    for f from 1 to r      f = f + 1
                         if f > n go to (1)
                          ftest(f) = F(f)
                                 IF ntest(n) = ftest(n)
                                          print   ntest(n)
                                           else  go to (2)
That ought to do it. There are short cuts possible especially if the lists are alphabetized. Output and check statements are not included.
So based on my example I have two questions:

1. is the full path in your text file ?
(if not I will have to make a modification)
2. Will you be searching one drive of multiple drives based on the answer to the question above ?
Avatar of Bill Prew
Bill Prew

@fboateng,

We can keep trying to guess the details of what you need, but it would be a lot faster and more efficient if you could be more specific with what you need.  Here are some questions that would help move things along if you could answer:

(1) Are their subfolders with directories in them to be compared as well, or just the folders at the base level of the specified folder?

(2) Do the lines in the text file you have contain just the folder name, or the full path to the folder?

(3) There are three conditions that could exist in the situation you describe, which of these do you want reported on?

     (a) The folder in the text file DOES exist on disk
     (b) The folder in the text file DOES NOT exist on disk
     (c) A folder exists on the disk which is NOT listed in the text file

(4) Is there any particular format you want the output in?

~bp
Avatar of fboateng

ASKER

Hi, many thanks to you all for your help. as i mentioned in my original post, i am an absolute beginner to scripting so over the weekend I tried to write the code in Poweshell.
bilprew, The following is the exact senario;
I have a text file containing a list of names(e.g c:\Temp\foldernames.txt) which corresponds to a list of folders in a directory (e.g c:\Temp\gprfolders )
I need a script to compare the list of names in the text file to the list of folders in the directory, and, if a match if found, then move the folder to another folder (e.g c:\Temp\gprarchive) deleting from C:\Temp\gprfoldes.I am using Windows.
example the contents of C:\Temp\foldernames.txt are

1000037
1000038
1000039
1000041
1000045
1000046
1000047
1000048
1000049
1000051
1000052
1000053
1000054
1000055
1000056
1000057
1000058
1000060


and C:\Temp\Gprfolders are (these are folders)
1000038
1000039
1000041
1000045
1000046
1000047
1000048
1000049
1000051
1000052
1000053
1000054
1000055
1000056
1000057
1000058
1000060
1000061
1000062
1000064
1000065
1000066
1000067

for example 1000060 in C:\Temp\foldernames.txt  matches folder 1000060 in c:\Temp\Gprfolders so
folder 1000060 in c:\Temp\Gprfolders is moved to c:\Temp\archivefolder - deleting it from c:\Temp\Gprfolders.

This is the powershell script that I attemped but getting all sorts of errors.
I hope these information will now make it easier for you guys to help me.
Thanks again.
$test-path ="C:\Temp\Gprfolders\";
$Dest-path ="C:\Temp\Gprarchive";
 $folders =get-content "C:\Temp\gprfolderlists.txt";
 foreach ($folder in $folders)
  {if (test-path +$user)
   {Move-Item test-path +$user Dest-path -force}}
$Destpath ="C:\Temp\Gprarchive"
gc C:\Temp\gprfolderlists.txt | % {
if (Test-path $_)
{move-item $_ -destination $Destpath -force -whatif}
}

Open in new window



If the list looks good to you, you can remove the "-whatif"
ASKER CERTIFIED SOLUTION
Avatar of fboateng
fboateng

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
The solution worked perfectly for the task at hand.