?
Solved

VB script to sort files

Posted on 2010-09-17
16
Medium Priority
?
317 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 8
  • 8
16 Comments
 
LVL 56

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 56

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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 56

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 56

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
 
LVL 56

Accepted Solution

by:
Bill Prew earned 1400 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 56

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 56

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 56

Expert Comment

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

~bp
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Welcome back!  My apologies for taking so long to write part two of this series; it's been a long time coming!  As I promised in Part 1, this article will focus on how to locate those elusive AD properties that you are searching for.  Why is this us…
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…
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …
Suggested Courses
Course of the Month14 days, 15 hours left to enroll

770 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