[Last Call] Learn about multicloud storage options and how to improve your company's cloud strategy. Register Now

x
?
Solved

VB script to sort files

Posted on 2010-09-17
16
Medium Priority
?
318 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 58

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 58

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 58

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 58

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 58

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 58

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 58

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 58

Expert Comment

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

~bp
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

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

Welcome, welcome!  If you are new to the series and haven't been following along, please take a brief moment to review the first three installments: Part 1 (http://www.experts-exchange.com/Programming/Languages/Visual_Basic/VB_Script/A_266-VBScri…
When it comes to writing scripts for a Client/Server computing environment it is essential to consider some way of enabling the authentication functionality within a script. This sort of consideration mainly comes into the picture when we are dealin…
Video by: ITPro.TV
In this episode Don builds upon the troubleshooting techniques by demonstrating how to properly monitor a vSphere deployment to detect problems before they occur. He begins the show using tools found within the vSphere suite as ends the show demonst…
In a question here at Experts Exchange (https://www.experts-exchange.com/questions/29062564/Adobe-acrobat-reader-DC.html), a member asked how to create a signature in Adobe Acrobat Reader DC (the free Reader product, not the paid, full Acrobat produ…

650 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