Solved

VBS to be an "Alphabetic File Sorter"

Posted on 2006-06-24
18
217 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
 
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Check version 13 68
passing a value with stream reader AFTER a ";" 3 58
OID and Active Directory Attributes 9 57
File download script VBSCRIPT fail on download PPT files 2 33
When trying to find the cause of a problem in VBA or VB6 it's often valuable to know what procedures were executed prior to the error. You can use the Call Stack for that but it is often inadequate because it may show procedures you aren't intereste…
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 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…
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…

911 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

22 Experts available now in Live!

Get 1:1 Help Now