Check if file exists (without having the full name)

3 visualizzazioni (ultimi 30 giorni)
ChrisMat
ChrisMat il 2 Mag 2019
Modificato: Stephen23 il 4 Nov 2024
Lets call the file i want to save file1. I am versioning the files.
file1_v1-0
file1_v1-1
Lets say i want to save file1_v1-1. I want Matlab to check if file1 is already existing in the folder i want to save it.
  1. If file1_v1-1 is already there -> skip
  2. If file1_v1-1 is there but in an older version. -> Delete old version and save new one.
  3. If file1_v1-1 is not there at all. -> save file1_v1-1
I have huge trouble with point 2. I tried to build something with "dir " and "stringfind". But that was not very satitisfying. Also tried to use "exist" but i need the full filename to work with that
Hope i made my problem clear. Thanks for your help.

Risposte (1)

Stephen23
Stephen23 il 2 Mag 2019
Modificato: Stephen23 il 4 Nov 2024
You might like to download my FEX submission nextname:
For example:
subd = 'absolute or relative path to where the files are saved';
name = nextname(subd,'file1_v1-<0>.txt',true)
You could easily adapt the function to also check the files' timestamps (in the output from DIR). Compare the datenum values using numeric comparisons, not using string operations.

Categorie

Scopri di più su Programming in Help Center e File Exchange

Prodotti


Release

R2014b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by