Welcome guest. Before posting on our computer help forum, you must register. Click here it's easy and free.

Author Topic: Batch file to create folder following the name of specific file then move files  (Read 8440 times)

0 Members and 1 Guest are viewing this topic.

jakel



    Greenhorn

    • Yes
  • Experience: Experienced
  • OS: Windows 7
save below line as myvbs.vbs
change line 13 to the target folder also
---------------------------------------------------------
Option Explicit
On Error Resume Next
Dim T
dim fs
dim folder
dim files
dim file
Dim oFSO
Dim X

Set fs = CreateObject("Scripting.FileSystemObject")
'Directory you want sorted
Set folder = fs.GetFolder("c:\myvbs\")

Set files = folder.Files
  For Each file in files
    If file.name = "myvbs.vbs" Then
      rem wscript.echo "oops found myself"
   Else
      T = Mid(file.name,1,len(file.name) - 4)
      MD(T)
      Set oFSO = CreateObject ("Scripting.FileSystemObject")
      X = folder & "\" & T & "\"
      oFSO.MoveFile file.name,X & file.name
   End IF
  Next

Private Function MD(X)
Dim objFS, objFol
Set objFS = CreateObject("Scripting.FileSystemObject")
Set objFol = objFS.CreateFolder(X)
End function

Squashman



    Specialist
  • Thanked: 134
  • Experience: Experienced
  • OS: Other
Looks like your code is creating a folder for each file name.  Which is not what the user asked.  Of the set of three files, there should only be one folder created and the three files moved to that folder.  Also, why not just use .GetBaseName(strPath) instead of stripping the extension with -4.  Many file extensions these days are no longer dot 3.

jakel



    Greenhorn

    • Yes
  • Experience: Experienced
  • OS: Windows 7
it would be more efficient if it didnt try to make a folder each time, but the outcome is the same. good point with getbasename.
i have cleaned it up a little bit and added what you said , also filename is irrelevant now as long as it has a .vbs extension , and it will just run in the folder it is in.
___________________________________

Dim T , fs, folder, files, file ,X , objFol
Set fs = CreateObject("Scripting.FileSystemObject")
Set folder = fs.GetFolder(WScript.ScriptFullName & "\..")
Set files = folder.Files
For Each file in files
   If Not file.name = Wscript.ScriptName Then
      T = fs.getbasename(file.name)
      Set objFol = FS.CreateFolder(T)
      X = folder & "\" & T & "\"
      FS.MoveFile file.name,X & file.name
   End IF
Next

jakel



    Greenhorn

    • Yes
  • Experience: Experienced
  • OS: Windows 7
not even a thankyou for solving this problem after a year  :'(

Salmon Trout

  • Guest
not even a thankyou for solving this problem after a year  :'(
If you are here for teh th4nxx you are in the wrong place.