Solved

VBS to be an "Alphabetic File Sorter"

Posted on 2006-06-24
18
218 Views
Last Modified: 2010-04-07
I need a VBS to do the following - create new folders (subfolders) inside a main folder I specify, which name is based on the initial characters of files' name. First I must specify if it is only the first character, or the two, or three (and so on) inital characters of files' name. For example, inside the main folder there are 4 files : aaaa.ext, abbb.ext, accc.ext, addd.ext => I specify that the subfolders' name will be equal to the two inital characters of files' name => it automatically :
creates aa\ and moves aaaa.ext inside aa\;
creates ab\ and moves abbb.ext inside aa\;
creates ac\ and moves accc.ext inside ac\;
creates ad\ and moves addd.ext inside ad\;
Did you understand ?
Is something like an automatic batch.bat file - «md aa» => «move aa* aa\»; but makes everything automatically after I specify that I want to use the two inital characters of files' name.
I do not need a GUI, command-line is enough.
Can you help me.
A big thank you in advance
Regards.
0
Comment
Question by:asgarcymed
  • 11
  • 7
18 Comments
 
LVL 19

Expert Comment

by:BrianGEFF719
ID: 16976548
Try this code. I did not test it but it should be correct.

dim basePath
basePath = "c:\test"


  basePath = iif( right(basePath,1) <> "\", basePath & "\", basePath )
  Dim objFileScripting, objFolder
  Dim filename, filecollection, strDirectoryPath, strUrlPath
  dim destFolder
  Set objFileScripting = CreateObject("Scripting.FileSystemObject")
  Set objFolder = objFileScripting.GetFolder( iif(right(basePath,1) <> "\",basePath & "\",basePath) )
  Set filecollection = objFolder.Files
  For Each filename In filecollection
        destFolder = basePath & left(fileName,2) & "\"
      if objFileScript.folderExists(destFolder) = false then
            objFileScripting.CreateFolder destFolder
      end if
      objFileScripting.MoveFile basePath & filename, destFolder & fileName
  Next
0
 
LVL 19

Expert Comment

by:BrianGEFF719
ID: 16976549
its VBScript btw.
0
 

Author Comment

by:asgarcymed
ID: 16977653
I replaced “iif” for “if”, but I still get an error – «Line: 5; Char: 14; Error: Syntax error; Code: 800A03EA; Source: Microsoft VBScript compilation error». Can you correct this ?
Thanks.
Regards.
0
Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

 
LVL 19

Expert Comment

by:BrianGEFF719
ID: 16977913
try this:

dim basePath
basePath = "c:\test"


  if right(basePath,1) <> "\" then basePath = basePath & "\"
  Dim objFileScripting, objFolder
  Dim filename, filecollection, strDirectoryPath, strUrlPath
  dim destFolder
  Set objFileScripting = CreateObject("Scripting.FileSystemObject")
  Set objFolder = objFileScripting.GetFolder( iif(right(basePath,1) <> "\",basePath & "\",basePath) )
  Set filecollection = objFolder.Files
  For Each filename In filecollection
       destFolder = basePath & left(fileName,2) & "\"
     if objFileScript.folderExists(destFolder) = false then
          objFileScripting.CreateFolder destFolder
     end if
     objFileScripting.MoveFile basePath & filename, destFolder & fileName
  Next
0
 
LVL 19

Expert Comment

by:BrianGEFF719
ID: 16977918
And IIF() is the conditional If statment. Its similiar to the () ?: statment in C++


Brian
0
 

Author Comment

by:asgarcymed
ID: 16977985
When I use this second VBS with the "iif" (original - exactly what you posted), I get the error - «Line: 10; Char: 3; Error: Type mismarch: "iif"; Code: 800A000D; Source: Microsoft VBScript runtime error».

If I replaced “iif” for “if”, I get the error - «Line: 10; Char: 47; Error: Syntax error; Code: 800A03EA; Source: Microsoft VBScript compilation error».

What do you say about this ?
Thanks.
Regards.
0
 
LVL 19

Expert Comment

by:BrianGEFF719
ID: 16977999
I say use the second version I posted using regular if statments.


Brian
0
 
LVL 19

Expert Comment

by:BrianGEFF719
ID: 16978001
dim basePath
basePath = "c:\test"


  if right(basePath,1) <> "\" then basePath = basePath & "\"
  Dim objFileScripting, objFolder
  Dim filename, filecollection, strDirectoryPath, strUrlPath
  dim destFolder
  Set objFileScripting = CreateObject("Scripting.FileSystemObject")
  Set objFolder = objFileScripting.GetFolder( iif(right(basePath,1) <> "\",basePath & "\",basePath) )
  Set filecollection = objFolder.Files
  For Each filename In filecollection
       destFolder = basePath & left(fileName,2) & "\"
     if objFileScript.folderExists(destFolder) = false then
          objFileScripting.CreateFolder destFolder
     end if
     objFileScripting.MoveFile basePath & filename, destFolder & fileName
  Next
0
 

Author Comment

by:asgarcymed
ID: 16978019
When I use :

 dim basePath
basePath = "c:\test"


  if right(basePath,1) <> "\" then basePath = basePath & "\"
  Dim objFileScripting, objFolder
  Dim filename, filecollection, strDirectoryPath, strUrlPath
  dim destFolder
  Set objFileScripting = CreateObject("Scripting.FileSystemObject")
  Set objFolder = objFileScripting.GetFolder( iif(right(basePath,1) <> "\",basePath & "\",basePath) )
  Set filecollection = objFolder.Files
  For Each filename In filecollection
       destFolder = basePath & left(fileName,2) & "\"
     if objFileScript.folderExists(destFolder) = false then
          objFileScripting.CreateFolder destFolder
     end if
     objFileScripting.MoveFile basePath & filename, destFolder & fileName
  Next

I get the error - «Line: 10; Char: 3; Error: Type mismarch: "iif"; Code: 800A000D; Source: Microsoft VBScript runtime error».



When I use :

 dim basePath
basePath = "c:\test"


  if right(basePath,1) <> "\" then basePath = basePath & "\"
  Dim objFileScripting, objFolder
  Dim filename, filecollection, strDirectoryPath, strUrlPath
  dim destFolder
  Set objFileScripting = CreateObject("Scripting.FileSystemObject")
  Set objFolder = objFileScripting.GetFolder( if(right(basePath,1) <> "\",basePath & "\",basePath) )
  Set filecollection = objFolder.Files
  For Each filename In filecollection
       destFolder = basePath & left(fileName,2) & "\"
     if objFileScript.folderExists(destFolder) = false then
          objFileScripting.CreateFolder destFolder
     end if
     objFileScripting.MoveFile basePath & filename, destFolder & fileName
  Next

 I get the error - «Line: 10; Char: 47; Error: Syntax error; Code: 800A03EA; Source: Microsoft VBScript compilation error».

Forgive my ignorance - I am a newbie... I am now beginning to learn VBS. Please be patient to help me.
Thanks.
Regards.
0
 
LVL 19

Expert Comment

by:BrianGEFF719
ID: 16978024
Sorry, try this instead.


  if right(basePath,1) <> "\" then basePath = basePath & "\"
  Dim objFileScripting, objFolder
  Dim filename, filecollection, strDirectoryPath, strUrlPath
  dim destFolder
  Set objFileScripting = CreateObject("Scripting.FileSystemObject")
  Set objFolder = objFileScripting.GetFolder( basePath )
  Set filecollection = objFolder.Files
  For Each filename In filecollection
       destFolder = basePath & left(fileName,2) & "\"
     if objFileScript.folderExists(destFolder) = false then
          objFileScripting.CreateFolder destFolder
     end if
     objFileScripting.MoveFile basePath & filename, destFolder & fileName
  Next
0
 

Author Comment

by:asgarcymed
ID: 16978096
Now I get the error - «Line: 14; Char: 6; Error: Object required: "objFileScript"; Code: 800A01A8; Source: Microsoft VBScript runtime error».
Thanks.
Regards.
0
 
LVL 19

Expert Comment

by:BrianGEFF719
ID: 16978099
Again sorry:


  if right(basePath,1) <> "\" then basePath = basePath & "\"
  Dim objFileScripting, objFolder
  Dim filename, filecollection, strDirectoryPath, strUrlPath
  dim destFolder
  Set objFileScripting = CreateObject("Scripting.FileSystemObject")
  Set objFolder = objFileScripting.GetFolder( basePath )
  Set filecollection = objFolder.Files
  For Each filename In filecollection
       destFolder = basePath & left(fileName,2) & "\"
     if objFileScripting.folderExists(destFolder) = false then
          objFileScripting.CreateFolder destFolder
     end if
     objFileScripting.MoveFile basePath & filename, destFolder & fileName
  Next
0
 

Author Comment

by:asgarcymed
ID: 16978119
Now I get the error - «Line: 15; Char: 11; Error: bad file name or number; Code: 800A0034; Source: Microsoft VBScript runtime error».
Thanks.
Regards.
0
 
LVL 19

Expert Comment

by:BrianGEFF719
ID: 16978128
try this, sorry for all this confusion I dont have WSH installed on this computer so i'm kidna shooting in the dark right now.

 if right(basePath,1) <> "\" then basePath = basePath & "\"
  Dim objFileScripting, objFolder
  Dim filename, filecollection, strDirectoryPath, strUrlPath
  dim destFolder
  Set objFileScripting = CreateObject("Scripting.FileSystemObject")
  Set objFolder = objFileScripting.GetFolder( basePath )
  Set filecollection = objFolder.Files
  For Each filename In filecollection
       destFolder = basePath & left(fileName,2) & "\"
     if objFileScripting.folderExists(left(destFolder,len(destFolder) - 1)) = false then
          objFileScripting.CreateFolder destFolder
     end if
     objFileScripting.MoveFile basePath & filename, destFolder & fileName
  Next
0
 

Author Comment

by:asgarcymed
ID: 16978147
The error is the same - «Line: 15; Char: 11; Error: bad file name or number; Code: 800A0034; Source: Microsoft VBScript runtime error».
Thanks.
Regards.
0
 
LVL 19

Accepted Solution

by:
BrianGEFF719 earned 380 total points
ID: 16978167
I'm very sorry about all this. I finally got to a computer with WSH and I fixed the code and it is now working 100%.
Good Luck.

Brian


dim basePath
basePath = "c:\test"


 if right(basePath,1) <> "\" then basePath = basePath & "\"
  Dim objFileScripting, objFolder
  Dim filename, filecollection, strDirectoryPath, strUrlPath
  dim destFolder
  Set objFileScripting = CreateObject("Scripting.FileSystemObject")
  Set objFolder = objFileScripting.GetFolder( basePath )
  Set filecollection = objFolder.Files
  For Each filename In filecollection
     tFile = fileName
     while instr(tFile,"\")
      tFile = right(tFile , len(tFile) - instr(tFile,"\"))
     wend

     destFolder = basePath & left(tFile,2)
     
     if objFileScripting.folderExists(destFolder) = false then
        msgbox destFolder
          objFileScripting.CreateFolder ( destFolder )
     end if
     objFileScripting.MoveFile basePath & tFile,destFolder & "\" & tFIle
  Next
0
 
LVL 19

Expert Comment

by:BrianGEFF719
ID: 16978169
you might want to remove the line "msgbox destFolder", I had that in there just for testing.
0
 

Author Comment

by:asgarcymed
ID: 16978223
Thank you a lot !!!! Now is perfect ;)
Regards.
0

Featured Post

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
VB Script/Bat: check if a file has been modified based on prefix 10 73
VB6 ListBox Question 4 42
change the windows script file to BAT 10 40
Excel 2016 loop through 6 36
Not long ago I saw a question in the VB Script forum that I thought would not take much time. You can read that question (Question ID  (http://www.experts-exchange.com/Programming/Languages/Visual_Basic/VB_Script/Q_28455246.html)28455246) Here (http…
Deploying a Microsoft Access application in a Citrix environment is not difficult but takes a few steps. However, Citrix system people are often of little help, as they typically know next to nothing about Access. The script provided here will take …
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…

776 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