2

I have this .torrent file list example inside a directory

Le Rane del Mare 1951.torrent
Left Behind - La Profezia 2014.torrent
Lo straordinario viaggio di T.S. Spivet.torrent
Maleficent 2014 [dvd rip].torrent
Mandingo (1975).torrent
Maurice - Versione Integrale 1987.torrent

I want a .bat. This bat must to generate folders extracting year from file name like

1951
2014
1975
1987

Then I want to move files inside folders previously created like this

Le Rane del Mare 1951 --> 1951
Left Behind - La Profezia 2014 --> 2014
Maleficent 2014 --> 2014
Mandingo 1975 --> 1975
Maurice - Versione Integrale 1987 --> 1987

I use Win7

I use this code BUT i must create folder list before. I want create with batch, not manually

    @echo off
setlocal enabledelayedexpansion
pushd %1
for /F "USEBACKQ tokens=*" %%a in (`dir /b /a:-d`) do (
    set "_file=%%a"
    for /D %%b in (*) do (
        if NOT "x!_file:%%b=!" == "x!_file!" (
            move "%%a" "%%b"
        )
    )
)
popd
Kob Teb
  • 161
  • 1
  • 7

2 Answers2

4

Based on your originally provided example this would have done:

@Echo Off
For %%a In (*19???.torrent *20???.torrent) Do Call :Sub "%%~a"
Exit/B
:Sub
Set "mfn=%~n1"
Set "lfc=%mfn:~-5%
Set "lfc=%lfc:(= %"
Set "lfc=%lfc:)= %"
Set "lfc=%lfc: =%"
If Not Exist "%lfc%\" MD %lfc%
Move %1 %lfc%

However you have changed the examples to include characters which follow the year and this solution will no longer work for you. Unfortunately without seeing a listing containing all of your torrent files it will not be possible for a script to idntify the four digits, only two of which are known, and move them all according to your requirements.

Alternative solution:

@Echo Off
For /F "Tokens=*" %%a In (
    'Dir/B/A-D *.torrent^|Findstr/R "\<19[0-9][0-9]\> \<20[0-1][0-9]\>"') Do (
    For %%b In (%%~na) Do Call :Sub "%%a" "%%b")
Pause
Exit/B
:Sub
Set "mfn=%~2"
Set "mfn=%mfn:(=%"
If %mfn:)=% GEq 1900 (If %mfn:)=% Lss 2017 (If Not Exist "%mfn:)=%\" MD %mfn:)=%
        Move %1 %mfn:)=%))
Compo
  • 36,585
  • 5
  • 27
  • 39
  • I test now: your solution is good but your script create folders only. Is possible to move files inside? – Kob Teb Sep 05 '16 at 18:20
  • 1
    I have edited to make it now perform the move, I have also produced an alternative version which may cover a few of the torrent files not touched by this version. – Compo Sep 05 '16 at 18:57
  • @Compo I like the second solution. – Hackoo Sep 06 '16 at 04:24
  • I'm surprised that the `findstr` search string in your second example works because according to [this post](http://stackoverflow.com/a/8844873), word boundaries (`\<` and `\>`) needed to be at the beginning and at the and of the regular expression, resp.; anyway, great solution! – aschipfl Sep 06 '16 at 09:20
0

Same solution in Powershell

Set-Location -Path .

# Get all the files in the current directory that match the pattern "*19???.torrent" or "*20???.torrent"
$files = Get-ChildItem -Filter "*.torrent" | Where-Object {$_.Name -match '19|20'}

# Loop through each file
foreach ($file in $files) {
    # Get the file name and extract the year from the file name
    $year = $file.Name -replace '[^0-9]',''
    # Create the destination folder if it does not exist
    $destination = "$year"
    if (-Not (Test-Path $destination)) {
        New-Item -ItemType Directory -Path $destination
    }
    # Move the file to the destination folder
    Move-Item -Path $file.FullName -Destination $destination
}
Super Sonic
  • 116
  • 9