Automate File Renaming and Moving With Scripts - Guide

Windows specific questions, problems.
Post Reply
insanebrain

Automate File Renaming and Moving With Scripts - Guide

Post by insanebrain »

Hi Guys,

I'm fairly new to the forum. I don't know how active I will be, but just recently I've configured my downloads to automate as much as I can.

I have a bunch of downloads that I look for via RSS (specifically mp4 and mkv) and when they download, upon their completion they are automatically renamed to remove as many download tags, render tags and other bits. Then remove all dots and replace them with spaces and add a space between the season and episode tags. Finally, once that's done, the completed files are then moved to a different location where they are further processed.

I thought some people here might like to see how I've got everything for renaming the files and moving them. Plus some of you could drastically improve the scripts I've been using for yourselves. And yes, I know my code is rubbish before anyone mentions it! ;)

This process will work for singular files or multiple files from the same torrent.

You may be in for a long read, just warning you now.

---------------------------------

Current Setup
At the moment, I have a micro form factor dell running windows 10, that I saved from being recycled running qbittorrent. I've set the BIOS to make sure it will auto turn on at a specific time (it's always left on anyway) plus also set it to turn back on after powerloss. So it's always up.

All downloads go to a USB3 external HDD (as it was the easiest way to up the storage, I'm lazy!), then once complete they are moved to a different folder on the same drive to run scripts. The reason to move them to run the scripts is so the scripts won't interfere with other downloads that have yet to finish. Then once all scripts have ran, they are moved to a network share for final processing on a different machine (Adding metadata and the like).

What does my script do?
It will automatically delete any .jpg, .txt, .nfo or .exe files that were included in the download folder. It will then try it's best to rename all files, specifically .mp4 and .mkv files. It will do this by looking for specific words or characters and replacing them. Then once done, uses Robocopy to transfer the files to their last destination.

What's left after the script has ran?
All files and their containing folder are moved to their final destination. An empty folder is left behind in the original location.

What format is the script?
It's a .bat file. So it runs using windows command line. This bat file will push some commands through to powershell but will do so without opening a new session. This means it can work through all commands and not get interrupted.

---------------------------------

OK, so I've read all that stuff above, show me the code already!
Just be aware that this isn't written to be fast or efficient or any of that stuff. Simply because I'm an noob when it comes to code.

Just remember, if you want to use this you'll have to customize it for your setup as this won't work otherwise. Also, be careful and use at your own risk.

Code: Select all

@echo off

REM This changes the drive to the one with my downloads
REM If you don't save to an external drive you can delete these 3 lines.
E:

REM This moves the directory to the final download location
REM you will need to put the location of your download folder here. For example C:\Username\Downloads\FinalDownload
REM in order for the code to work smoother I would recommend you set up your downloads to put temp downloads in
REM a downloads folder, then move them once completed to a different folder. Otherwise this will mess with downloads
REM that are already in progress.
cd "\Downloads\To Transfer"

REM These commands will silently delete any of the following files from the download location
del /s/f/q *.txt
del /s/f/q *.jpg
del /s/f/q *.nfo
del /s/f/q *.exe

REM These commands will automatically rename any mp4 or mkv files in the download. It will get rid of the usual download tags. 
REM If you want to add more tags, copy both lines if you want to include mp4 and mkv, then change the name between the 1st ''
REM for example '1080p', then add what you want that to change to in the second set of ''
REM for ecample '' for nothing or ' ' for a space, or 'EXAMPLE TEXT'
REM As the list grows, the batch file will take longer to run through
powershell -command "get-childitem -recurse | dir -Filter *.mkv | Rename-Item -NewName { $_.BaseName.replace('.1080p','') + $_.Extension }"
powershell -command "get-childitem -recurse | dir -Filter *.mp4 | Rename-Item -NewName { $_.BaseName.replace('.1080p','') + $_.Extension }"

powershell -command "get-childitem -recurse | dir -Filter *.mkv | Rename-Item -NewName { $_.BaseName.replace('.720p','') + $_.Extension }"
powershell -command "get-childitem -recurse | dir -Filter *.mp4 | Rename-Item -NewName { $_.BaseName.replace('.720p','') + $_.Extension }"

powershell -command "get-childitem -recurse | dir -Filter *.mkv | Rename-Item -NewName { $_.BaseName.replace('.WEB-DL','') + $_.Extension }"
powershell -command "get-childitem -recurse | dir -Filter *.mp4 | Rename-Item -NewName { $_.BaseName.replace('.WEB-DL','') + $_.Extension }"

powershell -command "get-childitem -recurse | dir -Filter *.mkv | Rename-Item -NewName { $_.BaseName.replace('.WEBRIP','') + $_.Extension }"
powershell -command "get-childitem -recurse | dir -Filter *.mp4 | Rename-Item -NewName { $_.BaseName.replace('.WEBRIP','') + $_.Extension }"

powershell -command "get-childitem -recurse | dir -Filter *.mkv | Rename-Item -NewName { $_.BaseName.replace('.WEBRip','') + $_.Extension }"
powershell -command "get-childitem -recurse | dir -Filter *.mp4 | Rename-Item -NewName { $_.BaseName.replace('.WEBRip','') + $_.Extension }"

powershell -command "get-childitem -recurse | dir -Filter *.mkv | Rename-Item -NewName { $_.BaseName.replace('.AMZN','') + $_.Extension }"
powershell -command "get-childitem -recurse | dir -Filter *.mp4 | Rename-Item -NewName { $_.BaseName.replace('.AMZN','') + $_.Extension }"

powershell -command "get-childitem -recurse | dir -Filter *.mkv | Rename-Item -NewName { $_.BaseName.replace('.HULU','') + $_.Extension }"
powershell -command "get-childitem -recurse | dir -Filter *.mp4 | Rename-Item -NewName { $_.BaseName.replace('.HULU','') + $_.Extension }"

powershell -command "get-childitem -recurse | dir -Filter *.mkv | Rename-Item -NewName { $_.BaseName.replace('.NF','') + $_.Extension }"
powershell -command "get-childitem -recurse | dir -Filter *.mp4 | Rename-Item -NewName { $_.BaseName.replace('.NF','') + $_.Extension }"

powershell -command "get-childitem -recurse | dir -Filter *.mkv | Rename-Item -NewName { $_.BaseName.replace('.H264','') + $_.Extension }"
powershell -command "get-childitem -recurse | dir -Filter *.mp4 | Rename-Item -NewName { $_.BaseName.replace('.H264','') + $_.Extension }"

powershell -command "get-childitem -recurse | dir -Filter *.mkv | Rename-Item -NewName { $_.BaseName.replace('.x264','') + $_.Extension }"
powershell -command "get-childitem -recurse | dir -Filter *.mp4 | Rename-Item -NewName { $_.BaseName.replace('.x264','') + $_.Extension }"

powershell -command "get-childitem -recurse | dir -Filter *.mkv | Rename-Item -NewName { $_.BaseName.replace('.264','') + $_.Extension }"
powershell -command "get-childitem -recurse | dir -Filter *.mp4 | Rename-Item -NewName { $_.BaseName.replace('.264','') + $_.Extension }"

powershell -command "get-childitem -recurse | dir -Filter *.mkv | Rename-Item -NewName { $_.BaseName.replace('.x265','') + $_.Extension }"
powershell -command "get-childitem -recurse | dir -Filter *.mp4 | Rename-Item -NewName { $_.BaseName.replace('.x265','') + $_.Extension }"

powershell -command "get-childitem -recurse | dir -Filter *.mkv | Rename-Item -NewName { $_.BaseName.replace('.PROPER','') + $_.Extension }"
powershell -command "get-childitem -recurse | dir -Filter *.mp4 | Rename-Item -NewName { $_.BaseName.replace('.PROPER','') + $_.Extension }"

powershell -command "get-childitem -recurse | dir -Filter *.mkv | Rename-Item -NewName { $_.BaseName.replace('.[rarbg]','') + $_.Extension }"
powershell -command "get-childitem -recurse | dir -Filter *.mp4 | Rename-Item -NewName { $_.BaseName.replace('.[rarbg]','') + $_.Extension }"

powershell -command "get-childitem -recurse | dir -Filter *.mkv | Rename-Item -NewName { $_.BaseName.replace('.[rartv]','') + $_.Extension }"
powershell -command "get-childitem -recurse | dir -Filter *.mp4 | Rename-Item -NewName { $_.BaseName.replace('.[ratv]','') + $_.Extension }"

powershell -command "get-childitem -recurse | dir -Filter *.mkv | Rename-Item -NewName { $_.BaseName.replace('-NTb','') + $_.Extension }"
powershell -command "get-childitem -recurse | dir -Filter *.mp4 | Rename-Item -NewName { $_.BaseName.replace('-NTb','') + $_.Extension }"

powershell -command "get-childitem -recurse | dir -Filter *.mkv | Rename-Item -NewName { $_.BaseName.replace('-GHOSTS','') + $_.Extension }"
powershell -command "get-childitem -recurse | dir -Filter *.mp4 | Rename-Item -NewName { $_.BaseName.replace('-GHOSTS','') + $_.Extension }"

powershell -command "get-childitem -recurse | dir -Filter *.mkv | Rename-Item -NewName { $_.BaseName.replace('-TVSmash','') + $_.Extension }"
powershell -command "get-childitem -recurse | dir -Filter *.mp4 | Rename-Item -NewName { $_.BaseName.replace('-TVSmash','') + $_.Extension }"

powershell -command "get-childitem -recurse | dir -Filter *.mkv | Rename-Item -NewName { $_.BaseName.replace('-LAZY','') + $_.Extension }"
powershell -command "get-childitem -recurse | dir -Filter *.mp4 | Rename-Item -NewName { $_.BaseName.replace('-LAZY','') + $_.Extension }"

powershell -command "get-childitem -recurse | dir -Filter *.mkv | Rename-Item -NewName { $_.BaseName.replace('iT','') + $_.Extension }"
powershell -command "get-childitem -recurse | dir -Filter *.mp4 | Rename-Item -NewName { $_.BaseName.replace('iT','') + $_.Extension }"

powershell -command "get-childitem -recurse | dir -Filter *.mkv | Rename-Item -NewName { $_.BaseName.replace('.REPACK','') + $_.Extension }"
powershell -command "get-childitem -recurse | dir -Filter *.mp4 | Rename-Item -NewName { $_.BaseName.replace('.REPACK','') + $_.Extension }"

powershell -command "get-childitem -recurse | dir -Filter *.mkv | Rename-Item -NewName { $_.BaseName.replace('.INTERNAL','') + $_.Extension }"
powershell -command "get-childitem -recurse | dir -Filter *.mp4 | Rename-Item -NewName { $_.BaseName.replace('.INTERNAL','') + $_.Extension }"

powershell -command "get-childitem -recurse | dir -Filter *.mkv | Rename-Item -NewName { $_.BaseName.replace('.DDP5.1','') + $_.Extension }"
powershell -command "get-childitem -recurse | dir -Filter *.mp4 | Rename-Item -NewName { $_.BaseName.replace('.DDP5.1','') + $_.Extension }"

powershell -command "get-childitem -recurse | dir -Filter *.mkv | Rename-Item -NewName { $_.BaseName.replace('.DDP2.0','') + $_.Extension }"
powershell -command "get-childitem -recurse | dir -Filter *.mp4 | Rename-Item -NewName { $_.BaseName.replace('.DDP2.0','') + $_.Extension }"

powershell -command "get-childitem -recurse | dir -Filter *.mkv | Rename-Item -NewName { $_.BaseName.replace('.AAC2.0','') + $_.Extension }"
powershell -command "get-childitem -recurse | dir -Filter *.mp4 | Rename-Item -NewName { $_.BaseName.replace('.AAC2.0','') + $_.Extension }"

powershell -command "get-childitem -recurse | dir -Filter *.mkv | Rename-Item -NewName { $_.BaseName.replace('.HDTV','') + $_.Extension }"
powershell -command "get-childitem -recurse | dir -Filter *.mp4 | Rename-Item -NewName { $_.BaseName.replace('.HDTV','') + $_.Extension }"

powershell -command "get-childitem -recurse | dir -Filter *.mkv | Rename-Item -NewName { $_.BaseName.replace('.BDRIP','') + $_.Extension }"
powershell -command "get-childitem -recurse | dir -Filter *.mp4 | Rename-Item -NewName { $_.BaseName.replace('.BDRIP','') + $_.Extension }"

powershell -command "get-childitem -recurse | dir -Filter *.mkv | Rename-Item -NewName { $_.BaseName.replace('.BRRIP','') + $_.Extension }"
powershell -command "get-childitem -recurse | dir -Filter *.mp4 | Rename-Item -NewName { $_.BaseName.replace('.BRRIP','') + $_.Extension }"

powershell -command "get-childitem -recurse | dir -Filter *.mkv | Rename-Item -NewName { $_.BaseName.replace('.BluRay','') + $_.Extension }"
powershell -command "get-childitem -recurse | dir -Filter *.mp4 | Rename-Item -NewName { $_.BaseName.replace('.BluRay','') + $_.Extension }"

powershell -command "get-childitem -recurse | dir -Filter *.mkv | Rename-Item -NewName { $_.BaseName.replace('-','') + $_.Extension }"
powershell -command "get-childitem -recurse | dir -Filter *.mp4 | Rename-Item -NewName { $_.BaseName.replace('-','') + $_.Extension }"


REM These commands will be used to rename the season/episode formatting to include a space
powershell -command "get-childitem -recurse | dir -Filter *.mkv | Rename-Item -NewName { $_.BaseName.replace('.S01E',' S01 E') + $_.Extension }"
powershell -command "get-childitem -recurse | dir -Filter *.mp4 | Rename-Item -NewName { $_.BaseName.replace('.S01E',' S01 E') + $_.Extension }"

powershell -command "get-childitem -recurse | dir -Filter *.mkv | Rename-Item -NewName { $_.BaseName.replace('.S02E',' S02 E') + $_.Extension }"
powershell -command "get-childitem -recurse | dir -Filter *.mp4 | Rename-Item -NewName { $_.BaseName.replace('.S02E',' S02 E') + $_.Extension }"

powershell -command "get-childitem -recurse | dir -Filter *.mkv | Rename-Item -NewName { $_.BaseName.replace('.S03E',' S03 E') + $_.Extension }"
powershell -command "get-childitem -recurse | dir -Filter *.mp4 | Rename-Item -NewName { $_.BaseName.replace('.S03E',' S03 E') + $_.Extension }"

powershell -command "get-childitem -recurse | dir -Filter *.mkv | Rename-Item -NewName { $_.BaseName.replace('.S04E',' S04 E') + $_.Extension }"
powershell -command "get-childitem -recurse | dir -Filter *.mp4 | Rename-Item -NewName { $_.BaseName.replace('.S04E',' S04 E') + $_.Extension }"

powershell -command "get-childitem -recurse | dir -Filter *.mkv | Rename-Item -NewName { $_.BaseName.replace('S05E',' S05 E') + $_.Extension }"
powershell -command "get-childitem -recurse | dir -Filter *.mp4 | Rename-Item -NewName { $_.BaseName.replace('S05E',' S05 E') + $_.Extension }"

powershell -command "get-childitem -recurse | dir -Filter *.mkv | Rename-Item -NewName { $_.BaseName.replace('.S06E',' S06 E') + $_.Extension }"
powershell -command "get-childitem -recurse | dir -Filter *.mp4 | Rename-Item -NewName { $_.BaseName.replace('.S06E',' S06 E') + $_.Extension }"

powershell -command "get-childitem -recurse | dir -Filter *.mkv | Rename-Item -NewName { $_.BaseName.replace('.S07E',' S07 E') + $_.Extension }"
powershell -command "get-childitem -recurse | dir -Filter *.mp4 | Rename-Item -NewName { $_.BaseName.replace('.S07E',' S07 E') + $_.Extension }"

powershell -command "get-childitem -recurse | dir -Filter *.mkv | Rename-Item -NewName { $_.BaseName.replace('.S08E',' S08 E') + $_.Extension }"
powershell -command "get-childitem -recurse | dir -Filter *.mp4 | Rename-Item -NewName { $_.BaseName.replace('.S08E',' S08 E') + $_.Extension }"

powershell -command "get-childitem -recurse | dir -Filter *.mkv | Rename-Item -NewName { $_.BaseName.replace('.S09E',' S09 E') + $_.Extension }"
powershell -command "get-childitem -recurse | dir -Filter *.mp4 | Rename-Item -NewName { $_.BaseName.replace('.S09E',' S09 E') + $_.Extension }"

powershell -command "get-childitem -recurse | dir -Filter *.mkv | Rename-Item -NewName { $_.BaseName.replace('.S10E',' S10 E') + $_.Extension }"
powershell -command "get-childitem -recurse | dir -Filter *.mp4 | Rename-Item -NewName { $_.BaseName.replace('.S10E',' S10 E') + $_.Extension }"

powershell -command "get-childitem -recurse | dir -Filter *.mkv | Rename-Item -NewName { $_.BaseName.replace('.S11E',' S11 E') + $_.Extension }"
powershell -command "get-childitem -recurse | dir -Filter *.mp4 | Rename-Item -NewName { $_.BaseName.replace('.S11E',' S11 E') + $_.Extension }"

powershell -command "get-childitem -recurse | dir -Filter *.mkv | Rename-Item -NewName { $_.BaseName.replace('.S12E',' S12 E') + $_.Extension }"
powershell -command "get-childitem -recurse | dir -Filter *.mp4 | Rename-Item -NewName { $_.BaseName.replace('.S12E',' S12 E') + $_.Extension }"

powershell -command "get-childitem -recurse | dir -Filter *.mkv | Rename-Item -NewName { $_.BaseName.replace('.S13E',' S13 E') + $_.Extension }"
powershell -command "get-childitem -recurse | dir -Filter *.mp4 | Rename-Item -NewName { $_.BaseName.replace('.S13E',' S13 E') + $_.Extension }"

powershell -command "get-childitem -recurse | dir -Filter *.mkv | Rename-Item -NewName { $_.BaseName.replace('.S14E',' S14 E') + $_.Extension }"
powershell -command "get-childitem -recurse | dir -Filter *.mp4 | Rename-Item -NewName { $_.BaseName.replace('.S14E',' S14 E') + $_.Extension }"

powershell -command "get-childitem -recurse | dir -Filter *.mkv | Rename-Item -NewName { $_.BaseName.replace('.S15E',' S15 E') + $_.Extension }"
powershell -command "get-childitem -recurse | dir -Filter *.mp4 | Rename-Item -NewName { $_.BaseName.replace('.S15E',' S15 E') + $_.Extension }"

REM These commands will remove the dots and try to clean up the spacing if it's gone funny
powershell -command "get-childitem -recurse | dir -Filter *.mkv | Rename-Item -NewName { $_.BaseName.replace('.',' ') + $_.Extension }"
powershell -command "get-childitem -recurse | dir -Filter *.mp4 | Rename-Item -NewName { $_.BaseName.replace('.',' ') + $_.Extension }"

powershell -command "get-childitem -recurse | dir -Filter *.mkv | Rename-Item -NewName { $_.BaseName.replace('  ',' ') + $_.Extension }"
powershell -command "get-childitem -recurse | dir -Filter *.mp4 | Rename-Item -NewName { $_.BaseName.replace('  ',' ') + $_.Extension }"

powershell -command "get-childitem -recurse | dir -Filter *.mkv | Rename-Item -NewName { $_.BaseName.replace('   ',' ') + $_.Extension }"
powershell -command "get-childitem -recurse | dir -Filter *.mp4 | Rename-Item -NewName { $_.BaseName.replace('   ',' ') + $_.Extension }"

REM This command will then move the newly named mp4 or mkv files to their final location. This works the same as cut paste. NOT copy and paste.
Robocopy "E:\Downloads\To Transfer" Z: /s /mov
I've input remarks (REM) into this script to try and help you along they way. Anything with REM will be skipped when the code runs.

---------------------------------

Let's break it down

The 1st command is:
E:
This will change the current directory to the E: drive (this is because I download to an external drive) if you download to your computer, you will normally have a C: drive. If you do, just delete the E: command as it won't apply to you.

The 2nd command is:
cd "\Downloads\To Transfer"
This will change to command prompts location to your download location. If you want to use this script I would recommend setting your downloads to temporarily stay in one location, then once finished, move them to the location the script will run. This will stop it from messing with your on going downloads.
If you are on a regular computer your location might be something like C:\Username\Downloads\Final_Download_Folder

The 3rd set of commands are:
del /s/f/q *.txt
del /s/f/q *.jpg
del /s/f/q *.nfo
del /s/f/q *.exe

These commands will quietly and forcefully delete all text, jpeg, info and executable files all located in your final download location. Most torrent's I download always have all of the above and since it qbittorrent is running pretty much in full auto, I can't be bothered to delete these files each time.

BE SUPER CAREFUL as if you accidentally run this command in the wrong place you could delete files on your drive by accident. If you're uncomfortable, just delete those lines.

The 4th set of commands are:
powershell -command "get-childitem -recurse | dir -Filter *.mkv | Rename-Item -NewName { $_.BaseName.replace('.1080p','your output') + $_.Extension }"

powershell -command "get-childitem -recurse | dir -Filter *.mp4 | Rename-Item -NewName { $_.BaseName.replace('.1080p','your output') + $_.Extension }"

etc, etc.

What these commands do is look in each folder in the final download location that end with a .mp4 and .mkv extension. It will then try to rename the file using words you specify. In the example above it will look for .1080p (as I said, most of my downloads have dots instead of spaces) and replace it with nothing.

It will run through trying to remove .x265 .x264 .1080p .720p .AAC2.0 and more. If you want to add more tags just copy the lines above and change what I've highlighted in italics above. The need to be in single quotes. You can add however many you like. Keep in mind the more you have the longer it may take. Currently it will take about 15 seconds to run through.

The final bit of code is:
Robocopy "E:\Downloads\To Transfer" Z: /s /mov

This will use Robocopy which is robust file copying built into Windows to move your newly renamed files (if you want to move them) to a different location.

For me they move the files from E:\Downloads\To Transfer which is my final download location, to a network drive which is mapped to Z:. if you want to change this for yourself, just edit the italics.

As this uses the move command with Robocopy, it will basically cut and paste from the old location to the new location. Only leaving behind an empty folder. Which you can delete manually, or like me, set up a script to clean this directory once a week.

---------------------------------

If you made it all the way down here, Bravo!

I've been using this script to move files for many months without issues, I've just added the renaming part today and it's already working well and saving me time!

It will take a file that looks like this:
Videofile.S08E11.1080p.WEBRip.DDP5.1.x264-NTb.mkv

and rename it to:
Videofile S08 E11.mkv

Your results will vary as I haven't included all tags, so you'll need to add them as you go.

For someone like me that relies on VSS to automate my downloads, then automatically rename and move them, this is a big time saver.

If you are unfamiliar with command line I would recommend you play with the script in parts to make sure you know how it works, don't just jump in if you're unsure.

Anyway, thanks for reading and I hope this manages to help some of you automate a little bit or inspire you with your own little scripts. As I said, this is quite specific to my use case but yeah... thought I'd post if for you guys.
Robolov

Re: Automate File Renaming and Moving With Scripts - Guide

Post by Robolov »

That's pretty sweet, thanks for sharing your script
insanebrain

Re: Automate File Renaming and Moving With Scripts - Guide

Post by insanebrain »

Hey, no problem, there might be someone out there someday that might need something like this.

While I've been tinkering I've found that you can link renaming methods in 1 command. Meaning that I can put all the tags in 4 commands rather than having 40 or so lines.

Not only does it look neater, the run time of the script has gone from 15-20 seconds to 2-3 seconds.

In order to add more tags just include .replace('.Text_to_replace',Replacement_text'') to the close bracket of the last replace command.

Making sure the very end of the command ends with + $_.Extension }"
Like this:
.replace('.Text_to_replace',Replacement_text'') + $_.Extension }"

If anyone is interested here it is:

Code: Select all

@echo off

REM This changes the drive to the one with my downloads
E:

REM This moves the directory to the final download location
cd "\Downloads\To Transfer"

REM These commands will automatically rename any mp4 or mkv files in the download. It will get rid of the usual download tags. 
REM If you want to add more tags, copy both lines if you want to include mp4 and mkv, then change the name between the 1st ''
REM for example '1080p', then add what you want that to change to in the second set of ''
REM for ecample '' for nothing or ' ' for a space, or 'EXAMPLE TEXT'
REM As the list grows, the batch file will take longer to run through

REM Download Tags
powershell -command "get-childitem -recurse | dir -Filter *.mkv | Rename-Item -NewName { $_.BaseName.replace('.1080p','').replace('.720p','').replace('.WEB-DL','').replace('.WEBRIP','').replace('.WEBRip','').replace('.AMZN','').replace('.HULU','').replace('.NF','').replace('.H264','').replace('.x264','').replace('.264','').replace('.x265','').replace('.PROPER','').replace('.[rarbg]','').replace('.[rartv]','').replace('-wutang','').replace('-NTb','').replace('-GHOSTS','').replace('-TVSmash','').replace('-LAZY','').replace('-AJP69','').replace('iT','').replace('.REPACK','').replace('.INTERNAL','').replace('.DDP5.1','').replace('.DDP2.0','').replace('.AAC2.0','').replace('.HDTV','').replace('.BDRIP','').replace('.BRRIP','').replace('.BluRay','').replace('.bluray','').replace('.2020','').replace('-','') + $_.Extension }"

powershell -command "get-childitem -recurse | dir -Filter *.mp4 | Rename-Item -NewName { $_.BaseName.replace('.1080p','').replace('.720p','').replace('.WEB-DL','').replace('.WEBRIP','').replace('.WEBRip','').replace('.AMZN','').replace('.HULU','').replace('.NF','').replace('.H264','').replace('.x264','').replace('.264','').replace('.x265','').replace('.PROPER','').replace('.[rarbg]','').replace('.[rartv]','').replace('-wutang','').replace('-NTb','').replace('-GHOSTS','').replace('-TVSmash','').replace('-LAZY','').replace('-AJP69','').replace('iT','').replace('.REPACK','').replace('.INTERNAL','').replace('.DDP5.1','').replace('.DDP2.0','').replace('.AAC2.0','').replace('.HDTV','').replace('.BDRIP','').replace('.BRRIP','').replace('.BluRay','').replace('.bluray','').replace('.2020','').replace('-','') + $_.Extension }"



REM Season Tags
powershell -command "get-childitem -recurse | dir -Filter *.mkv | Rename-Item -NewName { $_.BaseName.replace('.S01E',' S01 E').replace('.S02E',' S02 E').replace('.S03E',' S03 E').replace('.S04E',' S04 E').replace('.S05E',' S05 E').replace('.S06E',' S06 E').replace('.S07E',' S07 E').replace('.S08E',' S08 E').replace('.S09E',' S09 E').replace('.S10E',' S10 E').replace('.S11E',' S11 E').replace('.S12E',' S12 E').replace('.S13E',' S13 E').replace('.S14E',' S14 E').replace('.S15E',' S15 E') + $_.Extension }"

powershell -command "get-childitem -recurse | dir -Filter *.mp4 | Rename-Item -NewName { $_.BaseName.replace('.S01E',' S01 E').replace('.S02E',' S02 E').replace('.S03E',' S03 E').replace('.S04E',' S04 E').replace('.S05E',' S05 E').replace('.S06E',' S06 E').replace('.S07E',' S07 E').replace('.S08E',' S08 E').replace('.S09E',' S09 E').replace('.S10E',' S10 E').replace('.S11E',' S11 E').replace('.S12E',' S12 E').replace('.S13E',' S13 E').replace('.S14E',' S14 E').replace('.S15E',' S15 E') + $_.Extension }"


REM Clean Up
powershell -command "get-childitem -recurse | dir -Filter *.mkv | Rename-Item -NewName { $_.BaseName.replace('.',' ').replace('  ',' ').replace('   ',' ').replace(' H','').replace('[rarbg]','') + $_.Extension }"
powershell -command "get-childitem -recurse | dir -Filter *.mp4 | Rename-Item -NewName { $_.BaseName.replace('.',' ').replace('  ',' ').replace('   ',' ').replace(' H','').replace('[rarbg]','') + $_.Extension }"

REM This command will then move the newly named mp4 or mkv files to their final location. This works the same as cut paste. NOT copy and paste.
Robocopy "E:\Downloads\To Transfer" Z: /s /mov
I've separated them into Download Tags, Season Tags and Cleaning commands.
dragonflyoh

Re: Automate File Renaming and Moving With Scripts - Guide

Post by dragonflyoh »

I think this is what I need. I download movies and TV shows and transfer them to Movie and TV show folders for viewing with Kodi on my TV. I have downloaded your code and will see if I can make it work for me. Thank you for your work!!!
insanebrain

Re: Automate File Renaming and Moving With Scripts - Guide

Post by insanebrain »

Hey, no trouble. ^-^

This is mainly for renaming TV shows into a better name the. Move them.

It would be best to test it with some smaller torrents and see how it goes.
firekid

Re: Automate File Renaming and Moving With Scripts - Guide

Post by firekid »

could you help me a bit about batch script. i have folders has suffix name, i want to remove the suffix.

like following folder name:

Code: Select all

Naruto [abc.com]
Bleach [abc.com]
is there any command to remove [abc.com] from folder name?
insanebrain

Re: Automate File Renaming and Moving With Scripts - Guide

Post by insanebrain »

Annoyingly folders are a bit more complicated to do in batch.

You could use a move command (renaming with wildcards * don't work on folders)

Code: Select all

move "C:\Downloads\Test\*ABC.COM" C:\Downloads\Test\Final
There are 2 parts to edit, the main directory your sub directories are in, then you need to change after the wildcard * so if I wanted to rename all folders with ABC.COM I would use *ABC.COM

The last part is the new name of the folder. The issue with this is that it can only rename to one folder... So I wouldn't use this.

If you needed to rename a bunch or directories I would use this as a one off:
https://www.bulkrenameutility.co.uk/

This application looks crazy with all the controls, but you can bulk rename folders and remove the text you want.

To do this, install it and run the application. Then use the file explorer in the app on the left hand side and navigate to the directory with all of the sub folders you want to rename. Then in the settings you want to use Replace (3), in Replace put the text you want to get rid of ABC.COM and leave the 'with' box blank. Now use Filters (12) and tick the sub folders box.

With that done, in the main file window you should see all of the folders you want to rename, highlight them all to select them to rename. Now click PREVIEW, this will show you all of the changes that will be made and what they look like. If you're happy with how it looks, click rename and all of the changes will be made.

If you really want a script to do this, I would recommend asking on Stack Exchange.

Sorry I can't help more with a script.
insanebrain

Re: Automate File Renaming and Moving With Scripts - Guide

Post by insanebrain »

While I'm here I may as well update you with my refined scripts. Unfortunately I can't edit my original post, or see how to.

THIS IS MAINLY FOR RENAMING AND MOVING TV SHOWS, but you can use the moving files part of the script to automate moving stuff if you want.

I've broken my process down into 2 scripts, the 1st cleans out the crap by removing any empty directories (from previous transfers) then it deletes any JPEG, EXE, TXT and NFO files, then it calls a 2nd powershell script to do the heavy lifting of renaming the files and transferring them.

Script 1(add this one into qBittorrent) - Name this what you want, but make sure it's a .bat file
As with my previous scripts you might need to edit this for yourself, I use an external HDD call E: if you are using your default drive you can remove that line and start from the CD line.

Code: Select all

@echo off

REM CHANGES TO E DRIVE
E:

REM CHANGES TO DIRECTORY
cd "\Downloads\To Transfer"

REM REMOVES EMPTY DIRECTORIES
ROBOCOPY "\Downloads\To Transfer" "\Downloads\To Transfer" /S /MOVE

REM DELETES JUNK
del /s/f/q *.txt
del /s/f/q *.jpg
del /s/f/q *.nfo
del /s/f/q *.exe

REM CALLS CLEAN PS1 CHANGE THE LOCATION TO THE LOCATION OF 2ND SCRIPT
powershell -executionpolicy bypass "C:\Scripts\2_Second_Clean.ps1"
Script 2 - Clean and Move - Call this what you like, but save it as a .PS1 file, then edit the last line of the 1st script being sure to put the location of the PS1 file.
This will remove dots from the file name, reformat the name for S01 E01 etc, then remove everything after S01 E01, for example:
TV.Show.S01.E01.Episode name > TV Show S01 E01

It will then move the files. Again, you'll need to configure this for your setup. You'll need to edit the MOVES FILES and REMOVES EMPTY DIRECTORIES sections. If you don't care about removing empty directories you can delete REMOVES EMPTY DIRECTORIES all together.

Code: Select all

#THIS PART CLEANS UP THE FILE NAMES
cd "E:\Downloads\To Transfer"

#REMOVE DOTS
get-childitem -recurse | dir -Filter *.mp4 | Rename-Item -NewName { $_.BaseName.replace('.',' ') + $_.Extension }
get-childitem -recurse | dir -Filter *.mkv | Rename-Item -NewName { $_.BaseName.replace('.',' ') + $_.Extension }

#SPACE OUT SEASONS AND EPISODES - UPPER CASE
get-childitem -recurse | dir -Filter *.mp4 | Rename-Item -NewName { $_.BaseName.replace(' S01E',' S01 E').replace(' S02E',' S02 E').replace(' S03E',' S03 E').replace(' S04E',' S04 E').replace(' S05E',' S05 E').replace(' S06E',' S06 E').replace(' S07E',' S07 E').replace(' S08E',' S08 E').replace(' S09E',' S09 E').replace(' S10E',' S10 E').replace(' S11E',' S11 E').replace(' S12E',' S12 E').replace(' S13E',' S13 E').replace(' S14E',' S14 E').replace(' S15E',' S15 E') + $_.Extension }
get-childitem -recurse | dir -Filter *.mkv | Rename-Item -NewName { $_.BaseName.replace(' S01E',' S01 E').replace(' S02E',' S02 E').replace(' S03E',' S03 E').replace(' S04E',' S04 E').replace(' S05E',' S05 E').replace(' S06E',' S06 E').replace(' S07E',' S07 E').replace(' S08E',' S08 E').replace(' S09E',' S09 E').replace(' S10E',' S10 E').replace(' S11E',' S11 E').replace(' S12E',' S12 E').replace(' S13E',' S13 E').replace(' S14E',' S14 E').replace(' S15E',' S15 E') + $_.Extension }

#SPACE OUT SEASONS AND EPISODES - LOWER CASE NAMING TO UPPER CASE
get-childitem -recurse | dir -Filter *.mp4 | Rename-Item -NewName { $_.BaseName.replace(' s01e',' S01 E').replace(' s02e',' S02 E').replace(' s03e',' S03 E').replace(' s04e',' S04 E').replace(' s05e',' S05 E').replace(' s06e',' S06 E').replace(' s07e',' S07 E').replace(' s08e',' S08 E').replace(' s09e',' S09 E').replace(' s10e',' S10 E').replace(' s11e',' S11 E').replace(' s12e',' S12 E').replace(' s13e',' S13 E').replace(' s14e',' S14 E').replace(' s15e',' S15 E') + $_.Extension }
get-childitem -recurse | dir -Filter *.mkv | Rename-Item -NewName { $_.BaseName.replace(' s01e',' S01 E').replace(' s02e',' S02 E').replace(' s03e',' S03 E').replace(' s04e',' S04 E').replace(' s05e',' S05 E').replace(' s06e',' S06 E').replace(' s07e',' S07 E').replace(' s08e',' S08 E').replace(' s09e',' S09 E').replace(' s10e',' S10 E').replace(' s11e',' S11 E').replace(' s12e',' S12 E').replace(' s13e',' S13 E').replace(' s14e',' S14 E').replace(' s15e',' S15 E') + $_.Extension }

#CLEAN UP
get-childitem -recurse | dir -Filter *.mp4 | Rename-Item -NewName { $_.BaseName.replace('     ',' ').replace('    ',' ').replace('   ',' ').replace('  ',' ') + $_.Extension }
get-childitem -recurse | dir -Filter *..mkv | Rename-Item -NewName { $_.BaseName.replace('     ',' ').replace('    ',' ').replace('   ',' ').replace('  ',' ') + $_.Extension }

#REMOVE EVERYTHING AFTER EPISODE
get-childitem -recurse | dir -Filter *.mp4 | Rename-Item -NewName { ($_.BaseName -creplace '(?<=S\d+ E\d+)\D.*') + $_.Extension }
get-childitem -recurse | dir -Filter *.mkv | Rename-Item -NewName { ($_.BaseName -creplace '(?<=S\d+ E\d+)\D.*') + $_.Extension }

#MOVES FILES
Robocopy "E:\Downloads\To Transfer" Z: /s /mov

#REMOVES EMPTY DIRECTORIES
ROBOCOPY "\Downloads\To Transfer" "\Downloads\To Transfer" /S /MOVE

exit
TL;DR
Script 1 does some basic cleanup, then calls Script 2 which does some deep cleaning and renaming, then moves files to different location.

Oh, if you use this and download a bunch of files which finish almost at the same time, this might confuse the script and stop it from working OR you'll have multiple transfers running at once which will slow things down. If you have any issues, you can quit the Powershell process in in the task manager.

I generally have this on a box running 24/7 that does nothings else, so transfer speeds and stuff doesn't matter to me.

And as always, use at your OWN RISK.

Thanks guys.
Post Reply