Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Script to Move Folders and Files based on File Name

Status
Not open for further replies.

wjbeckett

Technical User
Oct 2, 2012
3
AU
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 and I need it to Move both folders and files based on keywords in the name.

Can someone give me a hand with this?

Below is my script so far.

Code:
'========================================================
' 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 = "C:\Temp\torrents\"
sTV = "C:\Temp\TV Shows\"
sMovie = "C:\Temp\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
 
Do you mean you want to move the whole folder if it contains a TV show or movie? Or you want to move tv shows and movies to a new location in a folder of the same name as where it was orinigally downloaded?

-Geates

"I do not offer answers, only considerations."
- Geates's Disclaimer

 
I need it to move the whole folder.

For example,
All of my downloads go to D:/Torrents/
Most downloads aren't single files but folders with the movie or TV show within them.

So I need the script to pick up any folder in D:/torrents that has a movie or TV show in it (determined by key words in the folded name) and move it to the TV show or movie directory. I also need the script to pick up single files (which is what it currently does).
 
I resolved this script by adding the following to the end.

Code:
' Scan each 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 Folder Detected - Move File
    objLog.WriteLine Now() & " - " & sFolder.Name & " Detected as TV Show Folder - Moving to " & sTV
    oFSO.MoveFolder sTorrents & sFolder.Name, sTV & sFolder.Name
' Move all other Files to Movies Directory
Else objLog.WriteLine Now() & " - " & sFolder.Name & " Detected as Movie Folder - Moving to " & sMovie
    oFSO.MoveFolder sTorrents & sFolder.Name, sMovie & sFolder.Name
End If
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top