MSFN Forum: Help with VBScript to move folders and files - MSFN Forum

Jump to content


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

Help with VBScript to move folders and files Rate Topic: -----

#1 User is offline   wjbeckett 

  • Group: Members
  • Posts: 2
  • Joined: 02-October 12
  • OS:Windows 7 x64
  • Country: Country Flag

Posted 02 October 2012 - 04:50 PM

I currently have a VBscript that scans a folder for files and moves the files to particular folders depending on key words in the file name.

Currently the script only moves loose files and not folders. I need it to move (or copy then delete) both folders and files based on keywords in the file/folder name.

Can someone give me a hand with this?

Below is my script so far.

'========================================================
' Script to Move Downloaded TV Shows and Movies to
' correct folders based on wildcards in File Name
'========================================================

On Error Resume Next

Dim sTorrents, sTV, sMovie, sFile, oFSO

' create the filesystem object
Set oFSO = WScript.CreateObject("Scripting.FileSystemObject")

' Create Log File
Set objLog = oFSO.OpenTextFile("c:\temp\log.txt", 8, True)

' Set Variables
sTorrents = "D:\torrents\"
sTV = "D:\Downloads\TV Shows\"
sMovie = "D:\Downloads\Movies\"

' Scan each file in the folder
For Each sFile In oFSO.GetFolder(sTorrents).Files
' check if the file name contains TV Show Parameters
If InStr(1, sFile.Name, "hdtv", 1) OR InStr(1, sFile.Name, "s0", 1) <> 0 Then
    ' TV Show Detected - Move File
    objLog.WriteLine Now() & " - " & sFile.Name & " Detected as TV Show - Moving to " & sTV
    oFSO.MoveFile sTorrents & sFile.Name, sTV & sFile.Name
' Move all other Files to Movies Directory
Else objLog.WriteLine Now() & " - " & sFile.Name & " Detected as Movie - Moving to " & sMovie
    oFSO.MoveFile sTorrents & sFile.Name, sMovie & sFile.Name
End If

Next

If sTorrents.File.Count = 0 And sTorrents.SubFolders.Count = 0 Then
    objLog.WriteLine Now() & " - There is nothing left to Process..."
    objLog.Close
End If


As I mentioned it only picks up files only and not folders. I need it to do both if that's possible.

So for example. I have a folder where utorrent dumps all the completed downloads (D:\torrents). I also have two directories that need to be used “D:\Downloads\TV Shows\” and “D:\Downloads\Movies\".
Lets see if we can make the script search the torrents folder - if it finds any folders move them to the correct directory (by using the keywords “hdtv” or “s0”). If it finds any files do the same.

Example:
D:\Torrents has the following files -
How.I.Met.Your.Mother.S08E01.x264.hdtv - This is a folder (Move to TV Shows)
New.Girl.S02E01.x264.hdtv.mp4 - This is a TV Show (Move to TV Shows)
Rock.of.Ages.720p.x264.RELEASE - This is a Folder (Move to Movies)
Arbitrage.DVD-R.RELEASE.avi - This is a Movie (Move to Movies)

I want the script to pick up files and entire folders (including and sub folders) and move the files and folders to the correct locations.


#2 User is offline   allen2 

  • Not really Newbie
  • PipPipPipPipPipPipPip
  • Group: Members
  • Posts: 1,736
  • Joined: 13-January 06

Posted 03 October 2012 - 12:44 AM

Something like this should work:
' Scan each file in the folder
For Each sFolder In oFSO.GetFolder(sTorrents).SubFolders
' check if the folder name contains TV Show Parameters
If InStr(1, sFolder.Name, "hdtv", 1) OR InStr(1, sFolder.Name, "s0", 1) <> 0 Then
    ' TV Show Detected - Move File
    objLog.WriteLine Now() & " - " & sFolder.Name & " Detected as TV Show - Moving to " & sTV
    oFSO.MoveFile sTorrents & sFolder.Name, sTV & sFile.Name
' Move all other Files to Movies Directory
Else objLog.WriteLine Now() & " - " & sFolder.Name & " Detected as Movie - Moving to " & sMovie
    oFSO.MoveFolder sTorrents & sFolder.Name, sMovie & sFolder.Name
End If

Next


#3 User is offline   wjbeckett 

  • Group: Members
  • Posts: 2
  • Joined: 02-October 12
  • OS:Windows 7 x64
  • Country: Country Flag

Posted 03 October 2012 - 04:10 PM

Thanks for the reply.
You're script worked! I just added it on to the end of the current script and worked perfectly!

Thanks so much!

This post has been edited by wjbeckett: 03 October 2012 - 06:21 PM


Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

2 User(s) are reading this topic
0 members, 2 guests, 0 anonymous users



All trademarks mentioned on this page are the property of their respective owners
Copyright © 2001 - 2013 msfn.org
Privacy Policy