Batch File to move tv shows using special characters? - batch-file

Trying to figure out an efficient way to sort tv shows from download folder. I
know this is far from the best solution but it's within my comfort zone :)
Anyway, I want the batch file to search the file name and move it to the correct folder
My TV Show folder structure is TV > Show Name > Season > Files
So for example if I have a file named Archer.S01E01.mkv I'd like it moved to TV > Archer > Season 1 etc. I've created a couple variables %source% and %dest% to cut down the amount of space needed as shown below:
::Show: Archer
move %source%*archer*S01* %dest%archer\"season 1"\
move %source%*archer*S02* %dest%archer\"season 2"\
move %source%*archer*S03* %dest%archer\"season 3"\
move %source%*archer*S04* %dest%archer\"season 4"\
move %source%*archer*S05* %dest%archer\"season 5"\
move %source%*archer*S06* %dest%archer\"season 6"\
Is there a way to use an array to move the files to the correct folders? Something along the lines of:
move %source%*archer*S0[1-6]* %dest%archer\"season [1-6]"\ ?
I know that particular example won't work, I'm guessing I'll need some sort of loop? But for the life of me I have no idea how to make that work.
It would be even better if the loop would go through the files and match part of the string to the show so I wouldn't have to create a command for each and every tv show I have in my library.
Any ideas or help would be greatly appreciated!
Thanks

Assuming all files share the same template of "[ShowName].S[SeasonNumber]E[EpisodeNumber].mkv", then the following should create all folders as needed, and move the files. Please note - this is untested.
#echo off
set "source=yourSourceFolder"
set "dest=yourDestinationFolder"
pushd "%source%"
for /f "delims=" %%F in (
'dir /b /a-d *.mkv ^| findstr /rix "[^.]*\.s[0-9]*e[0-9]*\.mkv"'
) do for /f "tokens=1,2 delims=." %%A in ("%%F") do (
for /f "delims=SsEe0" %%S in ("%%B") do (
if not exist "%dest%\%%A\season %%S\" md "%dest%\%%A\season %%S\"
move "%%F" "%dest%\%%A\season %%S\" >nul
)
)

Another way (in place of season it will just create the subdirectorys SO1, SO2,...SO9 in the directory named with the SHOWNAME (Archer in your example) :
#echo off
SET "SOURCE=YOUR\SOURCE PATH"
SET "DEST=YOUR\DESTINATION PATH"
SETLOCAL ENABLEDELAYEDEXPANSION
FOR /F "TOKENS=1,2* DELIMS=." %%A IN ('DIR /B/A-D "%SOURCE%\*.MKV"') DO (
SET $DEST=%%B
SET $DEST=!$DEST:~0,3!
IF NOT EXIST "%DEST%\%%A" MD "%DEST%\%%A"
IF NOT EXIST "%DEST%\%%A\!$DEST!" MD "%DEST%\%%A\!$DEST!"
MOVE "%SOURCE%\%%A.%%B.%%C" "%DEST%\%%A\!$DEST!"
)

Related

Automatically create folders based on unique key

I have various files with the same structure, they begin with a unique key and a hyphen followed by the name of the file. Examples below:
100023525_Document_1
100023525_Document_2
102008006_Document_1
102008006_Document_2
102008006_Document_3
I want to be able to generate folders for each unique key and have the files moved into them automatically. I have found the following code below written in batch that does so but puts each file into its own separate folder.
#echo off
for %%i in (*) do (
if not "%%~ni" == "organize" (
md "%%~ni" && move "%%~i" "%%~ni"
)
)
This puts the following files:
to
I want the code to be able to put the initial files into folders like this, based on the unique key:
How could I modify the initial batch code to do this? Thanks!
Note: I decided to simplify the code here.
There are probably easier and faster ways to do this, but here is one option if your files are all in the same format. You will want to do this from the folder containing your files:
for /f "delims=_" %%b in ('dir /b *.pdf') do (
if not exist %%b\. #mkdir "%%b"
)
for /f "tokens=1,2,3 delims=_" %%b in ('dir /b *.pdf') do (
if not exist "%%b\%%b_%%c_%%d" #echo move "%%b_%%c_%%d" "%%b\%%b_%%c_%%d"
)
For more information on how this works, please read the documentation on for /f. Take note of how the tokens and delimiters work.
This will show you the items it will move and where. Remove #echo in front of move to do the actual move.

Batch script for moving files to correct folder

I am looking for help to construct a .bat script to move PDFs into a predefined folder structure. The file names are structured and relate to where in the folder structure they should be moved to.
For example IRTYCAS001.pdf;
First two letters tell it to move it to the correct country folder (Ireland)
3rd and 4th will tell it what county folder to move it to
5th to 7th will tell it the correct town folder to move it to and
Last 3 digits tell it the land use type folder to move it to
The identifier lengths will always be the same in the file name.
The folder structure looks something like
example of folder structure
With extensions enabled (default) mkdir will create intermediate folders in one step.
So all you've to do is
iterate the files
use substrings to split the filename into parts and
create the folders if not already existing.
#echo off & setlocal EnableDelayedExpansion
set Src=A:\
set Dst=A:\
for /f "delims=" %%A in ('Dir /B "%Src%*.pdf"') do (
Set "File=%%A"
set "Folder=%Dst%\!File:~0,2!\!File:~2,2!\!File:~4,3!\!File:~7,3!\"
if not exist "!Folder!" MD "!Folder!" >NUL
Move "%%A" "!Folder!"
)
Sample tree:
> tree . /f
A:\
└───IR
└───TY
└───CAS
└───001
IRTYCAS001.pdf
you could try something like this:
FOR /F %%i IN ('dir /b c:\temp\*.pdf') DO call :moveFiles %%i
goto :EOF
:moveFiles
set myfile=%1
set part1=%myfile:~0,2%
set part2=%myfile:~2,2%
set part3=%myfile:~4,3%
set part4=%myfile:~7,3%
set dstFolder=C:\temp
if %part1%==IR set dstFolder=%dstFolder%\ireland
REM more options here...
if %part2%==TY set dstFolder=%dstFolder%\tipperary
REM more options here...
if %part3%==CAS set dstFolder=%dstFolder%\cashel
REM more options here...
if %part4%==001 set dstFolder=%dstFolder%\residential
REM more options here...
move /Y %myfile% %dstFolder%

finding textfile name by pattern and moving to newly created folder

I need some help with batch files as I have not done it before. I need to find textfiles with a matching pattern and move them to a class folder. I have a couple of folders that I am browsing through. Can anyone help me to modify the code to move the textfiles together?
This is what i research and piece together.
#echo off
setlocal enabledelayedexpansion
set file=name.txt
set foldername=class
set location=blockB
for /f "tokens=* delims=" %%a in (!file!) do (
set folder=%%a\public\
if not exists "%CD%\!folder!\!foldername!" (mkdir "%CD%\!folder!\!foldername!") ELSE (call)
dir /b "!folder!" | findstr /r /c"!location!"
)
my name.txt contains (adam,ben,charlie) 1 name per each line. So technically i want to move text files in (\adam\public) which contains blockB in the textfile name to a newly created folder call class (\adam\public\class). i want it to do the same for (\ben\public) and (\charlie\public). If I have any mistake in the way i code pls pardon me. Thanks.
Reason why I did not use a full path is because I am going to use it on different computers with same folder configurations.
I guess you are trying to accomplish this:
#echo off
setlocal EnableExtensions EnableDelayedExpansion
set "FILE=name.txt"
set "FOLDERNAME=class"
set "LOCATION=blockB"
for /F "usebackq eol=| delims=" %%F in ("!FILE!") do (
set "FOLDER=%%~fF\public"
mkdir "!FOLDER!\!FOLDERNAME!"
move "!FOLDER!\*!LOCATION!*.txt" "!FOLDER!\!FOLDERNAME!\"
)
endlocal

Batch programing to search some file in sub folders

I have hundreds of csv files . csv files are stored in folders and sub ​​folders . I want to search fifty csv file whose file names have been determined , for example 1.csv , 2.csv , 3.csv , ... , 50.csv . very troublesome if I searched one by one using the Windows search tool . I would like if the files are found , save in the folder named FOUND . please help to overcome this problem by using the batch programming / bat ? thank you very much
There's a number of approaches one can take, depending on how much automation you require... To help you get started, you may want to look at this it helped me (and indeed continues to do so) when I started learning batch. Furthermore I will provide one possible template for achieving your objective, as I have interpreted it. Perhaps it is not the most elegant or efficient method, but it introduces a number of batch commands that you may or may not have encountered, which in turn may help you develop your own method.
#echo off
setlocal enabledelayedexpansion
echo Please enter a drive letter:
set /p "drive=>"
echo Please enter a search string:
set /p "searchstring=>"
echo %searchstring%>search.txt
set /p search=<search.txt
set /a suffix=0
echo.>>search.txt
:LOOP
for /f "tokens=*" %%i in ("search.txt") do (
set /a suffix=suffix+1
set seq=%search% !suffix!
echo !seq!>>search.txt
)
if !suffix! leq 49 goto LOOP
for /f "tokens=*" %%i in (search.txt) do (
for /f "tokens=*" %%j in ('dir /b /s /a-d %drive%:\"%%i.csv" 2^>nul') do (
if not exist "%~dp0\found" md "%~dp0\found"
move /y "%%j" "%~dp0\found\%%~nxj"
)
)
pause
This is not intended as a definitive solution, though you may find it answers your original query/request. All the best.
Here's another working solution for you..
#ECHO OFF
SETLOCAL EnableDelayedExpansion
REM First Set your directories input and output
SET InputDir=C:\Directory to your CSV files\
SET OutputDir=C:\Directory to your CSV files\FOUND
REM check if the FOUND directory exist, if not, then create it.
IF NOT EXIST OutputDir (
mkdir %OutputDir%
)
REM Grab a scan of the input directory and save it to a temporary file list.
Dir /a /b %InputDir%>"%OutputDir%\Found.txt"
REM Set the files you would like to find.
SET "File1=1.csv"
SET "File2=2.csv"
SET "File3=50.csv"
REM The loop, to process the matching file(s).
FOR %%A IN (%File1%,%File2%,%File3%) DO (
FOR /F "usebackq" %%B IN ("%OutputDir%\Found.txt") DO (
IF %%A==%%B (
copy "%InputDir%\%%A" "%OutputDir%\%%A"
)
)
)
REM Clean up the temp file list.
DEL "%OutputDir%\Found.txt"
Make note, I didn't add quotes to the Input and Output variables, but instead added quotes to the copy portion of the code to compensate for white spaces in your directory path. I tried to keep it simple, so you could follow the logic of how it processed what you are looking for, you can now modify this to your liking.. Have fun. Cheers!

How can I append pictures which is not exist on folder?

I want to write .bat file,
I have two folders which names are A and B.
I have pictures on A , and I want to transfer them from A to B in every 10 minutes,
But I want to transfer the last 20 pictures. Pictures names are 1.jpg 2.jpg ,,,,90.jpg
How can I do that? Is it possible?
thanks
#ECHO OFF
SETLOCAL
SET "sourcedir=c:\sourcedir"
SET "destdir=c:\destdir"
SET /a numbertomove=20
FOR /f "tokens=1*delims=:" %%a IN (
'dir /b /a-d /o-d "%sourcedir%\*.jpg"^|findstr /n "."'
) DO (
IF %%a leq %numbertomove% ECHO MOVE "%sourcedir%\%%b" "%destdir%\"
)
GOTO :EOF
The required MOVE commands are merely ECHOed for testing purposes. After you've verified that the commands are correct, change ECHO MOVE to MOVE to actually move the files. Append >nul to suppress report messages (eg. 1 file moved)
Edited to remove self-scheduling.
to move the files you can use:
for /l %%i in (71,1,90) do move a\%%i.jpg b\%%i.jpg
For scheduling a task to run every 10 minutes see schtasks /create /?.
There are a lot of options, collect them to your needs.
It's a bit confusing to get the right syntax if you do it the first time, so you may have to ask a new question if you know, which options you want to use.
(NOTE: to use the forconstruct on commandline (not in batch) use single% instead of %%)

Resources