I will move certain files. how to move the image file from directory to another directory,
example file img.jpg from /storage/emulated/0/Myfolder to /storage/emulated/0/Urfolder?
File.rename works only if source file and destination path are on the same file system, otherwise you will get a FileSystemException (OS Error: Cross-device link, errno = 18). Therefore it should be used for moving a file only if you are sure that the source file and the destination path are on the same file system.
For example when trying to move a file under the folder /storage/emulated/0/Android/data/ to a new path under the folder /data/user/0/com.my.app/cache/ will fail to FileSystemException.
Here is a small utility function for moving a file safely:
Future<File> moveFile(File sourceFile, String newPath) async {
try {
// prefer using rename as it is probably faster
return await sourceFile.rename(newPath);
} on FileSystemException catch (e) {
// if rename fails, copy the source file and then delete it
final newFile = await sourceFile.copy(newPath);
await sourceFile.delete();
return newFile;
}
}
await File('/storage/emulated/0/Myfolder').rename('/storage/emulated/0/Urfolder')
If the files are on different file systems you need to create a new destination file, read the source file and write the content into the destination file, then delete the source file.
Related
User uploads file from UI and at springboot i check if the file exists and if yes, delete the file or copy the file to File server. I need to save the file with the file name what user has uploaded.
But Sonarqube is giving vulnerability error: "Change this code to not construct the path from user-controlled data."
private String processFileUpload(MultipartFile uploadedFile){
String filePathName = "/uploaded_files/"+uploadedFile.getOriginalFilename(); //Error at this line
File file = new File(uploadedFile.getOriginalFilename());
if (file.exists()) {
file.delete();
}
//file transfer code
}
I need the file to be stored with the same name user has uploaded. How to fix this sonarqube error?
I'm trying to save an animated gif to a file location. Here is my current code:
# create folder location and file path name
my $dn="cmp".int(rand(10));
my $fn="ca".int(rand(10)).int(rand(10)).int(rand(10)).int(rand(10)).int(rand(10)).int(rand(10)).".gif";
while (-f "$datapath/$dn/$fn") { $fn="ca".int(rand(10)).int(rand(10)).int(rand(10)).int(rand(10)).int(rand(10)).int(rand(10)).".gif"; }
# open the uploaded image for saving to a file
open (IMAGE,$insfn{'attachment'});
binmode(IMAGE);
# open the file path for writing the image to
open (OUTPUT,"$datapath/$dn/$fn");
binmode(OUTPUT);
# write the image to the file
my $buf;
my $bufSize=4096;
while(read(IMAGE,$buf,$bufSize)) { print OUTPUT $buf; }
For some reason its not saving the image to the file.
Not sure what to do past this point Im fairly new to perl.
You haven't opened OUTPUT for writing.
replace
open (OUTPUT,"$datapath/$dn/$fn");
with
open (OUTPUT, ">", "$datapath/$dn/$fn");
and Perl will create the file, truncate it if it exists already, and allow writes to it.
Relevant documentation.
I want to add output files of a map reduce program to the same directory every time I run the job by appending time stamp at the end of file name.
Currently i am able append the time stamp at the end of file output file, but I am unable to find out how to add files to the same output directory instead of overwriting it every time.
You can write output files in temporary folder and move them to target folder after the end of job. Example of a method that moves all files from one folder to another:
public static void moveFiles(Path from, Path to, Configuration conf) throws IOException {
FileSystem fs = from.getFileSystem(conf); // get file system
for (FileStatus status : fs.listStatus(from)) { // list all files in 'from' folder
Path file = status.getPath(); // get path to file in 'from' folder
Path dst = new Path(to, file.getName()); // create new file name
fs.rename(file, dst); // move file from 'from' folder to 'to' folder
}
}
The output can be controlled by using reduce method. I guess you can try out a logic in reducer.
Please note that number of reducers = number of output files.
I am looking for a way to add File(s) to an existing directory that has a random name as part of a Visual Studio Setup Project and I hoped someone might be able to help me solve this puzzle please.
I have been attempting to obtain the discovered path property of the directory using a Launch Condition; Unfortunately this method returns the full file path including the filename, which cannot be used as a directory property.
The directory in question takes the form [AppDataFolder]Company\Product\aaaaaaaaaaaa\
where aaaaaaaaaaaa is a random installation string.
Within the Launch Condition Setup I check for the directory's existence by searching for a file that would appear inside it,
Search Target Machine
(Name): File marker
Filename: sample.txt
Folder: [AppDataFolder]Company\Product\
Property: DIRFILE
Launch Condition
(Name): File marker exists
Condition: DIRFILE
In the Setup Project I add the file I wish to insert, with the details
Condition: DIRFILE
Folder: 'Installation folder'
Then in File System Setup I add a new folder entry for the random directory aaaaaaaaaaaa
(Name): Installation folder
Condition: DIRFILE
DefaultLocation: [DIRFILE]\..\ *Incorrect*
Property [DIRLOCATION]
As you can see the installer detects the existence of the marker file but, instead of placing my file at the same location, when using [DIRFILE] the installer would incorrectly try and insert it INTO the file;
This is because the file path was returned
[AppDataFolder]Company\Product\aaaaaaaaaaaa\sample.txt
where I instead need the directory path
[AppDataFolder]Company\Product\aaaaaaaaaaaa
Therefore I was wondering if it was possible to return the directory the file was found in from Search Target Machine (as opposed to the file location of the file), if I could extract the directory path by performing a string replace of the filename on the file location DIRFILE within the DefaultLocation field in File System Setup, or if perhaps there is even another method I am missing?
I'm also very interested in a simple solution for this, inside the setup project.
The way I did solve it was to install the files to a temporary location and then copy them to the final location in an AfterInstall event handler. Not a very elegant solution! Since it no longer care about the user selected target path I removed that dialog. Also I needed to take special care when uninstalling.
public override void OnAfterInstall(IDictionary savedState)
{
base.OnAfterInstall(savedState);
// Get original file folder
string originDir = Context.Parameters["targetdir"];
// Get new file folder based on the dir of sample.txt
string newDir = Path.GetDirectoryName(Context.Parameters["dirfile"]);
// Application executable file name
// (or loop for all files on the path instead)
string filename = "ApplicationName.exe";
// Move or copy the file
File.Move(Path.Combine(originDir, filename), Path.Combine(newDir, filename)));
}
I am trying to copy a selected folder to a defined target.
function importLabs(selected) {
if(selected = "labAuto") {
//Open Folder dialog box
var myLab=Folder.selectDialog("Import");
myLab.copy ("~/Desktop/In here");
}
}
Can't figure this one out. Am I using the wrong function .copy()?
Unfortunately there is no copy method for Folder.
You will need to create your destination folder first then copy each file.
If you would like to copy sub-folders as well then you need to do it recursively.