Skip to content

Instantly share code, notes, and snippets.

@lacan
Last active September 5, 2023 12:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lacan/f646871442a33d91ca5f5fe091a24fcd to your computer and use it in GitHub Desktop.
Save lacan/f646871442a33d91ca5f5fe091a24fcd to your computer and use it in GitHub Desktop.
[Rename VSI files and folders from CSV] This script helps rename VSI images and folders that have incorrect names #svi #slidescanner #groovy #fiji
#@File originalDir (label="Directory with original VSI Files", style="directory")
#@File csvFile (label="CSV File with full VSI file path in columns 1 and new name in column 2")
#@String sep (label="CSV separator", value=";")
/*
* Rename VSI images based on CSV reference file
*/
println sep
def csvImages = csvFile.readLines()*.split(sep)
//println csvImages
// First element is the original file name (full path)
// Second element is the thing to rename the vsi file
// Make a HashTable with the name as key and new name as value
def idMatches = new LinkedHashMap<File, String>()
csvImages.each{ row ->
if (row.size() > 1 ) {
idMatches.put( new File(row[0].trim()), row[1].trim() )
}
}
// List the images in the folder
vsiImages = originalDir.listFiles().findAll{ it.getName().endsWith("vsi") }
def renamingMap = new LinkedHashMap<Path, Path>()
vsiImages.each{ vsi ->
idMatches.each{ fileName, suggestedName ->
if( vsi.getName() == fileName.getName() ) {
def parent = vsi.getParent()
def oldFile = Paths.get( vsi.getAbsolutePath() )
def newFile = Paths.get( parent, suggestedName+ ".vsi" )
def oldFolder = Paths.get( parent, "_"+vsi.getName().split('\\.')[0]+"_" ) // remove .vsi, assume the file has no dots otehr than the extension
def newFolder = Paths.get( parent, "_"+suggestedName+"_" )
println "Will move file\n${oldFile} to \n${newFile}"
println "Will move folder\n${oldFolder} to \n${newFolder}"
renamingMap.put( oldFile, newFile )
renamingMap.put( oldFolder, newFolder )
}
}
}
// Display as results
def rt = new ResultsTable()
renamingMap.each{ oldPath, newPath ->
rt.incrementCounter()
rt.addValue("Original File", oldPath.toString())
rt.addValue("Renamed File", newPath.toString())
}
rt.show("File renaming outcome")
def warning = ""
if( renamingMap.keySet().size() != renamingMap.values().toUnique().size() ) {
warning = "WARNING: New names are not UNIQUE!\nCheck the new names and make sure that each row is unique, otherwise the script will fail!"
}
def dialog = new ij.gui.NonBlockingGenericDialog( "Apply File Renaming?")
dialog.addMessage("Please note that this will irrevocably change the file names.\nMake sure that the names are correct, then press OK")
dialog.addMessage( warning )
dialog.addCheckbox("I agree, proceed with file renaming", false)
dialog.showDialog()
// User cancels in a panic
if( dialog.wasCanceled() )return
// User decides not to do it
if( !dialog.getNextBoolean() ) return
renamingMap.each { oldFile, newFile ->
try{
Files.move( oldFile, newFile, StandardCopyOption.ATOMIC_MOVE )
} catch (def e) {
println e
}
}
println "File renaming script completed"
import ij.measure.ResultsTable
import java.nio.file.Paths
import java.nio.file.Path
import java.nio.file.Files
import java.nio.file.StandardCopyOption
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment