Mrrrr
AdMiN
Inregistrat: acum 18 ani
Postari: 2241
|
|
With the help of ChatGPT, I enhanced this so I don't have to keep to manually copy the .vbs file to the desired folder, manually run it, then manually delete it.
To automate the above, you need AutoHotkey and an update to the above VBS script. I saved the VBS script to a desired static folder (static as in whose path I won't change), I saved the AHK script file in the same folder, then created a shortcut to the AHK file in my shell:startup folder.
The keyboard shortcut I set with AutoHotkey is CTRL+R, shortcut that will do the following: - the AHK script will copy the VBS file from my static folder to the currently active folder (where I pressed the CTRL+R keyboard shortcut) - the AHK script will run the VBS file (I updated the VBS script it so it detects the current folder - where it was copied by AHK) - you will manually input the subfolder name into the VBS script, then press OK, then the text file name, then press OK to rename the files (note: the creation of the subfolder with files to be renamed, and the creation of the text file with desired names will still be manual) - then the VBS script closes after you click the OK button on the files renaming confirmation - then the AHK script will delete the VBS file from the current folder and give confirmation
Important note: If your current folder is not properly detected, make sure you open your desired folder in the first Windows Explorer on your taskbar. It does not to be the only open folder, just the first on your taskbar from left to right. If that does not work either, restart Windows Explorer from Task Manager.
VBS script (path to it is required in the AHK script) - I named it Rename files v1.5.vbs Made in this color what actually changed from the script above
Option Explicit
Dim objShell, objExplorer, objWindow Set objShell = CreateObject("Shell.Application") Set objExplorer = Nothing
' Find the active Explorer window For Each objWindow In objShell.Windows If InStr(1, objWindow.FullName, "explorer.exe", vbTextCompare) > 0 Then Set objExplorer = objWindow Exit For End If Next
' Check if an active Explorer window was found If objExplorer Is Nothing Then MsgBox "No active Explorer window found. Please open a folder and select a file.", vbExclamation, "Error" WScript.Quit End If
' Decode the folder path from URL to standard file path Dim folderPath folderPath = objExplorer.LocationURL folderPath = Replace(folderPath, "file:///", "") folderPath = Replace(folderPath, "/", "\") folderPath = Replace(folderPath, "%20", " ")
' Test if current active folder is correct 'MsgBox folderPath
Dim message message = MsgBox("How to use:" & VbCrlf & VbCrlf & _ " This script was originally addressed to renaming subtitles, but can be used for any types of files." & VbCrlf & VbCrlf & _ " 1. In the folder where the current script is located, create a subfolder and put the files to be renamed in it. The natural order of the files in the folder should be the order in which you want them renamed." & VbCrlf & _ " 2. In the folder where the current script is located (not in the subfolder), create a text file (.txt) containing the desired names of the files in the desired order, 1 name on each line (if 200 files, there should be 200 lines). Do not include extensions for the files, just their desired names." & VbCrlf & _ " 3. After clicking the Yes button below, you will be asked to input the name of the subfolder containg the files and after that the name of the text file containing the desired names." & VbCrlf & _ " 4. Then the script will do the job and give confirmation that renaming is complete." & VbCrlf & VbCrlf & _ "That's it!" & VbCrlf & VbCrlf & _ "DO YOU WANT TO PROCEED?", vbYesNo, "Renaming script usage 101")
' Exit script if the answer is No If message = vbNo Then WScript.Quit End If
' Initialize FileSystemObject Dim fso Set fso = CreateObject("Scripting.FileSystemObject")
' Input boxes for subfolder name and text file name Dim fName, fldName fldName = InputBox("Input the name of the SUBFOLDER containing the files (not case sensitive):") fName = InputBox("Input name of text FILE containing a list of desired names (not case sensitive):")
' Paths Dim namesFile, sbFolder 'sbFolder = fso.GetAbsolutePathName(fldName) 'namesFile = fso.GetAbsolutePathName(fName & ".txt") sbFolder = folderPath & "\" & fldName namesFile = folderPath & "\" & fName & ".txt"
' Check if the subfolder exists If Not fso.FolderExists(sbFolder) Then WScript.Echo "ERROR: The folder " & fldName & " does not exist." WScript.Quit End If
' Check if the text file exists If Not fso.FileExists(namesFile) Then WScript.Echo "ERROR: The file " & fName & " does not exist." WScript.Quit End If
' Read names from the text file Dim i Dim namesArray() i = 0 With fso.OpenTextFile(namesFile, 1) ' 1 = ForReading Do Until .AtEndOfStream ReDim Preserve namesArray(i) namesArray(i) = .ReadLine i = i + 1 Loop .Close End With
' Get list of files in the subfolder Dim subFile, subFiles Set subFiles = fso.GetFolder(sbFolder).Files
' Check if there are files to rename If subFiles.Count = 0 Then WScript.Echo "ERROR: No files found in the " & fldName & " subfolder." WScript.Quit End If
Dim wrng If i <> subFiles.Count Then wrng = MsgBox("WARNING!" & VbCrlf & VbCrlf & _ "There are more files in the " & fldName & " subfolder than lines in the " & fName & " file!" & VbCrlf & VbCrlf & _ "DO YOU WANT TO PROCEED?", vbYesNo, "WARNING!") If wrng = vbNo Then WScript.Quit End If End If
' Rename the files i = 0 For Each subFile In subFiles ' Match each file with a name from the txt file If i >= UBound(namesArray) + 1 Then WScript.Echo "WARNING! More files in " & fldName & " than lines in the " & fName & " file. Extra files will not be renamed." Exit For End If ' Keep the file extension Dim fileExtension fileExtension = fso.GetExtensionName(subFile.Name) ' Construct new file name Dim newFileName newFileName = namesArray(i) & "." & fileExtension ' Rename the file subFile.Name = newFileName i = i + 1 Next
WScript.Echo "Renaming complete." |
AHK script (I put a shortcut to it in shell:startup)
^r:: ; CTRL+R Hotkey
SourceFile := "D:\YOUR FOLDER PATH GOES HERE\Rename files v1.5.vbs" ExplorerPath := ""
; Use COM to retrieve the active Explorer window path For window in ComObjCreate("Shell.Application").Windows { ; Check if this is the active window if (window.hwnd = WinActive("A")) { ExplorerPath := window.Document.Folder.Self.Path break } }
; If a valid path is found, proceed if (ExplorerPath) { ; Copy the file to the active folder DestinationFile := ExplorerPath . "\Rename files v1.5.vbs" FileCopy, %SourceFile%, %DestinationFile% if !ErrorLevel { MsgBox, File copied successfully to %ExplorerPath%.
; Run the copied VBS script RunWait, % "wscript.exe """ . DestinationFile . """"
; Delete the VBS script after it runs FileDelete, %DestinationFile%
; Confirm deletion if !FileExist(DestinationFile) MsgBox, File executed and deleted successfully from %ExplorerPath%. else MsgBox, File could not be deleted from %ExplorerPath%. Please check permissions. } else { MsgBox, Failed to copy the file to %ExplorerPath%. Please check permissions or file existence. } } else { MsgBox, Could not determine the active folder. Please make sure a Windows Explorer window is active. } return |
_______________________________________
|
|