Gradle renaming file name with wildcards - file

I have the following task in my build.gradle
task zipConfiguration(type: Zip) {
def myDir = project(':SomeProject').projectDir.toString() + '/build/libs/'
from myDir
archiveName 'Output.zip'
destinationDirectory = file("$buildDir/libs")
}
build/libs of project SomeProject will always have one versioned jar file. For e.g. configuration_2021_06.jar
With every build, the jar created will have a new version number. So the name of the jar I am zipping is dynamic.
However, I want a constant name myconfig.jar to go inside the Output.zip
How can I rename configuration_*.jar to myconfig.jar ?
Tried rename ('*.jar', 'myconfig.jar') : Gives error Dangling meta character '*' near index 0
Tried rename ('configuration*.jar', 'myconfig.jar') : Doesn't rename it. I still see
configuration_2021_06.jar inside Output.zip

The * needs to be like:
rename ('configuration(.*).jar', 'myconfig.jar')

Related

How to move files in Google Drive automatically with Google Script, based on partial file name?

I'm rather new to Google Scripts. We are wanting to make all files uploaded to a single folder in Google Drive be automatically moved to other folders, based on part of their file name.
3 example files: APX PMT 05.02.2019, ALT PMT 05.03.2019, BEA PMT 05.04.2019
We want these files to be moved to their destination folders based on the first 3 letters of their file name. APX PMT 05.02.2019 gets moved to the APX folder, ALT PMT 05.03.2019 gets moved to the ALT folder, ect.
Do not have code samples as I'm extremely new to this. Move files automatically from one folder to another in Google Drive is a good start on me learning this, but still unsure how to make it move file based on only part of the file name.
Results: Wanting people to be able to upload files to a single destination, and the code auto moves them to their proper folders.
Test Code version 2.0 . Works as below if I remove the spaces between the 2 character sets (change BEA RFT to BEARFT or BEA_RFT) , as our workplace would like them sorted by the first 7 characters in the file name now. How can i make it work when there is a space in the characters?:
function moveFiles() {
var dfldrs=['BEA RFT', 'BEA ADJ', 'BEA PMT', 'BEA CHG'];//Seven letter prefixes
var ofObj={BEA RFT:'ID',BEA ADJ:'ID',BEA PMT:'ID',BEA CHG:'ID'};//distribution folder ids
var upldFldr=DriveApp.getFolderById('ID');
var files=upldFldr.getFiles();
while(files.hasNext()) {
var file=files.next();
var key=file.getName().slice(0,7);
var index=dfldrs.indexOf(key);
if(index>-1) {
Drive.Files.update({"parents": [{'id': ofObj[key]}]}, file.getId());
}
}
}
Moving Files
Please, Read these instructions before running script
You need to provide the three letter prefixes
You need to provide the distribution folder ids associated with each prefix
You need to provide the upload folder id
You need to run this program from your upload file script or provide an alternate trigger function as you desire.
You need to enable Advance Drive API version 2
The Code
function moveFiles() {
var dfldrs=['APX','ALT','BEA'];//Three letter prefixes
var ofObj={APX:'APX id',ALT:'ALT id',BEA:'BEA id'};//distribution folder ids
var upldFldr=DriveApp.getFolderById('folderid');
var files=upldFldr.getFiles();
while(files.hasNext()) {
var file=files.next();
var key=file.getName().slice(0,3);
var index=dfldrs.indexOf(key);
if(index>-1) {
Drive.Files.update({"parents": [{"id": ofObj[key]}]}, file.getId());
}
}
}
Drive API Reference

Create folder in explicit directory AIR

Like the title says I want to create a folder in a specific directory with Adobe Air.
If I use static methods of File like File.userDirectory works fine but I need to give the choice to select the directory.
I am trying this:
file.addEventListener(Event.SELECT, dirSelected);
file.browseForDirectory("Select a directory");
function dirSelected(e:Event):void {
trace(file.nativePath);
file.resolvePath("new_folder");
file.createDirectory();
}
Nothing happens
"resolvePath: Creates a new File object with a path relative to this File object's path, based on the path parameter (a string)."
So:
var newDir:File = file.resolvePath("new_folder");
newDir.createDirectory();

How to get the latest file from a directory using groovy?

I have a directory that contains a list of files. I wanted to get the latest file out of all the contents of the said directory. How will I do that?
I am using this code, but I am not getting the latest file out of it. Please help.
def fileDir = new File("A/B").listFiles().first()
Thanks.
As simple as:
new File( 'A/B' ).listFiles()?.sort { -it.lastModified() }?.head()
(taking the negative lastModified, as we want the newest file first)

Gradle - Add single file to JAR

I have the file "license.txt" in the root directory of my project. In the jar-task, I want to add this file to the (root folder of the) JAR file.
I tried
jar {
from '.' include 'license.txt'
}
but this replaces the other content (.class files) instead of adding a file.
And I do not want to add the license.txt to the resources folder, because I do not want to change my project structure just because of the build tool.
Who can help? Thank you!
To add a single file, you can simply do:
jar {
from "license.txt"
}
Your solution should also work if you scoped your include to your from by enclosing it in curly braces.
If you would like to add multiple files, you can do:
jar{
from{
["aaa.txt","bbb.txt"]
}
}
You would add multiple files to output jar as under:
jar {
// Update jar name according to Ascertia conventions
/**
* archiveFileName
* The archive name. If the name has not been explicitly set, the pattern for the name is:
* [archiveBaseName]-[archiveAppendix]-[archiveVersion]-[archiveClassifier].[archiveExtension]
*/
archiveFileName = 'database_postgresql.jar'
from(['build/classes/java/main','mappings/postgresql'])
}
All contents in below directories will be added:
build/classes/java
/main/mappings/postgresql

getting the file in a sub-folder # Isolated Storage - WP7

I would like to get all the files that a sub-folder holds in a string array.
So, I have tried something like the following:
var IOstore = IsolatedStorageFile.GetUserStoreForApplication();
string searchpath = System.IO.Path.Combine("product", ProductName);
string filesInSubDirs[] = IOstore.GetFileNames(searchpath);
But I got all the files in the "product" folder. I have also tried with "productname" only as the parameter.
Thanks for your help.
The search pattern for a sub-folder needs to include "*.*" at the end to pattern match any file, which would make your code something like the following:
var IOstore = IsolatedStorageFile.GetUserStoreForApplication();
string searchpath = System.IO.Path.Combine("product", ProductName);
searchpath = string.Format("{0}\\*.*", searchpath);
string filesInSubDirs[] = IOstore.GetFileNames(searchpath);
Something you might want to try. (this is sort of a left field answer, sorry). In my dropbox client http://sharpdropbox.codeplex.com/) I have a set of facades for System.IO.File, System.IO.FileInfo, System.IO.Directory, and System.IO.DirectoryInfo. They work pretty good and I have tested them.
Basically, you add a Using or Import for System.IO.IsolatedStorage and then PSFile, PSDirectory, PSFileInfo, or PSDirectoryInfo. It's saved me from having to remember all the nuances... for instance if you are querying a directory, it knows to add a slash, etc. BTW, the "PS" prefix stands for "Persisted Storage" which is what IsolatedStorage is sometimes called (starting them with an "I" implies they are interfaces.. and having no prefix makes things even more confusing).
Anyway, you can grab the code from source or I believe the last release had the DLLs for them (it's called something like "IsolatedStorageFacade-WP7")

Resources