Solved

VB script to sort files

Posted on 2010-09-17
16
311 Views
Last Modified: 2012-05-10
I have a folder with 1000's of files named by social security numbers.  . for example AB123456B(then random).pdf.   I would like to create a script that will scan the folder and create folders based on the social security numbers I.e the first 9 characters and then move all the pdf files into their respective folder.
0
Comment
Question by:gazzauk80
  • 8
  • 8
16 Comments
 
LVL 52

Expert Comment

by:Bill Prew
ID: 33700262
If the first 9 characters are always the SSN, then here's a script that should do that.

~bp
0
 
LVL 52

Expert Comment

by:Bill Prew
ID: 33700266
And here's the script...

~bp
@echo off
setlocal EnableDelayedExpansion
set BaseDir=c:\temp
for %%A in ("%BaseDir%\*.pdf") do (
  set DirName=%%~nA
  set DirName=!DirName:~0,9!
  if not exist "%BaseDir%\!DirName!\" mkdir "%BaseDir%\!DirName!\"
  move "%%~A" "%BaseDir%\!DirName!\"
)

Open in new window

0
 

Author Comment

by:gazzauk80
ID: 33701133
Sorry i misunderstood what was wanted.. The above is correct but they also then want it to scan for a key word I.e disciplinary then moved within that folder Disciplinary so  ab123323g-Disciplinary (it might not be in that order but the SSN number will always be first so could be ab123323g-blah what ever-Disciplinary) and then would either check to see if there was a folder for ab123323g if there was then check for Disciplinary folder if there is one put the file in there. If there isnt then make a SSN number off the front of the file name then within that make a folder called Disciplinary and put the file in there. I hope all this makes sence.
0
 
LVL 52

Expert Comment

by:Bill Prew
ID: 33701238
So is "Disciplinary" the only trigger string we are searching for, to then create another subfolder under the SSN folder, or are there others?

And for files that do not contain "Disciplinary" in their file name, do they just get moved to the SSN folder?

~bp
0
 

Author Comment

by:gazzauk80
ID: 33701404
For the files that do not have Disciplinary leave them where they are. But the folder locations are in 2 diff places. so it would be picking them up from say server1\scanned docs\AB123456h-whatever_disciplinary.pdf and putting them onto server2\HR\Disciplinary\AB123456h-whatever_disciplinary.pdf
only to create if it doesnt already exist if it does then just put the file in that folder
0
 

Author Comment

by:gazzauk80
ID: 33701431
sorry i have got that wrong it should be so it would be picking them up from say server1\scanned docs\AB123456h-whatever_disciplinary.pdf and putting them onto  server2\HR\AB123456h\disciplinary\AB123456h-whatever_disciplinary.pdf
0
 
LVL 52

Expert Comment

by:Bill Prew
ID: 33701490
Sorry, just want to make sure I have it right this time.  By "For the files that do not have Disciplinary leave them where they are" do you mean do not move them at all, or move them but only to the SSN folder?

~bp
0
 

Author Comment

by:gazzauk80
ID: 33701595
Nope dont move them at all. The reason behind that is they have other Keywords . So im just going to change the script with the other keywords . so the next keyword might be starters. So please dont make it too hard to understand that i cant change the keyword or the end folder location  :)

Thanks
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 52

Accepted Solution

by:
Bill Prew earned 350 total points
ID: 33703414
I think this should get the job done, but I haven't done a test on it.

~bp
@echo off
setlocal EnableDelayedExpansion
set BaseDir=c:\temp
set DestDir=d:\temp
set SelectText=disciplinary
set SelectDest=disciplinary
for %%A in ("%BaseDir%\*.pdf") do (
  set DirName=%%~nA
  for /f "delims=" %%Z in ('echo "!DirName!" ^| find /C /I "%SelectText%"') do SelectMatch=%%Z
  if !SelectMatch! GTR 0 (
    set DirName=!DirName:~0,9!
    set DirName=%BaseDir%\!DirName!\%SelectDest%
    if not exist "!DirName!\" mkdir "!DirName!\"
    move "%%~A" "!DirName!\"
  )
)

Open in new window

0
 

Author Comment

by:gazzauk80
ID: 33714331
no the above script doesnt do anything. I just get the dos screen and doesnt seem to move or do anything
0
 
LVL 52

Expert Comment

by:Bill Prew
ID: 33715395
Can you change the following line:

  for /f "delims=" %%Z in ('echo "!DirName!" ^| find /C /I "%SelectText%"') do SelectMatch=%%Z

to:

  for /f "delims=" %%Z in ('echo "!DirName!" ^| find /C /I "%SelectText%"') do set SelectMatch=%%Z

~bp
0
 

Author Comment

by:gazzauk80
ID: 33715510
ok that made the folders and put the file in  the base dir not the dest.
0
 
LVL 52

Expert Comment

by:Bill Prew
ID: 33715604
Ah yes, missed that, change this:

    set DirName=%BaseDir%\!DirName!\%SelectDest%

to:

    set DirName=%DestDir%\!DirName!\%SelectDest%

~bp
0
 

Author Comment

by:gazzauk80
ID: 33715638
That worked .Thank you Very much :)
0
 

Author Closing Comment

by:gazzauk80
ID: 33715653
The scipt worked a treat
0
 
LVL 52

Expert Comment

by:Bill Prew
ID: 33716206
Great, sorry for those oversights, glad we got it useful for you.

~bp
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

This script will sweep a range of IP addresses (class c only, 255.255.255.0) and report to a log the version of office installed. What it does: 1.)      Creates log file in the directory the script is run from (if it doesn't already exist) 2.)      Sweep…
Over the years I have built up my own little library of code snippets that I refer to when programming or writing a script.  Many of these have come from the web or adaptations from snippets I find on the Web.  Periodically I add to them when I come…
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.
Internet Business Fax to Email Made Easy - With  eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, f…

920 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now