kizer

Create a Folder from a FileName and move File into it

Recommended Posts

kizer    23

For instance I'd like to do something of the sort. 

 

From Some Folder lets Say

 

/mnt/Cache/2BeProcessed/

Transformers.mkv

Aliens.mkv

KingKong.mkv

SuperMan.mkv

 

into:

 

Movies/

Transformers/Transfomers.mkv

Aliens/Aliens.mkv

KingKong/KingKong.mkv

Superman/Superman.mkv

 

I'd like to be able to execute a script or click a button and move a group of files into their prospective folder and place said folders in a final destination. I guess this would work great from Cron or from user.scripts just as well. Actually I think it would be a bit cleaner to run it from user.scripts honestly. 

 

Please and Thank you of course. I tried a few times to find some code that would execute this, but oddly I can never get the loop to work right or for it to pick a folder opposed to executing it directly from the folder the script sits in along with the files. 

Share this post


Link to post
Share on other sites
Squid    515

From Windows.  I have it saved from years ago, but test before use on sample directories.  Not sure what its talking about why it would be a risk on network shares

REM file2folder.bat
REM Written by Jon from theHTPC.net
REM Ensure file attribute is set to hidden.  Very important!
REM Place in folder where your media files are located.
REM Use at own risk on network shares.  Bad things have happened.
REM Run the batch file and all of your files will be moved into newly
REM created folders of the same name!
REM This script is NOT recursive.  Must be run on all directories containing
REM media files.
REM NOT recommended for TV series episodes!
REM Visit www.theHTPC.net for HTPC-related news, tips, plugins and more!

REM ---BEGIN SCRIPT---

@echo off
for %%a in (*.*) do (
md "%%~na" 2>nul
move "%%a" "%%~na"
)
pause

REM ----END SCRIPT----

 

Share this post


Link to post
Share on other sites
kizer    23

Thanks Squid. I currently have something more or less of the same sorts. I was trying to find a unRAID version of it so I could pull my windows box out of the mix now and then, but I guess I'll continue to use what I have until I find something that can run via linux. 

 

I attached what I'm currently running in windows. Its Basically a VBS file I found on the net long time ago. Kinda me freaked me out trusting a .VBS file. Lol

 

dim objFSO, objFolder, b, c, newfolder
dim colFiles, source, destination

source = "\\192.168.1.127\cache\ZZZZZ\"
destination = "\\192.168.1.127\cache\Travel\"

Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFolder = objFSO.GetFolder(source)
Set colFiles = objFolder.Files

For Each objFile In colFiles

b = objFSO.GetBaseName (objFile.Name) 'get 'text' instead of 'text.txt'
c = objFSO.CreateFolder(source & b) 'create folder of same filename in same location as source directory
objFile.Move c & "\" & objFile.Name 'move file to within new folder

set newfolder = objFSO.GetFolder(source & b)
newfolder.Move(destination)

Next

 

createfolder.vbs

Share this post


Link to post
Share on other sites
kizer    23

@Squid Fixed out a Linux way and for unRAID to take some of the load off so I don't have to do it via windows. 

 

I rip all My HD content down to SD content so my 2 boys can easily watch on their iPads via Plex and it also insures there is no un-needed on the Fly Transcoding while everybody is home or if/when they want to sync with Plex and take with them. I wanted a way of Creating folders based off file names so I could store them easier for myself and reduce the drag of attempting to open up a share in windows and wait for ever for it to load or simply freeze. 

 

I ended up hacking this together. Not the prettiest, but it does work. Basically a 2 part operation for my sanity and as well I want to make sure there are no conflicts while running. 

 

This checks a folder looking for files that are at least 10 minutes old and the reason for that is I didn't want files to be attempted to be moved or tinkered with while HandBrake is converting. I have this set to run at a specific time, but just in case HandBrake is taking longer than I expect or I'm processing more than 1 file.

 

Check_Older_10Min

#!/bin/bash

find /Files_Location -maxdepth 1 -type f -mmin +10 -exec mv {} /NewFiles_Destination/ \;

 

When I first hacked the below together it wouldn't create a proper folder and move it if the file name had any spaces. If a file name was "A Dogs Day" it would create a folder named "A" and then it would error out trying to copy a file supposively of the same. So I hacked together the code to remove spaces. I had issues trying to get the code to run from anywhere so I just cheesed in the cd location nonsense so I could run it from anywhere, which is now from user.scripts. 

 

1. It removes the Spaces in file names

2. Then it creates a folder based off file name then drops said File into Folder which is on the Cache drive into a Movie Share. 

 

RemoveSpaces_n_CreateFolder

#!/bin/bash

#Remove Spaces and replace with . Change . below if you desire otherwise
cd /Current_FILEs_Location/
for f in *\ *; do mv "$f" "${f// /.}"; done

#Create Folder from name and move to final location. File types editable below
for FILE in `ls /Current_File_Location/ | egrep "mkv|mp4"`
do
DIR=`echo $FILE | rev | cut -f 2- -d '.' | rev`
mkdir /Final_Folder_destination/$DIR
mv /Current_FILEs_Location/$FILE /Final_Folder_destination/$DIR
done

Sure its some sloppy work, but honestly it works and gets rid of a few things I had to do by hand. 

 

Anyways I just wanted to share what I needed as a fix and if anybody decides to play with any of this please use Dummy files and make sure everything works because I'm not the best coder, but I hack away at things until I get it to work for my needs. I'm sure somebody else could come up something nicer or way more elegant with checks built in.  

 

Or of course you could combine them like I did

 

#!/bin/bash

#Move files that are older than 10 minutes
find /Files_Location -maxdepth 1 -type f -mmin +10 -exec mv {} /NewFiles_Destination/ \;

#Remove Spaces and replace with . Change . below if you desire otherwise
cd /Current_FILEs_Location/
for f in *\ *; do mv "$f" "${f// /.}"; done

#Create Folder from name and move to final location. File types editable below
for FILE in `ls /Current_File_Location/ | egrep "mkv|mp4"`
do
DIR=`echo $FILE | rev | cut -f 2- -d '.' | rev`
mkdir /Final_Folder_destination/$DIR
mv /Current_FILEs_Location/$FILE /Final_Folder_destination/$DIR
done

 

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Copyright © 2005-2017 Lime Technology, Inc. unRAID® is a registered trademark of Lime Technology, Inc.