Solved

VB script to sort files

Posted on 2010-09-17
16
310 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 51

Expert Comment

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

~bp
0
 
LVL 51

Expert Comment

by:Bill Prew
Comment Utility
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
Comment Utility
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 51

Expert Comment

by:Bill Prew
Comment Utility
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
Comment Utility
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
Comment Utility
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 51

Expert Comment

by:Bill Prew
Comment Utility
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
Comment Utility
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
How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

 
LVL 51

Accepted Solution

by:
Bill Prew earned 350 total points
Comment Utility
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
Comment Utility
no the above script doesnt do anything. I just get the dos screen and doesnt seem to move or do anything
0
 
LVL 51

Expert Comment

by:Bill Prew
Comment Utility
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
Comment Utility
ok that made the folders and put the file in  the base dir not the dest.
0
 
LVL 51

Expert Comment

by:Bill Prew
Comment Utility
Ah yes, missed that, change this:

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

to:

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

~bp
0
 

Author Comment

by:gazzauk80
Comment Utility
That worked .Thank you Very much :)
0
 

Author Closing Comment

by:gazzauk80
Comment Utility
The scipt worked a treat
0
 
LVL 51

Expert Comment

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

~bp
0

Featured Post

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

Hello again, all.  For those of you that have been following along, you'll know that this is my third article on this topic (though it is not Part III).  This article is sort of remedial, and probably the topic with which I should have started the s…
Introduction During my participation as a VBScript contributor at Experts Exchange, one of the most common questions I come across is this: "I have a script that runs against only one computer. How can I make it run against a list of computers in …
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

772 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

12 Experts available now in Live!

Get 1:1 Help Now