Results 1 to 5 of 5

Thread: creating folders from file names

  1. #1
    Time for Walkies... Atomic's Avatar
    Join Date
    Apr 2004
    Location
    Norfolk, UK
    Posts
    1,959
    Thanks
    0
    Thanked
    0 times in 0 posts

    creating folders from file names

    Basically I have a bunch of files (peoples names eg smithj, jonesp, carterg etc.) and I need to create a folder with the same name and move the file to it.

    eg. smithj.txt -> smithj/smithj.txt

    Anyone know a good script to do this? (vb or a bat file would be best but unix would be ok)
    Last edited by Atomic; 15-10-2004 at 05:29 PM.

  2. #2
    Senior Member Shad's Avatar
    Join Date
    Jul 2003
    Location
    In front
    Posts
    2,782
    Thanks
    23
    Thanked
    42 times in 25 posts
    In VBS, something along the lines of...

    Set myfol as fso.getfolder(pathtofoldercontainingfiles)
    For each file in myfol
    fso.createfolder(pathtofoldercontainingfiles & "\" & removeExtension(file.name))
    Next
    Simon


  3. #3
    Ex-MSFT Paul Adams's Avatar
    Join Date
    Jul 2003
    Location
    %systemroot%
    Posts
    1,926
    Thanks
    29
    Thanked
    77 times in 59 posts
    • Paul Adams's system
      • Motherboard:
      • Asus Maximus VIII
      • CPU:
      • Intel Core i7-6700K
      • Memory:
      • 16GB
      • Storage:
      • 2x250GB SSD / 500GB SSD / 2TB HDD
      • Graphics card(s):
      • nVidia GeForce GTX1080
      • Operating System:
      • Windows 10 x64 Pro
      • Monitor(s):
      • Philips 40" 4K
      • Internet:
      • 500Mbps fiber
    This code should work - just edit strFolderPath to suit.
    It checks to see if the folder already exists before creating it, and will move all files of the same name but different extension into the same folder.
    (e.g. Test File 1.txt and Test File 1.mpg would both end up in C:\Test Folder\Test File 1)

    Probably not the most elegant or efficient way to do it, and it doesn't check that your specified folder exists first, but it works (on my PC at least )

    Code:
    Const	strFolderPath = "C:\Test Folder"
    Dim	fso, objFolder, strFileLessExt
    
    Set fso = CreateObject("Scripting.FileSystemObject")
    Set objFolder = fso.GetFolder(strFolderPath)
    
    For each objFile in objFolder.Files
      strFileLessExt = Left(objFile.Name, InStr(objFile.Name, ".") - 1)
      If Not fso.FolderExists(strFolderPath & "\" & strFileLessExt) then
        fso.CreateFolder(strFolderPath & "\" & strFileLessExt)
      End If
      objFile.Move(strFolderPath & "\" & strFileLessExt & "\")
    Next
    
    set fso = Nothing
    set objFolder = Nothing
    ~ I have CDO. It's like OCD except the letters are in alphabetical order, as they should be. ~
    PC: Win10 x64 | Asus Maximus VIII | Core i7-6700K | 16GB DDR3 | 2x250GB SSD | 500GB SSD | 2TB SATA-300 | GeForce GTX1080
    Camera: Canon 60D | Sigma 10-20/4.0-5.6 | Canon 100/2.8 | Tamron 18-270/3.5-6.3

  4. #4
    Time for Walkies... Atomic's Avatar
    Join Date
    Apr 2004
    Location
    Norfolk, UK
    Posts
    1,959
    Thanks
    0
    Thanked
    0 times in 0 posts
    Oh Excellent Cheers Guys!

    Thats gonna save me doing the 400 or so files by hand!

  5. #5
    Time for Walkies... Atomic's Avatar
    Join Date
    Apr 2004
    Location
    Norfolk, UK
    Posts
    1,959
    Thanks
    0
    Thanked
    0 times in 0 posts
    A little extension to this actually

    They are actually HTML files, and there is an image in them, named the same as the file.

    I need the script to search the file and replace that string before it moves it into the new folder created in the above problem.

    eg. in smithj.htm is <img src="images/smithj.jpg"> this should be replaced with <img src="photo.jpg">

    Many thanks to anyone who could do this for me. I got dragged into re-making the staff pages at work and its no fun to say the least

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Replies: 63
    Last Post: 14-11-2011, 09:17 AM
  2. file eating monster: virus, XP or hard drive
    By justravis in forum PC Hardware and Components
    Replies: 16
    Last Post: 26-02-2009, 02:58 AM
  3. How do you backup?
    By Howard in forum PC Hardware and Components
    Replies: 48
    Last Post: 05-01-2005, 09:05 AM
  4. Long File Names Deleting
    By Stringent in forum Software
    Replies: 11
    Last Post: 22-04-2004, 12:10 PM
  5. Manage downloaded webpages file and folder
    By Zathras in forum Software
    Replies: 0
    Last Post: 17-09-2003, 05:37 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •