Rename file on FTP using Powershell - (553) File name not allowed [duplicate] - powershell

In my application, I have files in FTP server one directory and I move that file source to target path. In this process, when I move selected source file that source file will not show in the source path, it will show only in target path.
I tried this below code, but I am getting error:
string sourceurl = "ftp://ftp.com/Mainfoder/Folder1/subfolder/subsubfolder/"
string Targetpat =
"ftp://ftp.com/Mainfoder/DownloadedFiles/"+subfolder+"/"+todaydatefolder+"/"+susubfolder;
Uri serverFile = new Uri(sourceurl + filename);
request = (FtpWebRequest)FtpWebRequest.Create(serverFile);
request.Method = WebRequestMethods.Ftp.Rename;
request.Credentials = new NetworkCredential(ftpUserID, ftpPassword);
request.RenameTo = Targetpat+"/"+newfilename;//folders without filename
response = (FtpWebResponse)request.GetResponse();
Stream ftpStream = response.GetResponseStream();
An unhandled exception of type 'System.Net.WebException' occurred in System.dll
Additional information: The remote server returned an error: (553) File name now allowed.
response = (FtpWebResponse)request.GetResponse(); //This line throwing the above exception
request.RenameTo = newfilename: when I set only newfilename, it renames that source same file name only.
How can I move this file to another directory within in same FTP server?
Please can anyone tell me. Thank you

As I wrote you already before:
request.RenameTo takes a path only.
So this is wrong:
string Targetpat =
"ftp://ftp.com/Mainfoder/DownloadedFiles/"+subfolder+"/"+todaydatefolder+"/"+susubfolder;
request.RenameTo = Targetpat+"/"+newfilename;
It should be:
string Targetpat =
"/Mainfoder/DownloadedFiles/"+subfolder+"/"+todaydatefolder+"/"+susubfolder;
request.RenameTo = Targetpat+"/"+newfilename;

Related

Flutter how to remove file extension when i need to put file name into variable

I use p.basename from flutter path library to get file name, but i got it with extension, how to remove extension? also to get path I use getApplicationDocumentsDerectory from path_provider library
File newFile = File(filepath);
String name = p.basename(newFile.path);
this is code I use to get file name
You just use 'basenameWithoutExtension' method like basename.
File newFile = File(filepath);
String name = p.basename(newFile.path);
String nameWithoutExtension = p.basenameWithoutExtension(newFile.path);

Ionic 3 Cordova File plugin gives error for copyFile operation

I am trying to copy a file from one dir to another using the copyFile(path, fileName, newPath, newFileName) function. It gives an error like {"code":13, "message":"input is not a directory"}. The documentation has only 12 error code and no 13th. I'd like to know what i did wrong please.
Here is a sample of my actual code.
this.path = "file:///storage/emulated/0/TheFolder/thefile.ext";
this.newPath = "file:///storage/emulated/0/NewFolder";
this.fileCtrl.copyFile(this.path, fileName, this.newPath, newFileName)
this.path must be a directory but your are showing some file name
change your code as follows
this.path = "file:///storage/emulated/0/TheFolder";
this.newPath = "file:///storage/emulated/0/NewFolder";
this.fileCtrl.copyFile(this.path, YOUR_EXISTING_FILE_NAME, this.newPath, NEW_FILE_NAME);
path -Base FileSystem
fileName - Name of file to copy
newPath - Base FileSystem of new location
newFileName - New name of file to copy to (leave blank to remain the same)

Saving screenshots with protractor

I'm attempting to save a screenshot using a generic method in protractor. Two features, it creates the folder if it does not exist and it saves the file (with certain conditions).
export function WriteScreenShot(data: string, filename: string) {
let datetime = moment().format('YYYYMMDD-hhmmss');
filename = `../../../test-reports/${filename}.${datetime}.png`;
let path =filename.substring(0, filename.lastIndexOf('/'));
if (!fs.existsSync(path)) {
fs.mkdirSync(path);
}
let stream = fs.createWriteStream(filename);
stream.write(new Buffer(data, 'base64'));
stream.end();
}
This can be used by calling browser.takeScreenshot().then(png => WriteScreenShot(png, 'login/login-page')); Using this example call, a file will be created, I assumed, in the path relative where my WriteScreenShot method's file resides. But that does not appear to be the case.
For example, when I run my spec test in the spec's folder, the image gets saved in the correct place. But if I run it at the project root, an error is capture. Obviously, this has to do with my relative path reference. How do I capture the project's root directory and build from that so that I can run the test from any directory?
This is a classical directory access error. Let me just explain what is happening to your code -
let path =filename.substring(0, filename.lastIndexOf('/'));
The above line outputs to ../../../test-reports
fs.existsSync checks whether thispath exists or not -
case 1 :(postive flow) Your spec folder is in the same current working directory in which you are trying to create reports folder. When you run your test, the path exists, it generates the test-reports directory & screenshots and your code works fine.
case 2:(negative flow) When you try to run it from the root directory which is the current working directory now, fs.existsSync tries to check the path & the reports folder inside it. If it doesn't exist , fs.mkdirSync tries to create your directories but it would fail as it cannot create multiple directories.
You should be using native path module of nodejs to extract the path instead of using file substring and the mkdirp external module for creating multiple directories.
import * as path from 'path';
let {mkdirp} = require('mkdirp'); // npm i -D mkdirp
export function WriteScreenShot(data: string, filename: string) {
let datetime = moment().format('YYYYMMDD-hhmmss');
filename = `../../../test-reports/${filename}.${datetime}.png`;
let filePath = path.dirname(filename); // output: '../../..' (relative path)
// or
let filePath = path.resolve(__dirname); // output: 'your_root_dir_path' (absolute path)
// or
let filePath = path.resolve('.'); // output: 'your_root_dir_path' (absolute path)
if (!fs.existsSync(filePath )) {
mkdirp.sync(filePath); // creates multiple folders if they don't exist
}
let stream = fs.createWriteStream(filename);
stream.write(new Buffer(data, 'base64'));
stream.end();
}
If you are curious to know the difference btw mkdir & mkdir-p please read this SO thread.

Get upload filename in eclipse using servlet

I have an application that uploads a file. I need to pass this file into another program but for that I need the file name only. Is there any simple code for that, using only Java or a servlet procedure?
while (files.hasMoreElements())
{
name = (String)files.nextElement();
type = multipartRequest.getContentType(name);
filename = multipartRequest.getFilesystemName(name);
originalFilename = multipartRequest.getOriginalFileName(name);
//extract the file extension - this can be use to reject a
//undesired file extension
extension1 = filename.substring
(filename.length() - 4, filename.length());
extension2 = originalFilename.substring
(originalFilename.length() - 4, originalFilename.length());
//return a File object for the specified uploaded file
File currentFile = multipartRequest.getFile(name);
//InputStream inputStream = new BufferedInputStream
(new FileInputStream(currentFile));
if(currentFile == null) {
out.println("There is no file selected!");
return;
}
There's a method in apache commons-io to get the file's extension. There's also guava Files class, with its getFileExtension method.

How to resolve pdf parsing error

scala code :
val file = new File(path + name)
val raf = new RandomAccessFile(file, "r")
val channel = raf.getChannel()
val buf = channel.map(FileChannel.MapMode.READ_ONLY, 0, channel.size())
val pdffile = new PDFFile(buf) // line 5
here, file is referring to pdf file. path is the address of pdf file and name is name of file.
In normal case, it executes fine, but in some pdf files, it throws error in line 5 as :
com.sun.pdfview.PDFParseException: Expected 'xref' at start of table
at com.sun.pdfview.PDFFile.readTrailer(PDFFile.java:974) ~[pdf-renderer-1.0.5.jar:na]
at com.sun.pdfview.PDFFile.parseFile(PDFFile.java:1175) ~[pdf-renderer-1.0.5.jar:na]
at com.sun.pdfview.PDFFile.<init>(PDFFile.java:126) ~[pdf-renderer-1.0.5.jar:na]
at com.sun.pdfview.PDFFile.<init>(PDFFile.java:102) ~[pdf-renderer-1.0.5.jar:na]
I think this pdf file has some problem with its format or content. When i made another pdf file using save as with this pdf file and used that new created pdf file, it worked fine.
So how can i resolve this problem so that whetehr i use proper file or bad file, my code should work fine.
EDIT
I found the following in the com/sun/pdfview/PDFFile.java description
#throws PDFParseException if the document appears to be malformed, or
its features are unsupported
.