Solved

VBS to be an "Alphabetic File Sorter"

Posted on 2006-06-24
18
222 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
Creating Instructional Tutorials  

For Any Use & On Any Platform

Contextual Guidance at the moment of need helps your employees/users adopt software o& achieve even the most complex tasks instantly. Boost knowledge retention, software adoption & employee engagement with easy solution.

 
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

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

When designing a form there are several BorderStyles to choose from, all of which can be classified as either 'Fixed' or 'Sizable' and I'd guess that 'Fixed Single' or one of the other fixed types is the most popular choice. I assume it's the most p…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
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…
Suggested Courses
Course of the Month5 days, 11 hours left to enroll

627 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