Ant : get files from sub-folders of a directory - loops

i have a tree like this :
Folder1
-Sub-folder1
-file1
-file2
-Sub-folder2
-file3
-file4
-...
-...
I would like to copy all files (without sub-folders) in an other directory. I think about "for loop" but I can't get just files.

Related

How do I copy files based on a list, into their corresponding folders which are also created based on a list?

So, I'm completely new to scripting and batch files.
I'm looking to copy files based on a list of the files to copy. I have a text file for the list of files to copy and I want the script to copy all these files line by line from a source directory.
For example I have over a 1000 files to copy, and there are 3 files for each folder I want to create and put them into. Below is an example of how the files names are formatted:
file3_AB12_autoc.pdf
file3_AB12.jpeg
file1_AB12.png
file3_CD34_autoc.pdf
file3_CD34.jpeg
file1_CD34.png
...etc...
Once these are copied, I want to move them into folders that I created using a text file and the command:
FOR /F %i in (folders_list.txt) do md %i
Now, with my script, I want to move the files it copies into their corresponding folders. Basically if the file name contains the folder name, then they should move into the folder. For example:
C:\AB12 THIS FOLDER SHOULD HAVE ALL THE FOLLOWING FILES
file3_AB12.jpeg
file3_AB12_autoc.pdf
file1_AB12.png
C:\CD34 THIS FOLDER SHOULD HAVE ALL THE FOLLOWING FILES
file3_CD34.jpeg
file3_CD34_autoc.pdf
file1_CD34.png
I have looked up other similar questions but nothing seems to work for what I want to do. I believe the links below are useful but I do not know how to put them together.
How do I copy files into folders based on the file containing the folder name?
Copy files based on a list stored in .txt file
In PowerShell, Move-Item will move the files. Then you just need regular expressions to determine the path and new file name, similar to:
$txt = "file3_AB12_autoc.pdf
file3_AB12.jpeg
file1_AB12.png
file3_CD34_autoc.pdf
file3_CD34.jpeg
file1_CD34.png
badfilename.txt"
foreach ($orig_file in $txt -split '\r\n') {
if ( $orig_file -match '^([^_]*)_([^_.]*)(.*)\.(.*)$' ) {
$dir = $matches[2]
$new_file = '{0}{1}.{2}' -f $matches[1], $matches[3], $matches[4]
'Moving {0} to {1}\{2}' -f $orig_file, $dir, $new_file
Move-Item -Path $orig_file -Destination ('{0}\{1}' -f $dir, $new_file)
}
else {
'{0} not processed' -f $orig_file
}
}

powershell compare-object file sync issue with the diff

I want to compare and sync two directories in two different locations. Using the diffs, I want to copy files from directory 1 to directory 2, and directory 2 to directory 1 (assuming each directory has files the other doesn't). I'll explain the issue with an example.
dir1: c:\john\abc
dir2: a:\mary\abc
let's say has 10 files. 1 diff.
c:\john\abc\folder\file.txt
a:\mary\abc\file.txt
If I do compare object dir1 dir2 -property name. This 1 diff doesn't show up because I'm only comparing file names. If I do compare-object dir1 dir2 property fullname then everything is a diff, so that won't work.
ok, I can get around that. I chop off the root dir path, so that I compare these:
folder\file.txt
file.txt
(2 diffs found)
Issue: How do I know which directory has the file.txt inside a folder, and which directory has file.txt in the root? (c:\john\abc vs a:\mary\abc) ?

file command + search directories and sub-directories

If file *.txt gets all text files from the current directory, how can I use it in order to get the text files from a specific directory and all its sub-directories?
maybe you are looking for
$ find . -name '*.txt' -exec file {} +

Search for exe file within a condition

I want to search for all .exe files greater than 200 kB or smaller than 120 kB in the current folder and its subfolders. Then I want to move them to another folder called "folder" and execute in this folder the file called "executable.exe" infinitely and show some information about its memory consumption.
Any ideas?
Using a gnu-ish find (not sure what mingw uses), something like this?
cd your_folder
find . -name '*.exe' \( -size +200k -o -size -120k \) -exec mv {} folder \;
cd folder
run_some_executable.exe
The find finds your files, exec's a move of each one to your folder.
Then it cd's to your folder and runs the executable.
You'd then have to run another tool to check memory consumption.

how to move files from one path to another path in unix using shell-script

i want to move logs files from one path(source path) to another path(destination path). This source path and destination path is present in one text file. as::
source_path=abc/xyz
source_path=pqr/abc
desination_path=abcd/mlk
so i read this file and stored paths in different variables.i have multiple source path so i stored it in a varible which acts as array. as below::
sourcePaths=`grep source_path myfile |cut -d= -f2`
destPath=`grep destination_path myfile |cut -d= -f2`
and i have only one destination path where i want to move all these files.
Now i want to travel through all these source path, find the logs file with specific extension, zip these all files and move to destination path.
i use loop to retrieve data from sourcePaths variable(this is just trial). as:;
for i in $sourcePaths
do
echo $i
done
Can anybody help me in this.
Thanks a lot for your reply Guys.Now i want to add some filter in this. I want to move files from Source path which are 10 days old,and i want to delete these files from source path after moving. Please help
Just one more question!! What if i want to store this zip file with name which contain time stamp,so that there will be no duplication of file name at destination path.
Hi All,one more question.When i am zipping the file with above option,it is creating subfolder in zip file according to its path.ie. if i am zipping logs files from src/int/xlog/abc, and when i unzip this zip file,these newly craeted zip file contents logs files in src/int/xlog/abc/abc.zip. i dont want this.what i want is that this zip file directly content abc.zip. Not in sub folder.
Hi Guys, i am back with one more question.Here using below script i am able to create filename of zip as base pathname.e.g.xyz.zip and abc.zip as per my above given path using below code::
timestamp=`date +%Y%m%d_%H%M%S`
zipFile=$destPath/$(basename $sourcePath).$timestamp.zip
But now i want to create this file name(i.e.Zip file name) more specific depends on its source path.Like if my text file contents
source_path=obj/imp/backup/logs/db1
source_path=obj/res/input/int/db2
desination_path=abcd/mlk
Now i want to create zip file name something like this.
backup_logs_db1.zip
input_int_db2.zip
means i want to use last three directory name to create new zip file name. It is provided that source path in text file contents more than three directory name.
So can anybody help me in this.
Thnaks in advance!!!
To create ZIP archives you can do this:
for sourcePath in $sourcePaths
do
zipFile=$destPath/$(basename $sourcePath).zip
find $sourcePath -type f -name "*.log" | xargs zip $zipFile -#
done
which will create
abcd/mlk/xyz.zip (containing all *.log files under abc/xyz) and
abcd/mlk/abc.zip (containing all *.log files under pqr/abc)
To create GZIPped archives you can:
for sourcePath in $sourcePaths
do
tarFile=$destPath/$(basename $sourcePath).tar
find $sourcePath -type f -name "*.log" -exec tar -uvf $tarFile {} \;
gzip $tarFile
done
which will create:
abcd/mlk/xyz.tar.gz (containing all *.log files under abc/xyz)
abcd/mlk/abc.tar.gz (containing all *.log files under pqr/abc)
To move files from source to dest, that have not been modified in the last 10 days, you can use this:
for sourcePath in $sourcePaths
do
find $sourcePath -type f -mtime +10 -name "*.log" -exec mv {} $destPath \;
done
If you want to delete the input files after zipping use the following:
for sourcePath in $sourcePaths
do
zipFile=$destPath/$(basename $sourcePath).zip
find $sourcePath -type f -mtime +10 -name "*.log" | xargs zip -mT $zipFile -#
done
Use the date command to generate a timestamp. The flags are:
%Y is the year
%m is the month
%d is the day
%H is the hour
%M are the minutes
%S are the seconds
Add the timestamp to the name of the zipFile, so it becomes:
for sourcePath in $sourcePaths
do
timestamp=`date +%Y%m%d_%H%M%S`
zipFile=$destPath/$(basename $sourcePath).$timestamp.zip
find $sourcePath -type f -mtime +10 -name "*.log" | xargs zip -mT $zipFile -#
done
Use the -j zip flag to store just the filename without the directory name. The new command will be:
for sourcePath in $sourcePaths
do
timestamp=`date +%Y%m%d_%H%M%S`
zipFile=$destPath/$(basename $sourcePath).$timestamp.zip
find $sourcePath -type f -mtime +10 -name "*.log" | xargs zip -jmT $zipFile -#
done
The following will iterate through all source paths, create a tar.gz file in $destPath. This .tar.gz contains a folder named $sourcePath containing logfiles (files with .log extension).
The name of the .tar.gz file will be based on the base name of $sourcePath, abc/xyz results in a file xyz.tar.gz to be created.
for sourcePath in $sourcePaths;do
name=`basename $sourcePath`
tar czf "$destPath/$name.tar.gz" $sourcePath/*.log
done
Note: your source_paths may not contain spaces or tabs, log files are not recursively searched.

Resources