renameTo() in grails to rename an apk file to zip file - groovy

I want to upload apk files. And then convert it to zip in order to extract some information.
I used renameTo() function but its not working.
String newFilename = "new.apk"
new File(f.getOriginalFilename()).renameTo(new File("new.apk"))
(f.getOriginalFilename() will return the name of an apk file) How exactly can I do renaming?

Got it. need to give the full path to the file. for example
new File( webRootDir + "/" + [folder name] + "/" +f.originalFilename ).renameTo(
new File(webRootDir + "/" + [folder name] + "/" +appname+".zip") )
will solve the problem.

And Groovier using GStrings:
new File("$webRootDir/$folderName/${f.originalFilename}").renameTo(new File("$webRootDir/$folderName/$appname.zip"))

Related

retrieving files from ftp to specific os directory using python 3

I'm trying to automate a daily ftp transfer using a Python3 script. I'm having a small issue though with writing the files were i want them to be. This is what I'm doing:
import time, os
from ftplib import FTP
from datetime import datetime
today=time.strftime('%d%m%y')
dirName='mydir'+today
if not os.path.exists(dirName):
os.mkdir(dirName)
print("Directory " , dirName , " Created ")
else:
print("Directory " , dirName , " already exists")
os.chdir(dirName)
start = datetime.now()
ftp = FTP('ftp')
ftp.login('user','pass')
ftpdir='localdir'+today
ftp.cwd(ftpdir)
# Get All Files
files = ftp.nlst()
# Print out the files
for file in files:
print("Downloading..." + file)
ftp.retrbinary("RETR " + file, open(dirName + file, 'wb').write)
ftp.close()
what I get with this code is that all the downloaded ftp files stay in the folder level above "today" while their filename start with the "today" str.
Can someone give a hand here please.
Thanks in advance
You have to separate the path components. For platform independent solution, use os.path.join:
import os
dirName = os.path.join('mydir', today)
Solved the issue with a bar:
# Print out the files
for file in files:
print("Downloading..." + file)
ftp.retrbinary("RETR " + file, open(dirName + '\\' + file, 'wb').write)
ftp.close()

How to get the name of the directory from the name of the directory + the file

In an application, I can get the path to a file which resides in a directory as a string:
"/path/to/the/file.txt"
In order to write another another file into that same directory, I want to change the string "/path/to/the/file.txt" and remove the part "file.txt" to finally only get
"/path/to/the/"
as a string
I could use
string = "/path/to/the/file.txt"
string.split('/')
and then glue all the term (except the last one) together with a loop
Is there an easy way to do it?
You can use os.path.basename for getting last part of path and delete it with using replace.
import os
path = "/path/to/the/file.txt"
delete = os.path.basename(os.path.normpath(path))
print(delete) # will return file.txt
#Remove file.txt in path
path = path.replace(delete,'')
print(path)
OUTPUT :
file.txt
/path/to/the/
Let say you have an array include txt files . you can get all path like
new_path = ['file2.txt','file3.txt','file4.txt']
for get_new_path in new_path:
print(path + get_new_path)
OUTPUT :
/path/to/the/file2.txt
/path/to/the/file3.txt
/path/to/the/file4.txt
Here is what I finally used
iter = len(string.split('/'))-1
directory_path_str = ""
for i in range(0,iter):
directory_path_str = directory_path_str + srtr.split('/')[i] + "/"

Groovy split path into name and parent

I am trying to split a path into parent and the name.
When trying
String path = "/root/file"
File file = new File(path)
println("Name: " + file.name)
println("Parent: " + file.parent)
We get
Name: file
Parent: /root
With the Windows path C:\\root\\file.exe we get
Name: C:\root\file.exe
Parent: null
Is this the intended behaviour? And if it is how do I get the same result for a Windows path? (If possible please without using regular expressions)
use .replace to change the "\" to "/
String path = "C:\\root\\file.exe"
path = path.replace("\\","/")
File file = new File(path)
println("Name: " + file.name)
println("Parent: " + file.parent)

Why is File.separator using the wrong character?

I'm trying to add functionality to a large piece of code and am having a strange problem with File Separators. When reading a file in the following code works on my PC, but fails when on a Linux server. When on PC I pass this and it works:
fileName = "C:\\Test\\Test.txt";
But when on a server I pass this and get "File Not Found" because the BufferedReader/FileReader statement below swaps "/" for "\":
fileName = "/opt/Test/Test.txt";
System.out.println("fileName: "+fileName);
reader = new BufferedReader(new FileReader(new File(fileName)));
Produces this output when run on the LINUX server:
fileName: /opt/Test/Test.txt
File Not Found: java.io.FileNotFoundException: \opt\Test\Test.txt (The system cannot find the path specified)
When I create a simple Test.java file to try and replicate it behaves as expected, so something in the larger code source is causing the BufferedReader/FileReader line to behave as if it's on a PC, not a Linux box. Any ideas what that could be?
I don't see where you used File.separator. Try this instead of hard coding the path separators.
fileName = File.separator + "opt" + File.separator + "Test" + File.separator + "Test.txt";

Getting the last modify date of a directory in ftp

How do i get when a directory was recently modify via ftp using c#
I have tried this, with no luck
FtpWebRequest ftpRequest = (FtpWebRequest)WebRequest.Create(new Uri("ftp://" + ftpAddress + "/" + "public_html" + "/" + this.Url));
ftpRequest.Credentials = new NetworkCredential(FTPUsername, FTPPassword);
ftpRequest.Method = WebRequestMethods.Ftp.GetDateTimestamp;
DateTime FtpFileLastModified = ((FtpWebResponse)ftpRequest.GetResponse()).LastModified;
return FtpFileLastModified;

Resources