How to increment the filename if file already exists in javascript
You can check this.state.Files
before. A recursive function could be used here. Imagine you load a file named export.csv
. The second one would export.csv
transformed in export_1.csv
. But on a third one named export.csv
, the verification would be done on export
, leading to export_1
=> Error !
The best is to do :
const checkNameOfTheFile = (newFileName) => {
// Ex 'export.csv'
const counter = this.state.Files.filter(f => f.fileName === newFileName).length;
// If counter >= 2, an error has already been passed to the files because it means
// 2 files have the same name
if (counter >= 2) {
throw 'Error duplicate name already present';
}
if (counter === 0) {
return newFileName
}
if (counter === 1) {
const newName = `${newFileName.split('.')[0]}_${counter}.${newFileName.split('.')[1]}`;
// Return export_1.csv;
return checkNameOfTheFile(newName);
// We need to check if export_1.csv has not been already taken.
// If so, the new name would be export_1_1.csv, not really pretty but it can be changed easily in this function
}
};
const CompleteData= {
fileData: reader.result,
fileName: checkNameOfTheFile(file.name),
};
How to Increment filename if file exists
This problem is always initializative num = 0
so if file
exists, it save file0.jpg
and not check whether file0.jpg
is exists ?
So, To code work. You should check until available :
int num = 0;
String save = at.getText().toString() + ".jpg";
File file = new File(myDir, save);
while(file.exists()) {
save = at.getText().toString() + (num++) +".jpg";
file = new File(myDir, save);
}
Increment the file name if the file already exists in c#
There is one more problem in your code. Why do you have file names like "New1.txt2","New1.txt2.txt3", "New1.txt2.txt3.txt4"? Because you don't keep initial filename somewhere. So, I'd propose to keep two variables for filenames: for instance, filename_initial
and filename_current
.
Try something like this:
String filename_initial = @"C:\path\New.txt";
String filename_current = filename_initial;
count = 0;
while (File.Exists(filename_current))
{
count++;
filename_current = Path.GetDirectoryName(filename_initial)
+ Path.DirectorySeparatorChar
+ Path.GetFileNameWithoutExtension(filename_initial)
+ count.ToString()
+ Path.GetExtension(filename_initial);
}
Increment the filename if it already exists when saving files in node.js?
I use something similar to version uploaded image files on disk. I decided to use the "EEXIST" error to increment the number rather than explicitly iterating the files in the directory.
const writeFile = async(filename, data, increment = 0) => {
const name = `${path.basename(filename, path.extname(filename))}${increment || ""}${path.extname(filename)}`
return await fs.writeFile(name, data, { encoding: 'utf8', flag: 'wx' }).catch(async ex => {
if (ex.code === "EEXIST") return await writeFile(filename, data, increment += 1)
throw ex
}) || name
}
const unversionedFile = await writeFile("./file.txt", "hello world")
const version1File = await writeFile("./file.txt", "hello world")
const version2File = await writeFile("./file.txt", "hello world")
How to increment the filename if file already exists
Hi here's a pretty "procedural" answer:
Dim counter As Integer = 0
Dim newFileName As String = orginialFileName
While File.Exists(newFileName)
counter = counter + 1
newFileName = String.Format("{0}({1}", orginialFileName, counter.ToString())
End While
you will need an imports statement for System.IO
Related Topics
Swift Parse Json - the Data Couldn'T Be Read Because It Isn'T in the Correct Format
How to Embed a Custom Font in an Iphone Application
How to Share Data Between View Controllers and Other Objects in Swift
How to Rotate a Uiimage 90 Degrees
Programmatically Retrieve Memory Usage on Iphone
Communicating and Persisting Data Between Apps With App Groups
Having a Uitextfield in a Uitableviewcell
Detecting Taps on Attributed Text in a Uitextview in Ios
Nsattributedstring Background Color and Rounded Corners
How to Change the Title of the "Back" Button on a Navigation Bar
Move Textfield Up When the Keyboard Has Appeared in Swiftui
Draw Text Along Circular Path in Swift For Ios
Appearancewhencontainedin in Swift
Uitapgesturerecognizer - Make It Work on Touch Down, Not Touch Up
Can't Get Correct Value of Keyboard Height in Ios8
Xcode Command /Usr/Bin/Codesign Failed With Exit Code 1: Errsecinternalcomponent