Gradle - Move a folder from ABC to XYZ - file

Directory Structure:
Project1/ABC/file1.txt
I want the above ABC folder moved/renamed to XYZ (without leaving ABC there).
How can I do this using Gradle. Seems like in Gradle: For a right hand person, it's itching your right ear using your left hand, taking it across top of your head.
I have used the following example: but it doesn't do anything:
task renABCToXYZ(type: Copy) << {
copy {
from "Project1"
into "Project1"
include 'ABC'
rename ('ABC', 'XYZ')
}
}

Your task declaration is incorrectly combining the Copy task type and project.copy method, resulting in a task that has nothing to copy and thus never runs. Besides, Copy isn't the right choice for renaming a directory. There is no Gradle API for renaming, but a bit of Groovy code (leveraging Java's File API) will do. Assuming Project1 is the project directory:
task renABCToXYZ {
doLast {
file("ABC").renameTo(file("XYZ"))
}
}
Looking at the bigger picture, it's probably better to add the renaming logic (i.e. the doLast task action) to the task that produces ABC.

With the solution given above you can rename files and folders, you can move files, but you cannot move folders to another destination with renameTo.
For this case you have to use ant.move:
ant.move(file: sourceDir, tofile: targetDir)
Remark: The question is about renaming folders. So the answer above is correct (but the question is a little bit ambiguous). But maybe my answer is helpfull for other users who stumple upon this question and want move move a folder.

Following code will move a file from one directory to another and will rename file
task wb764Jar( type: Jar ) {
doFirst{
copy {
from 'deployment/alpha/workbench_alpha7_64.jnlp'
into 'build/libs/jar_merge/developed/alpha64/JNLP-INF/'
rename('workbench_alpha7_64.jnlp', 'APPLICATION.JNLP')
}
}
baseName = 'WorkbenchMaster7_64'
from files(wbLibsDir + '/jar_merge/developed/alpha64/')
from zipTree("$wbJar.archivePath")
}

Related

Trying to loop through Directory to list all the files using lua

I am new to lua to trying to understand and put pieces to together and looking out for some help.
I have gone through the existing articles on lua file looping but unable to get the desired output.
Question - I have a folder with files, Folder path - "D:\Test_Files\Outbound\Client\final"
Files in the folder with extension - .txt
Trying to :
Get the count of files in the folder(in this case "final" folder).
Read every file, building a loop something similar to this:
list = {}
for i=0,(#Totalfilecount) do
local fr = io.open('D:\Test_Files\Outbound\Client\final\'..filename.,'rb')
local f = fr.read('*.txt')
Customfunction(f) -- Passing file content to customfunction to apply business logic.
end
Questions :
How to get file count from a directory?
How to read the directory to check if the files with "*.txt" exist?
How to use table list to store each file name and read through the loop?
How to read each file via loop and pass the value to function "Customfunction(f)"?
Code is expected to run on windows. Please share suggestions in pure lua without using external file system functions such as 'lfs' as we do not like to import external functions.
Any Suggestions/help will be greatly appreciated!
You can't (at least shouldn't) do this without extensions to Lua. To accomplish this, you have to download LuaFileSystem library. You can do it using LuaRocks:
$ luarocks install luafilesystem
Use library as such:
require "lfs"
function dirtree(dir)
assert(dir and dir ~= "", "Please pass directory parameter")
if string.sub(dir, -1) == "/" then
dir=string.sub(dir, 1, -2)
end
local function yieldtree(dir)
for entry in lfs.dir(dir) do
if entry ~= "." and entry ~= ".." then
entry=dir.."/"..entry
local attr=lfs.attributes(entry)
coroutine.yield(entry,attr)
if attr.mode == "directory" then
yieldtree(entry)
end
end
end
end
return coroutine.wrap(function() yieldtree(dir) end)
end
An example use of code above:
for filename, attr in dirtree("D:\Test_Files\Outbound\Client\final") do
print(attr.mode, filename)
end
You have to check does extension equal to txt. To read file extension use this snippet:
function GetFileExtension(path)
return path:match("^.+(%..+)$")
end
So, to answer your question(s), you can get amount of files in directory just by counting elements in array returned in dirtree. To answer second question, just use code from the post. Table that you want is returned by dirtree(), but you may want to extract only .txt files from it. To read a file, just check other SO answers. You've got given name (in array), so use it.
EDIT: You can parse result of dir and ls command to get directory listing, but you shouldnt. Althrough this way you wouldn't need to install any libraries, your code is going to be heavily OS-depedent.
Adding libraries to your code isn't so bad. Hacking things is worse.
(Not sure file extension extracting function is going to work. I didn't make dirtree code used in this post, it belongs to David Kastrup)

Zip a folder using SSIS

I am trying to zip a Folder in SSIS, there are 12 files in the source folder and I need to zipthat folder. I can get the files to zip fine my problem is the folders.
I have to use winzip to create the zipped packages.
Can anyone point me to a good tutorial. I haven't been able to implement any of the samples that I have found.
Thanks
Adding a Script Task, yuo can use the ZipFile (class) here reference, you must refer to the System.IO.Compression.FileSystem assembly in the project (.NET Framework 4.5).
You need to provide to the Script Task the folder to be zipped and the name of the compressed folder as ReadOnlyVariables (to be added in the tab ReadOnlyVariables)
These two variables must be defined in the Variables tab (String type) of the package and can be changed dynamically through a cycle (eg. for each)
I use these two variables:
sFolderCompressed - the folder '.zip' that you want to obtain eg. C:\folder1\result.zip
sFolderSource - the source folder containing the files affected eg. C:\folder1\folder2
The script is made using c#, choose Script Language: Microsoft Visual C#
This is the code to be added in the Main method:
using System.IO.Compression;
public void Main()
{
try
{
string zipPath = (string)Dts.Variables["User::sFolderCompressed"].Value;
string startPath = (string)Dts.Variables["User::sFolderSource"].Value;
ZipFile.CreateFromDirectory(startPath, zipPath);
}
catch (Exception objException)
{
Dts.TaskResult = (int)ScriptResults.Failure;
// Log the exception
}
Dts.TaskResult = (int)ScriptResults.Success;
}
I hope can help.
Try using 7zip it is free. Take a look at 7zip command line user guide it contains all commands you need
And use a script task or an execute process task to achieve this. Also there are other useful links:
https://www.dotnetperls.com/7-zip-examples
UPDATE 1
you can follow this link for winzip:
http://www.vbforums.com/showthread.php?202918-Well-WinZip-Command-Line-Folders-to-Zip-keep-folder-structure-
In the link above they suggested using this command:
wzzip "c:\Test.zip" "c:\myfolder" -exPR
Write these things in bat file...
"C:\Program Files\WinZip\WINZIP64.EXE" -a "C:\Desktop\destination_folder\Sample.zip" "C:\Desktop\Sample"
In Execute process task:
Mention the location of bat file in Execute process Task-->Process-->Executable.
It's work fine.

Delete different named folders using batch

I've been doing a little research, and I haven't find anything remotely similar to what I'm looking at, so here's my case:
I have a folder, lets name it MasterFolder
So, lets say that my MasterFolder, has inside of it, four more folders, I know I can use
cd C:\MasterFolder\
to leave my CMD console just on top of those so, for didactic meanings, lets call them:
- Folder_A
- Folder_B
- Folder_C
- Folder_D
So, inside each of those folders, there are even more folders, that are named
"EncodingMaster_Originals"
the name is the same for all, so, that way, we could say that the folders are, for example:
- Folder_A\EncodingMaster_Originals (and OTHER files that I won't delete)
- Folder_B\InsideFolder_B\EncodingMaster_Originals (In another folder inside Folder_B
Those two are the most simple examples.
So, what I want to do?
Well, is simple, I just want to delete the folder named
"EncodingMaster_Originals"
from each of the folders, without having to be going into each one (There's around 200 folders with those little ones inside, I've made some massive re-encoding of files, and those folders appeared from nowhere).
I think that I've to use this line of code:
rd
(the Remove directory command), but my problem is that I don't know what I have to tell to the RD command, I don't want to look more dumb so I won't type what I've tried, because it was obviously wrong.
If you could help me, I would appreciate it.
So, after a brief search with a different bunch of words. I found THIS post:
How to remove all folders of name x within a directory using cmd/batch file
And the answer is this comment:
https://stackoverflow.com/a/10399706/4625105
Thanks if you were about to answer, I think the case is closed.

How to copy folder contents recursively in gradle zipTree?

copy {
from zipTree(rootDir.getPath() + "/archive.war")
include 'dirAbc/subdir/'
into 'src/dirEfg/subdir/'
includeEmptyDirs = false
}
I am trying to copy a folder and its contents (including subfolders and their contents), from a war file to a local location.
I have almost got what I want. The problem is the above leaves me with the following directory structure:
src/dirEfg/subdir/dirAbc/subdir/
instead of what I want and was expecting:
src/dirEfg/subdir/
What am I doing wrong?
It looks like this is currently not possible out of the box with the current implementation of zipTree in Gradle.
This enhancement is tracked in this ticket.
There are some workarounds you can try as suggested in this post in the Gradle forums.
One of them hints at extracting the zip tree and moving/renaming the contents.

groovy exclude .svn directory from while traversing thru all sub-directories

I am trying to traverse through all files and sub-directories excluding .svn directory. Can somebody please let me know how to this?
In what regards? Build tools like GMaven already handle .svn directories. If you are writing a groovy script to do something on your filesystem, then you'll have to handle it yourself.
Something like:
def dir = new File('some/path')
dir.eachFileRecurse { file ->
if (file.toString().contains(".svn")) { return }
// handle your processing
if (file.isDirectory()) { // do some directory processing
}
// etc
}
There's a grails page on adding grails to subversion, but I haven't had any problems.
If you're writing build scripts, you may want to consider gradle # gradle.org - you get simplified domain specific languages for builds and can mix in groovy to handle special cases

Resources