I found a script to rename batches of files with a CSV file (can't remember where, it was long ago), but I'd like to improve it a bit and I'm afraid that my scripting skills are not sufficient enough:
Code: Select all
#NoEnv
#SingleInstance
SetWorkingDir %A_ScriptDir%
SetBatchLines, -1 ; At end of init set to 0.
; Settings
csv_file := "D:\Videos\New\titles.csv" ; Database with the new file names.
source_folder := "D:\Videos\New" ; Place of the source files.
destination_folder := "D:\Videos\New" ; Where to copy the files.
/*
CSV file content is expected like this:
S01E01,S01E01 - First Episode
S01E02,S01E02 - Second Episode
File names in source folder are expected like this:
S01E01.avi
S01E01.avi
*/
; Setup Folders
FileSelectFile, csv_file, , %csv_file%, Which file contains the names?, CSV files (*.csv)
FileSelectFolder, source_folder, %source_folder%, 1, Where are the files?
FileSelectFolder, destination_folder, %destination_folder%, 1, Where to copy the files?
FileCreateDir, %destination_folder%
; Processing
Loop, Read, %csv_file%
{
line := A_LoopReadLine ; A_LoopReadLine contains in example "S01E01,S01E01 - First Episode".
Loop, Parse, line, CSV
{
title := RegExReplace(A_LoopReadLine, "^.*?,(.*)", "$1") ; Trims A_LoopReadLine to get only the title
FileCopy, %source_folder%\%A_LoopField%.avi, %destination_folder%\%title%.avi, 1 ; If A_Index is "1", then A_LoopField contains in example "S01E01".
FileRecycle, %source_folder%\%A_LoopField%.avi ; Sends the source file to the recycle bin
Break ; First entry is needed, not more.
}
}
Is there is a way to "collect" the extension and change it in the "FileCopy" command first and second parameters, as well as in the "FileRecycle" command? Obviously, it needs to be the same as the original file.
2. If a file name in the "titles.csv" has a "?", a ":", or any other character not allowed in file names, the file is not renamed (but the "FileRecycle" still erases the original file).
Is there is a way to avoid this, eg. detecting such characters and removing or replacing them, or at least sending an error message/not sending the file to the recycle bin?
3. It seems that this script doesn't recognise the values of the settings variable (csv_file, source_folder, destination_folder) and every time I launch the script, it asks where are the CSV file and the folder locations, so where does the problem comes from?
Thank you for your answers.