0

Trying to organize a shows *.nfo files so they have the same name as the mkv(Edit:avi woops doesn't really change code) they match.

Test Folder:
76107.2.1.nfo 
76107.2.2.nfo
76107.2.3.nfo
Doctor Who - S02E01 (009) - Planet of Giants (1) - Planet of Giants.avi
Doctor Who - S02E01 (009) - Planet of Giants (2) - Dangerous Journey.avi
Doctor Who - S02E01 (009) - Planet of Giants (3) - Crisis.avi

Now the episode numbers in the 76107.2.*.nfo's are wrong in later folders the similarity in the test folder is only because it is the first of the season. so I can't simply compare names to match them up but they are in the right order so I'm trying to use counters to rename the first *.nfo the same as the first *.avi and then the second *.nfo the same as the second *.avi and so on!

I made a script I thought should work but it's doing nothing. Any help would be much appreciated! Edit It now works thanks to Aacini except for an odd bug noted under code.

@echo on
setlocal enableDelayedExpansion

set cnta=0
Set cntb=0
for %%b in (*.avi) do (
 set /a cnta=cnta+1
 set line_!cnta!=%%~Nb
)

for %%c in (*.nfo) do (
 set /a cntb=cntb+1
 for %%i in (!cntb!) do ren "%%~fc" "!line_%%i!.nfo"
)

Bug: The First nfo seems to get bumped to the end of the list but its place still kept. As in every other nfo is named to match its appropriate avi.

Test 1 (Original Test Folder) Results:
.nfo
Doctor Who - S02E01 (009) - Planet of Giants (1) - Planet of Giants.avi
Doctor Who - S02E01 (009) - Planet of Giants (2) - Dangerous Journey.avi
Doctor Who - S02E01 (009) - Planet of Giants (2) - Dangerous Journey.nfo
Doctor Who - S02E01 (009) - Planet of Giants (3) - Crisis.avi
Doctor Who - S02E01 (009) - Planet of Giants (3) - Crisis.nfo

Test 2 (Added Extra Bonus File) Results:
Doctor Who - S02E01 (009) - Planet of Giants (1) - Planet of Giants.avi
Doctor Who - S02E01 (009) - Planet of Giants (2) - Dangerous Journey.avi
Doctor Who - S02E01 (009) - Planet of Giants (2) - Dangerous Journey.nfo
Doctor Who - S02E01 (009) - Planet of Giants (3) - Crisis.avi
Doctor Who - S02E01 (009) - Planet of Giants (3) - Crisis.nfo
Doctor Who - S02E01 (009) - Planet of Giants (4) - The Making of Planet of Giants.avi
Doctor Who - S02E01 (009) - Planet of Giants (4) - The Making of Planet of Giants.nfo

Some folders will contain bonus videos at the end of the list that do not have matching nfo's but they are always at the end. I read the nfo's to verify only the ".nfo"(Test 1)/"Doctor Who - S02E01 (009) - Planet of Giants (4) - The Making of Planet of Giants.nfo"(Test 2) were misplaced they should of been "Doctor Who - S02E01 (009) - Planet of Giants (1) - Planet of Giants.nfo"

Dracrius
  • 7
  • 1
  • 1
  • 7
  • You might have better luck by sorting your file list with `dir` options. But I don't know if you're going to have an environment space problems with a long list of variables. – shawnt00 Mar 20 '15 at 22:54
  • You also need a double expansion of variables in that second loop. Maybe this will help: http://stackoverflow.com/questions/1199931/how-expand-a-cmd-shell-variable-twice-recursively – shawnt00 Mar 20 '15 at 22:57
  • This is probably a better reference: http://stackoverflow.com/questions/7882395/why-is-delayed-expansion-in-a-batch-file-not-working-in-this-case – shawnt00 Mar 20 '15 at 22:59

3 Answers3

2

The right term to name your line_!cnta! variables is array. You may review a complete description of array management in Batch files at this post. For example, your code may be fixed this way:

EDIT: Small bug fixed

@echo on
setlocal enableDelayedExpansion

set cnta=0
Set cntb=0

for %%b in (*.mkv) do (
 set /a cnta=cnta+1
 set line_!cnta!=%%~Nb
)

for /F "delims=" %%c in ('dir /B *.nfo') do (
 set /a cntb=cntb+1
 for %%i in (!cntb!) do ren "%%~fc" "!line_%%i!.nfo"
)
Community
  • 1
  • 1
Aacini
  • 65,180
  • 12
  • 72
  • 108
  • I'd argue that a better name would be "array-like," since it's not a true array. It's the same as the difference between a folder containing a bunch of files and an archive file containing those same files that doesn't use any compression. – SomethingDark Mar 21 '15 at 01:55
  • @Dracrius: Excuse me, but the rename logic is the same you posted in your question; I just fixed the point on the access to the array elements. If you want that we also fix the rename logic, then you must post a list of files that cause the error and the desired result. Please, edit your _question_ and place the list of names inside a code tag, do NOT post list of names here, in comments... – Aacini Mar 21 '15 at 01:56
  • @SomethingDark: I disagree. An array is a _data structure_: a variable comprised of a series of elements that are accessed via a numeric index or subscript. If certain data uses this structure, it is an array; if not, then is not. You don't need a given programming language or a specific notation (even nor a computer!) to manage an array... – Aacini Mar 21 '15 at 02:11
  • True, but batch arrays are just a group of variables that happen to have similar names and are otherwise unrelated. – SomethingDark Mar 21 '15 at 02:14
  • @SomethingDark: You are talking about the _implementation details_ of arrays in a Batch file! These details make this data structure to not be an "array"? In such a case, there are NOT arrays in assembly language! (nor in the computer memory for that matter)... – Aacini Mar 21 '15 at 02:46
  • @Aacini: Thank you for your help I have updated my question! I'm totally stumped on whats wrong with rename logic. – Dracrius Mar 21 '15 at 03:03
  • @Dracius: I found the bug! It is related to the process of several files when they are renamed: the first file renamed is processed twice by `FOR` command. I fixed the bug. Please, copy again the program above and test it... – Aacini Mar 21 '15 at 03:17
  • @Aacini: Perfect thank you very much I never would of found that on my own. – Dracrius Mar 21 '15 at 15:50
1

I think this is going to work for you. You should change the echo ren to ren once you decide it looks right.

@echo on
setlocal enableDelayedExpansion

set cnta=0
set cntb=0

for %%a in (*.mkv) do (
    set /a cnta=cnta+1
    set line_!cnta!=%%a
)

for %%b in (*.nfo) do (
    set /a cntb=cntb+1
    set newname=line_!cntb!
    call set newname=%%!newname!%%
    echo ren "%%~fb" "!newname!"
)

I think you'll prefer a loop like this. I actually tested it all out this time:

@echo on
setlocal enableDelayedExpansion

set cnta=0
set cntb=0

for /f "usebackq delims=" %%a in (`dir *.mkv /a-d /b /on`) do (
    set /a cnta=cnta+1
    set line_!cnta!=%%a
)

for /f "usebackq delims=" %%b in (`dir *.nfo /a-d /b /on`) do (
    set /a cntb=cntb+1
    set newname=line_!cntb!
    call set newname=%%!newname!%%
    echo ren "%%~fb" "!newname!"
)

If you have any real interest in playing with batch scripts, you're going to need a lot of patience and trial and error before it really starts to click. It can be very intricate and complicated.

shawnt00
  • 16,443
  • 3
  • 17
  • 22
  • This still doesn't do anything. Thanks for trying it looks better then mine but it won't even echo! – Dracrius Mar 20 '15 at 23:48
  • You were using `for /f` when you really want `for` although I think you'll still want the option using a `dir` command. I edited the answer. – shawnt00 Mar 20 '15 at 23:59
  • Why will the dir version be better? And I'm sorry to say it still does nothing. – Dracrius Mar 21 '15 at 00:18
  • @Dracrius dir is better because of /on - that orders by name to insure that you get the files in the correct order. Both examples here work for me. – Bob Mar 21 '15 at 00:57
1

Since you appear to be looking for an alternative, does each .nfo file contain the name of its corresponding .mkv file? If so, you could use find and conditional execution to pair the .mkv with its .nfo by the .nfo's contents. This is much (much!) less efficient than shawn and Aacini's answers, but at least you don't have to worry about whether or not the files are in order this way.

@echo off
setlocal

for %%I in (*.mkv) do (
    for %%x in (76107*.nfo) do if not exist "%%~nI.nfo" (
        find /i "%%~nxI" "%%x" >NUL && ren "%%x" "%%~nI.nfo"
    )
)
rojo
  • 24,000
  • 5
  • 55
  • 101