Computer Hope
Microsoft => Microsoft DOS => Topic started by: mrwonker on April 03, 2019, 09:46:28 PM
-
Hello,
I have a folder, in that folder there are numerous other folders with random names, inside each of these folders there is a file called movie.nfo in that text file there is a line that says <title>random text</title> I was wondering if it would be possible to create a batch script that could be run from the parent directory containing the random folders for it to edit these text files and replace random text with the name of the folder they are contained within, would save me some serious time if this could be done so would be grateful for any help given.
Thanks.
-
if you need to do that only once you can use notepad++
with that you can change the same string in multiple files.
If not you can use a little program called fart.
And maybe use it in bach
-
Thanks for your reply Blisk, I have downloaded notepad++ and looks like this could do what I'm wanting if I just knew what phrase to use to call the parent folder name in the renaming process, any ideas?
-
Hi, I'm still stuck on this problem I've spent I don't know how may hours trying to figure a solution to no avail, I've created scripts in the past but always with help as I don't pretend to know much of the language needed, I try and learn but the fequency that I actually need to make a script means I've forgoten half of what I've learned by the time it comes to need to write another, I've tried using notepad++ as suggested but don't think there's anyway to call the parent folder for the rename process, there's basically 3 things I need to achieve
1. method to open movie.nfo recursively through numerous subfolders
2. use a find and replace string for the text inside the movie.nfo files and
3. be able to call the parent folder of the movie.nfo file to be used in the replace string
surely that these 3 things must be achievable using a batch or vbs script?
I've read that there could be a problem using a batch file in that the text I'm trying to find includes < and > which I understand these are special characters that will cause the script to do something other than intended but also read that you can use ^ before special characters to stop this? I know how to use find and replace using a vbs script and that's fine in replacing <title>random text</title> but don't know how to do 1. and 3.
please someone help I'm pulling my hair out on this one!
-
Can you clarify one thing? In each subfolder you have exactly one movie.nfo file, and at the end you still want exactly one movie.nfo file, and everything in that file is the same as it was before, except that any line like this <title>blablabla</title> you want to change to <title>sub folder name</title> . Is that right. OK two things. Is that just the folder name or the whole path?
Example
In a folder called sub101 you have movie.nfo, do you want to see this? <title>sub101</title> ? Is that correct?
OK three things. Do you want the old movie.nfo to just vanish, or be renamed, e.g. movie.nfo.old ?
-
Fourth question: You have a main folder, and under this are a bunch of (let's face it!) folders with movies in them. Is each movie.nfo file in a folder one level below the main folder?
-
Thanks for your Reply Salmon Trout in response to your first reply you've interpreted it exactly correct in what I'm trying to do and regards to the old movie.nfo file yes I'd be happy for that to dissappear, overwritten etc and regarding 'Fourth question: You have a main folder, and under this are a bunch of (let's face it!) folders with movies in them. Is each movie.nfo file in a folder one level below the main folder?' yes that is also absolutely correct, thank you for your time, I do hope you can help (fingers crossed)
-
I am going to use an example movie.nfo I found on the Kodi wiki on this page
https://kodi.wiki/view/NFO_files/Movies
I hope this is the right format?
-
File movie.nfo in subfolder sub101
1. Before
(https://images2.imgbox.com/49/ad/HWqUnzoi_o.jpg)
2. After
(https://images2.imgbox.com/ae/7a/DmW8IVDU_o.jpg)
-
Yes Salmon Trout that’s precisely what I want to achieve.
-
I made 20 subfolders each with the same movie.nfo, and ran the batch below. It is a bit slow, even on an SSD. It has to process every line of each movie.nfo, and takes about 20 seconds each time on my system. 20 files took 8 minutes. Put the batch script in the folder above the subfolders.
I suggest you try it with a test folder first in case it trashes all your nfo files!!!! Also test the movie.nfo with whatever app you are using them for.
@echo off
setlocal enabledelayedexpansion
set thisfolder="%~dp0"
echo.
cd /d "%thisfolder%"
set nfos=0
echo Folder: %thisfolder%
for /f "delims=" %%A in ('dir /b /ad') do if exist "%%A\movie.nfo" set /a nfos+=1
echo Found %nfos% movie.nfo file(s) under this folder
echo Press a key to process files
echo or CTRL + C to abort
pause
echo %date% %time% Started
for /f "delims=" %%A in ('dir /b /ad') do (
echo !date! !time! Checking folder %%A
if exist "%%A\movie.nfo" (
echo !date! !time! Found movie.nfo: YES
cd "%%A"
if exist movie.nfo.new del movie.nfo.new
for /f "delims=" %%B in (movie.nfo) do (
echo "%%B" | find "<title>" | find "</title>" >nul && (
echo !date! !time! Old: %%B
for /f "tokens=1-4 delims=^<^>" %%C in ("%%B") do (
echo %%C^<title^>%%A^</title^> >> movie.nfo.new
echo !date! !time! New: %%C^<title^>%%A^</title^>
)
) || (
echo %%B >> movie.nfo.new
)
)
del movie.nfo
ren movie.nfo.new movie.nfo
cd "%thisfolder%"
) else (
echo !date! !time! Found movie.nfo: NO
)
)
echo %date% %time% Finished - press a key to close
pause
-
Thanks so much Salmon, I’m out at the minute so will be a few hours before can try this out, but can’t wait to give it a go, the time it takes to process isn’t a big issue so that should be fine, I’ll report back when I’ve tried it.
-
Thanks Salmon script works perfectly really appreciate your time in helping me with this, I did have an issue when first tried the script the script was escaping at the point where it says press key to continue or ctrl c to abort, I moved test folder to C: drive and it worked, thought it might have been due to security setting which did differ on the other drive so altered them and still no good on original test folder, tried it on another drive that worked, anyway to cut a long story short after a bit of head scratching worked out the reason was due to the folder was running it from had () in the title 'Movies (1)' on renaming the folder to 'Movies 1' the script worked, you'll probably understand the reson why the brackets in the folder name stopped it running but it really doesn't matter to me, I don't need the parent folder to contain brackets so all is good.
:) :) :)
-
Glad it worked! Is this just a one-time thing, or are you going to be doing this a lot? Because I have been working on a much faster version using VBScript.
-
My VBscript processed the same 20 files in 0.6 seconds. As before, try it out on test files/folders first!!! Place as a .vbs file in the top folder (like the .bat) and you can double click it in Windows Explorer. You should not see a console window, just 2 message boxes.
Const ForWriting = 2
Set objFSO = CreateObject("Scripting.FileSystemObject")
set objShell = createobject("wscript.shell")
strScriptDir = CreateObject("Scripting.FileSystemObject").GetParentFolderName(WScript.ScriptFullName)
iInfoCount = 0
For Each objFolder In objFSO.GetFolder(strScriptDir).SubFolders
aFilePath = split(objFolder.Path, "\")
strSubFolderName = aFilePath(Ubound(aFilePath))
strNFOpath = objFolder.Path & "\" & "movie.nfo"
If objFSO.FileExists(strNFOpath) Then
iInfoCount = iInfoCount + 1
End If
Next
v = msgbox("Found " & iInfoCount & " movie.nfo file(s)" & vbcrlf & vbcrlf & "Click OK to start or Cancel to quit", 1, "Movie.nfo processor")
if v = 2 Then
wscript.quit
End If
st = timer
iFiles = 0
For Each objFolder In objFSO.GetFolder(strScriptDir).SubFolders
aFilePath = split(objFolder.Path, "\")
strSubFolderName = aFilePath(Ubound(aFilePath))
strNFOpath = objFolder.Path & "\" & "movie.nfo"
If objFSO.FileExists(strNFOpath) Then
set nfoFile = objFSO.OpenTextFile(strNFOpath, 1)
content = nfoFile.ReadAll
aNFOfileLines = split(content, vbcrlf)
For iLine = 0 To Ubound(aNFOfileLines)
strThisLine = aNFOfileLines(iLine)
If instr(strThisLine, "<title>") > 0 Then
If instr(strThisLine, "</title>") > 0 Then
strLineIndent = Space(instr(strThisLine, "<title>")-1)
strOldLine = strThisLine
strNewLine = "<title>" & strSubFolderName & "</title>"
aNFOfileLines(iLine) = strLineIndent & strNewLine
Exit For
End If
End If
Next
nfoFile.close
objShell.currentdirectory = objFolder
Set objTextFile = objFSO.OpenTextFile ("movie.nfo.new", ForWriting, True)
For iLine = 0 To Ubound(aNFOfileLines)
objTextFile.WriteLine(aNFOfileLines(iLine))
Next
objTextFile.Close
objFSO.DeleteFile "movie.nfo"
objFso.MoveFile "movie.nfo.new", "movie.nfo"
objShell.currentdirectory = strScriptDir
iFiles = iFiles + 1
End If
Next
et = timer
strMsg = "Processed " & iFiles & " file(s)" & vbcrlf & vbcrlf & "in " & et-st & " seconds"
v = msgbox(strMsg, 0, "Movie.nfo processor")
-
My VBscript processed the same 20 files in 0.6 seconds.
Tried it again and got 0.34 seconds.
-
Hi Salmon,
Sorry only just seen your further input to this post, you really are a star, I've tried the VBScript and yea it's super quick, whilst I said it didn't matter about the speed, It actually is going to be much better for me using the VBScript given the processing speed, I really can't thank you enough. :) :) :)
-
Here is a new version. You can store it anywhere, it does not have to be in the parent folder of the movie folders. When you run the script it shows the Windows 'browse for folder' dialog and you can use that to find the folder you want to scan. Then it works the same as before.
Const ForWriting = 2
Set objFSO = CreateObject("Scripting.FileSystemObject")
set objShell = createobject("wscript.shell")
'strScanDir = CreateObject("Scripting.FileSystemObject").GetParentFolderName(WScript.ScriptFullName)
strPromptString = "Choose folder to scan (click Cancel to quit)"
strScanDir = BrowseFolder( "My Computer", False)
if lcase(strScanDir) = "quit" Then
wscript.quit
End If
iInfoCount = 0
For Each objFolder In objFSO.GetFolder(strScanDir).SubFolders
aFilePath = split(objFolder.Path, "\")
strSubFolderName = aFilePath(Ubound(aFilePath))
strNFOpath = objFolder.Path & "\" & "movie.nfo"
If objFSO.FileExists(strNFOpath) Then
strMessage = "yes"
iInfoCount = iInfoCount + 1
Else
strMessage = "no"
End If
Next
v = msgbox("Found " & iInfoCount & " movie.nfo file(s)" & vbcrlf & vbcrlf & "Click OK to start or Cancel to quit", 1, "Movie.nfo processor")
if v = 2 Then
wscript.quit
End If
st = timer
iFiles = 0
For Each objFolder In objFSO.GetFolder(strScanDir).SubFolders
aFilePath = split(objFolder.Path, "\")
strSubFolderName = aFilePath(Ubound(aFilePath))
strNFOpath = objFolder.Path & "\" & "movie.nfo"
If objFSO.FileExists(strNFOpath) Then
set nfoFile = objFSO.OpenTextFile(strNFOpath, 1)
content = nfoFile.ReadAll
aNFOfileLines = split(content, vbcrlf)
For iLine = 0 To Ubound(aNFOfileLines)
strThisLine = aNFOfileLines(iLine)
If instr(strThisLine, "<title>") > 0 Then
If instr(strThisLine, "</title>") > 0 Then
strLineIndent = Space(instr(strThisLine, "<title>")-1)
strOldLine = strThisLine
strNewLine = "<title>" & strSubFolderName & "</title>"
aNFOfileLines(iLine) = strLineIndent & strNewLine
Exit For
End If
End If
Next
nfoFile.close
objShell.currentdirectory = objFolder
Set objTextFile = objFSO.OpenTextFile ("movie.nfo.new", ForWriting, True)
For iLine = 0 To Ubound(aNFOfileLines)
objTextFile.WriteLine(aNFOfileLines(iLine))
Next
objTextFile.Close
objFSO.DeleteFile "movie.nfo"
objFso.MoveFile "movie.nfo.new", "movie.nfo"
objShell.currentdirectory = strScanDir
iFiles = iFiles + 1
End If
Next
et = timer
strMsg = "Processed " & iFiles & " file(s)" & vbcrlf & vbcrlf & "in " & et-st & " seconds"
v = msgbox(strMsg, 0, "Movie.nfo processor")
Function BrowseFolder( myStartLocation, blnSimpleDialog )
' This function generates a Browse Folder dialog
' and returns the selected folder as a string.
'
' Arguments:
' myStartLocation [string] start folder for dialog, or "My Computer", or
' empty string to open in "Desktop\My Documents"
' blnSimpleDialog [boolean] if False, an additional text field will be
' displayed where the folder can be selected
' by typing the fully qualified path
'
' Returns: [string] the fully qualified path to the selected folder
'
' Based on the Hey Scripting Guys article
' "How Can I Show Users a Dialog Box That Only Lets Them Select Folders?"
' http://www.microsoft.com/technet/scriptcenter/resources/qanda/jun05/hey0617.mspx
'
' Function written by Rob van der Woude
' http://www.robvanderwoude.com
Const MY_COMPUTER = &H11&
Const WINDOW_HANDLE = 0 ' Must ALWAYS be 0
Dim numOptions, objFolder, objFolderItem
Dim objPath, objShell, strPath, strPrompt
' Set the options for the dialog window
strPrompt = strPromptString
If blnSimpleDialog = True Then
numOptions = 0 ' Simple dialog
Else
numOptions = &H10& ' Additional text field to type folder path
End If
' Create a Windows Shell object
Set objShell = CreateObject( "Shell.Application" )
' If specified, convert "My Computer" to a valid
' path for the Windows Shell's BrowseFolder method
If UCase( myStartLocation ) = "MY COMPUTER" Then
Set objFolder = objShell.Namespace( MY_COMPUTER )
Set objFolderItem = objFolder.Self
strPath = objFolderItem.Path
Else
strPath = myStartLocation
End If
Set objFolder = objShell.BrowseForFolder( WINDOW_HANDLE, strPrompt, _
numOptions, strPath )
' Quit if no folder was selected
If objFolder Is Nothing Then
BrowseFolder = "quit"
Exit Function
End If
' Retrieve the path of the selected folder
Set objFolderItem = objFolder.Self
objPath = objFolderItem.Path
' Return the path of the selected folder
BrowseFolder = objPath
End Function
-
I have changed the script slightly since I posted it about 1 hour ago.
-
Hi, Salmon, sorry, again only just seen you've added further, don't know why I'm not getting notifications of post updates! I have selected to do so, anyway tried out your latest script and it's fantastic, will certainly be useful to have as a main script to be run from anywhere, great stuff, awesome help, awesome site, I can't thank you enough.
-
Thank you for taking the time to come back and report. This is what I like about Computer Hope - you sometimes get an interesting script idea, have fun trying to make it work, and it works and is useful to someone, who is kind enough to come back and say so.