How to create a file in distributed system using ant script? - file

i am working with mqfte. i have to create a empty txt file with the same name as the source file after it is transferred to the destination. How can do this using ant script?
Scenario:
Srcpath: \src\test.dat
destpath: \dest\test.dat
After the file is moved from src path, i need to create a empty file test.dat in the src path using ant script? how can this be done?

Before you transfer the files, you could use the Touch task to create the new empty files matching those you will be transferring (reuse the same fileset) in a temporary directory. After the transfers are complete, you could use the Move task to copy the empty files into your src dir. You could use overwrite="false" in the Move task to ensure that files remaining in the src dir do not get replaced (e.g. if you wanted to be sure to have empty files only for successful transfers).
Here is an example.
<project default="test">
<target name="test">
<touch>
<fileset dir="src">
<include name="test*"/>
</fileset>
<mapper type="regexp" from="(.*)" to="tmp/\1"/>
</touch>
<move todir="dest">
<fileset dir="src">
<include name="test*"/>
<!-- simulate file not transferred -->
<exclude name="test.doc"/>
</fileset>
<globmapper from="test.*" to="result_*.txt"/>
</move>
<move todir="src" overwrite="false">
<fileset dir="tmp"/>
</move>
</target>
</project>
The first move stands in for your transfer. One file (test.doc) is not moved out of the src dir (simulate failed transfer). You can test be creating files with some content in the src dir. After the target completes, the test.doc should still have its original content. The other files should be empty.
In response to your question about the regexp mapper:
<mapper type="regexp" from="(.*)" to="tmp/\1"/>
This captures the whole of the incoming file name into a group...
from="(.*)"
and prepends "tmp/" to that captured group...
to="tmp/\1"
(In regular expression \1 refers to the first captured group in the expression. Groups are defined using parentheses.)
So from src/somefile.txt, we will get somefile.txt as the input file to the mapper, and we translate this to tmp/somefile.txt.

Related

MSBUILD Copy multiple files from source paths to different destination paths

I need to copy the files from different source paths to different destination paths.
Example Sources:
xyz\x.txt
pqr\p.img
Corresponding Destinations:
mno\x1.txt
qst\p_sth.img
I am trying to use the batch copy as follows:
Created a ItemGroup specifying source file and destination files:
Run batch Copy command:
<Project ToolsVersion="14.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003"
DefaultTargets="start">
<ItemGroup>
<src Include="xyz\x.txt">
<OutputFile>"mno\x1.txt"</OutputFile>
</src>
<src Include="pqr\p.img" >
<OutputFile>"qst\p_sth.img"</OutputFile>
</src>
</ItemGroup>
<Target Name="start">
<Message Importance="high" Text="Bulk Copy"/>
<Copy SourceFiles="%(src.FullPath)" DestinationFiles="%(src.OutputFile)"/>
</Target>
</Project>
I am getting the "Illegal character" error pointing to line #17, shown as:
Can I even achieve this using Copy command? Also RoboCopy command is acceptable.
[But, requirement is we need full source path with file name and full destination path with file name]
What is the best approach to handle this?
The solution is just removing the unrequired quotes from the output file paths (As suggested by #stjin):
<ItemGroup>
<src Include="xyz\x.txt">
<OutputFile>mno\x1.txt</OutputFile>
</src>
<src Include="pqr\p.img" >
<OutputFile>qst\p_sth.img</OutputFile>
</src>
</ItemGroup>

how to copy _PublishedWebsites Files?

In Fact, I use Visual Studio 2013, and I want to copy the _PublishedWebsites Files using a batch file after deployment of my build , so this is an image to my Build Definitions:
I'm using in the batch file, this command:
xcopy /s ...\_PublishedWebsites\subfolders ...\main\Service >> ...\output.txt
To be sure that the copy works well, I wrote the result of copying operation in output.txt, so the result is:
0 File(s) copied
I noticed that the copy operation was done before than the _Publishedfiles files are created.
Do you know how can I copy the _Publishedfiles files after their complilation ?
In addition, I don't have the rights for changing in .xaml files :(
Use publish profile and type file system which will copy _PublishedWebsites to sharelocation/location
for publish profile see
https://msdn.microsoft.com/en-us/library/dd465337%28v=vs.110%29.aspx?f=255&MSPPError=-2147217396
If you know _PublishedWebSites directory on build machine you can copy files to path you want . You can do something like this, i used this task for same issue.
<ItemGroup >
<MyProjectSource Include="$(OutputRoot)/MySource/**/*.*" />
</ItemGroup>
<Target Name="AfterCopy" AfterTargets="WebPublish">
<Copy SourceFiles="#(MyProjectSource)"
OverwriteReadOnlyFiles="true" DestinationFolder="$(PublishFolder)/% (RecursiveDir)"/>
OutputRoot = your _PublishedWebsites path
MySource = your project
PublishFolder = your destination folder
You can do like this in your csproj file or if you use tfs build definition you can add a copy file task step to defintion and just fill paramaters.

i am using Command line dataloader in Salesforce & create simple “process-conf.xml” file ,i want to give relative path insted of absolute path

I am using Command line data loader in Salesforce & create simple “process-conf.xml” file , I want to access csv file with relative path.
For Example:
<entry key="dataAccess.name" value="\Users\User\Desktop\Data Loader sample1\CSV Files\insertAccounts.csv"/>
And process.bat file is present in :-
C:\Program Files\salesforce.com\Data Loader\bin
Any suggestions?
process-conf.xml doesn't seem to allow relative paths so you'll have to script something prior to launching process.bat. Here's one solution, which involves creating a template, then using ant to substitute values:
https://force201.wordpress.com/2010/10/15/scripting-the-apex-data-loader-via-ant/

Rename files in a directory based on other directory

I'm having several outputs of a program that I need to compare with a "expected" ouput folder. But my problem is that the output files always have a different filename.
How can I rename all files in a folder to the same filenames in another folder. Here the example - the expected file structure (can of course change):
expected/folder1/file1.txt
expected/folder1/file2.txt
expected/folder2/file1.txt
expected/folder3/file1.txt
And my output looks like this (number of files and position is always equal):
result/folder1/fileOtherName1.txt
result/folder1/fileOtherName2.txt
result/folder2/fileOtherName1.txt
result/folder3/fileOtherName1.txt
I tried using ANT (because I know that), but was stuck, because I cannot select a file by the index (sorted alphabetically).
Here my pseudocode in ANT (but don't know how to continue):
<target name="foo">
<foreach>
<fileset dir="result" casesensitive="yes">
<include name="**/*.txt"/>
</fileset>
<antcall target="rename">
</antcall>
</foreach>
</target>
<target name="rename">
<!-- how can I access another fileset and take the correct file? -->
<!-- Here I got stuck -->
<echo message="foreach.file is ${foreach.file}" />
<echo message="foreach.dir is ${foreach.dir}" />
<echo message="foreach.name.ext is ${foreach.name.ext}" />
<echo message="foreach.name is ${foreach.name}" />
</target>
Thanks for any help, it must not be in ANT only - a BASH script or similar could do the job too.
How about
#!/bin/bash
IN_DIR="expected/folder1"
OUT_DIR="result/folder2"
IN_FILES=($IN_DIR/*)
OUT_FILES=($OUT_DIR/*)
for ((i=0; i<${#IN_FILES[#]}; i++)); do
mv ${OUT_FILES[i]} $OUT_DIR/$(basename ${IN_FILES[i]})
done
Tested it on some test dirs, and it works OK.
Note that if any of your file names contains a space, it does not work.
I personally don't know ANT, so I'll give it a shot in bash.
for i in *; do
k=1;
for j in $i/*; do
mv "$j" $i/file$k.txt;
k=$[$k+1];
done;
done
This iterates through the folders and increments a counter for every file in the folder. When we proceed to the next folder, the counter is reset.
I created a test directory structure like yours and it worked for me.

Concatenating Files - Including files in folders

I currently have this command:
copy /b *.txt newfile.txt
But I want to include all files with folders as well.
How can I do this? Is it possible to add this to Apache Ant as well?
I also consider doing this to minify JS files.
Im using windows and would like a command to run or batch file but having issues.
Is there anyway to remove lines as well?
Is there a better command to use than the one I am currently using?
UPDATE:
<target name="concatenate" description="Concatenate all js files">
<concat destfile="build/application.js">
<fileset dir="js" includes="*.js" />
</concat>
</target>
<target name="compress" depends="concatenate" description="Compress application.js to application-min.js">
<apply executable="java" parallel="false">
<filelist dir="build" files="application.js" />
<arg line="-jar" />
<arg path="C:\yuicompressor-2.4.7\build\yuicompressor-2.4.7.jar" />
<srcfile />
<arg line="-o" />
<mapper type="glob" from="*.js" to="build/*-min.js" />
<targetfile />
</apply>
Now i am using the above code but cant get it to include files within folders
As oers pointed out in the comment, Ant patterns use ** to recursively match directories. Here is the relevant Patterns section from the Ant manual:
To make things a bit more flexible, we add one extra feature, which makes it possible to match multiple directory levels. This can be used to match a complete directory tree, or a file anywhere in the directory tree. To do this, ** must be used as the name of a directory. When ** is used as the name of a directory in the pattern, it matches zero or more directories. For example: /test/** matches all files/directories under /test/, such as /test/x.java, or /test/foo/bar/xyz.html, but not /xyz.xml.
There is one "shorthand": if a pattern ends with / or \, then ** is appended. For example, mypackage/test/ is interpreted as if it were mypackage/test/**.
The "concatenate" target above would be:
<target name="concatenate" description="Concatenate all js files">
<concat destfile="build/application.js">
<fileset dir="js" includes="**/*.js" excludes="**/*.min.js" />
</concat>
</target>

Resources