Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

VBS to be an "Alphabetic File Sorter"

Posted on 2006-06-24
18
Medium Priority
?
223 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
[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
  • 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
Independent Software Vendors: 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 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 1520 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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

If you have ever used Microsoft Word then you know that it has a good spell checker and it may have occurred to you that the ability to check spelling might be a nice piece of functionality to add to certain applications of yours. Well the code that…
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…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…
Suggested Courses

705 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