Batch/CMD: Finding out every single folder file meta.xml - directory

i have a directory which contains the following:
-directory
-another directory
-meta.xml
-yet another directory
-meta.xml
...
So it needs to find in every directory 'meta.xml', all by itself (so that i dont have to type in every directory at the code) And i want that with EVERY meta.xml file a command is done.
Is there ANY possible way to do this, and could this be quiet done because the command i was talking about gives a text line from that meta.xml file, and i would like to not see whats its doing, that you get something like this:
text from meta file 1
text from meta file 2
text from meta file 3
and not
meta.xml found
text from meta file 1
meta.xml found
text from meta file 2
meta.xml found
text from meta file 3
If thats not clear, just think that i would just to like it run quietly, ok. (/Q maybe?)
this is the command i was talking about:
findstr "<name>" meta.xml >temp1.lis
FOR /F "tokens=2 delims=>" %%i in (temp1.lis) do #echo %%i > temp2.lis
FOR /F "tokens=1 delims=<" %%i in (temp2.lis) do #echo %%i > temp3.lis
it needs to do this for every single meta.xml file and giving the output so you get a total list when you do
type temp3.lis
Thanks!

Ok, as far as I can tell, this should work:
findmeta.cmd
#echo off
for /f "delims=" %%f in ('dir /b /s meta.xml') do (
FOR /F "tokens=2 delims=>" %%i in ('findstr "<name>" %%f') do #echo %%i > temp2.lis
FOR /F "tokens=1 delims=<" %%i in (temp2.lis) do #echo %%i >> temp3.lis
)
I modified your script part so answers are added to temp3.lis, instead of replacing it, and removed the use of the first temp file.
If, in the end, what you're looking for is what ever is between the tags, like
<name>blahblah</name> (the blahblah part)
this should do even faster, no temp files needed and one less for loop (broke into multiple lines for ease of reading, it all hold on a single line if needed.
for /f "delims=" %%f in ('dir /b /s meta.xml') do (
FOR /F "tokens=2 delims=><" %%i in ('findstr "<name>" %%f') do (
#echo %%i >> temp3.lis
)
)
Single line version:
for /f "delims=" %%f in ('dir /b /s meta.xml') do FOR /F "tokens=2 delims=><" %%i in ('findstr "<name>" %%f') do #echo %%i >> temp3.lis
Not sure which you prefer, IF this is what you're looking for.
EDIT:
Ok I think I got it, but I'm not sure it's gonna fix everything. Basically you want to change the tokens=2 to tokens=3 like this:
for /f "delims=" %%f in ('dir /b /s meta.xml') do FOR /F "tokens=3 delims=><" %%i in ('findstr "<name>" %%f') do #echo %%i >> temp3.lis
Now see if this works.

findstr /r /s ".*" meta.xml
will give you every line from every meta.xml file in the current directory and all its subdirectories, in the form:
dir1\dir2\dir2\meta.xml: line is here.
If you need anything more complex, I'd suggest installing Cygwin or GNU tools and using the bash shell or UNIX-like utilities from that, something like:
find . -name meta.xml -exec cat {} ';'

To find all meta.xml files:
dir meta.xml /s
Then you can pipe the output to some other command like
dir meta.xml /s | someCommand

I am not quite sure if I understood what you want. You want to find all files named meta.xml, but then do you want to search for something inside that file or do you want to see the complete content from that file? And you want to filter out any output regarding the name of the files found, you are only interested in the content of the files, right?
I am not sure if this is possible to do with cmd, but if you install cygwin you can easily do it the following way:
prompt>find top_level_directory -name meta.xml -print0 | xargs -0 grep -h "your search pattern"
for searching or
prompt>find top_level_directory -name meta.xml -print0 | xargs -0 cat
for outputting the complete file.

Related

How to find filenames from ls in a file in unix

I am trying to find if files in a directory, output of ls, exists in a file. So I have a file called test.txt inside this file I have few filenames like, V1.txt,v2.txt, v3.txt.
Now when I do ls I find list of files in the directory, I want to search if any of these files are on my test.txt file.
Example:
Sounds like you got two tasks you want to do.
1) loop through directory listing
2) search text file for any of the names of files that were in the directory
Test Setup
echo "V1.txt" > file_list.txt
echo "V2.txt" >> file_list.txt
mkdir testdir
touch testdir/V1.txt
touch testdir/V2.txt
touch testdir/V3.txt
Code
#!/bin/bash
for f in `ls testdir`
do
if grep -q $f file_list.txt; then
echo "found $f"
else
echo "not found $f"
fi
done

How to break the FOR Loop Inside the batch file?

Here is my code :
#echo off
for /f "tokens=2*" %%a in ('REG QUERY "HKEY_LOCAL_MACHINE\SOFTWARE\NCR\APTRA\Aggregate
Installer\Inventory\Aggregate\APTRA Self-Service Support" /f 06.04.01') do set "AppPath=%%~b"
echo %AppPath%
cmd /k
So when we run this batch file command prompt will open, so my requirement is when i click on Enter we need to break the FOR LOOP as the command prompt is not getting closed, Could any one please help me out ?
try with goto:
#echo off
for /f "tokens=2*" %%a in ('REG QUERY "HKEY_LOCAL_MACHINE\SOFTWARE\NCR\APTRA\Aggregate
Installer\Inventory\Aggregate\APTRA Self-Service Support" /f 06.04.01') do (
set "AppPath=%%~b"
goto :break
)
:break
echo %AppPath%
cmd /k

File name pattern matching in unix shell script [duplicate]

This question already has answers here:
binary operator expected error when checking if a file with full pathname exists
(3 answers)
Closed 8 years ago.
cd /DIR1/
if [ -f abc_*] ; then
ls -l abc_*| awk 'NR >0 && !/^d/ {print $NF}' >>list.txt
chmod 664 list.txt
else
echo "file not found"
fi
it giving the error as "Binary Operator expected "
If any files found in the directory starting with the pattern "abc_" it should create a file called list.txt and move the filenames to list.txt
-f checks for the existance of a single file, you can rather use
if ls abc_* 2>/dev/null; then
do stuff
fi
You could just use single liner find command like below:
find . -maxdepth 1 -name "abc_*" -printf "%f\n" >> list.txt
find command will find your files in current (replace . with /DIR1 to find in /DIR1) directory
maxdepth - just in directory specified and not recursively within each directories.
name name like abc_*
printf - print just the file name without directory prefixed.

Shell script to sort & mv file based on date

Im new to unix,I have search a lot of info but still don not how to make it in a bash
What i know is used this command ls -tr|xargs -i ksh -c "mv {} ../tmp/" to move file by file.
Now I need to make a script that sorts all of these files by system date and moves them into a directory, The first 1000 oldest files being to be moved.
Example files r like these
KPK.AWQ07102011.66.6708.01
KPK.AWQ07102011.68.6708.01
KPK.EER07102011.561.8312.13
KPK.WWS07102011.806.3287.13
-----------This is the script tat i hv been created-------
if [ ! -d /app/RAID/Source_Files/test/testfolder ] then
echo "test directory does not exist!"
mkdir /app/RAID/Source_Files/calvin/testfolder
echo "unused_file directory created!"
fi
echo "Moving xx oldest files to test directory"
ls -tr /app/RAID/Source_Files/test/*.Z|head -1000|xargs -i ksh -c "mv {} /app/RAID/Source_Files/test/testfolder/"
the problem of this script is
1) unix prompt a syntax erro 'if'
2) The move command is working but it create a new filename testfolder instead move to directory testfolder (testfolder alredy been created in this path)
anyone can gv me a hand ? thanks
Could this help?
mv `ls -tr|head -1000` ../tmp/
head -n takes the n first lines of the previous command (here the 1000 oldest files). The backticks allow for the result of ls and head commands to be used as arguments to mv.

How do you recursively unzip archives in a directory and its subdirectories from the Unix command-line?

The unzip command doesn't have an option for recursively unzipping archives.
If I have the following directory structure and archives:
/Mother/Loving.zip
/Scurvy/Sea Dogs.zip
/Scurvy/Cures/Limes.zip
And I want to unzip all of the archives into directories with the same name as each archive:
/Mother/Loving/1.txt
/Mother/Loving.zip
/Scurvy/Sea Dogs/2.txt
/Scurvy/Sea Dogs.zip
/Scurvy/Cures/Limes/3.txt
/Scurvy/Cures/Limes.zip
What command or commands would I issue?
It's important that this doesn't choke on filenames that have spaces in them.
If you want to extract the files to the respective folder you can try this
find . -name "*.zip" | while read filename; do unzip -o -d "`dirname "$filename"`" "$filename"; done;
A multi-processed version for systems that can handle high I/O:
find . -name "*.zip" | xargs -P 5 -I fileName sh -c 'unzip -o -d "$(dirname "fileName")/$(basename -s .zip "fileName")" "fileName"'
A solution that correctly handles all file names (including newlines) and extracts into a directory that is at the same location as the file, just with the extension removed:
find . -iname '*.zip' -exec sh -c 'unzip -o -d "${0%.*}" "$0"' '{}' ';'
Note that you can easily make it handle more file types (such as .jar) by adding them using -o, e.g.:
find . '(' -iname '*.zip' -o -iname '*.jar' ')' -exec ...
Here's one solution that extracts all zip files to the working directory and involves the find command and a while loop:
find . -name "*.zip" | while read filename; do unzip -o -d "`basename -s .zip "$filename"`" "$filename"; done;
You could use find along with the -exec flag in a single command line to do the job
find . -name "*.zip" -exec unzip {} \;
This works perfectly as we want:
Unzip files:
find . -name "*.zip" | xargs -P 5 -I FILENAME sh -c 'unzip -o -d "$(dirname "FILENAME")" "FILENAME"'
Above command does not create duplicate directories.
Remove all zip files:
find . -depth -name '*.zip' -exec rm {} \;
Something like gunzip using the -r flag?....
Travel the directory structure recursively. If any of the file names specified on the command line are directories, gzip will descend into the directory and compress all the files it finds there (or decompress them in the case of gunzip ).
http://www.computerhope.com/unix/gzip.htm
If you're using cygwin, the syntax is slightly different for the basename command.
find . -name "*.zip" | while read filename; do unzip -o -d "`basename "$filename" .zip`" "$filename"; done;
I realise this is very old, but it was among the first hits on Google when I was looking for a solution to something similar, so I'll post what I did here. My scenario is slightly different as I basically just wanted to fully explode a jar, along with all jars contained within it, so I wrote the following bash functions:
function explode {
local target="$1"
echo "Exploding $target."
if [ -f "$target" ] ; then
explodeFile "$target"
elif [ -d "$target" ] ; then
while [ "$(find "$target" -type f -regextype posix-egrep -iregex ".*\.(zip|jar|ear|war|sar)")" != "" ] ; do
find "$target" -type f -regextype posix-egrep -iregex ".*\.(zip|jar|ear|war|sar)" -exec bash -c 'source "<file-where-this-function-is-stored>" ; explode "{}"' \;
done
else
echo "Could not find $target."
fi
}
function explodeFile {
local target="$1"
echo "Exploding file $target."
mv "$target" "$target.tmp"
unzip -q "$target.tmp" -d "$target"
rm "$target.tmp"
}
Note the <file-where-this-function-is-stored> which is needed if you're storing this in a file that is not read for a non-interactive shell as I happened to be. If you're storing the functions in a file loaded on non-interactive shells (e.g., .bashrc I believe) you can drop the whole source statement. Hopefully this will help someone.
A little warning - explodeFile also deletes the ziped file, you can of course change that by commenting out the last line.
Another interesting solution would be:
DESTINY=[Give the output that you intend]
# Don't forget to change from .ZIP to .zip.
# In my case the files were in .ZIP.
# The echo were for debug purpose.
find . -name "*.ZIP" | while read filename; do
ADDRESS=$filename
#echo "Address: $ADDRESS"
BASENAME=`basename $filename .ZIP`
#echo "Basename: $BASENAME"
unzip -d "$DESTINY$BASENAME" "$ADDRESS";
done;
You can also loop through each zip file creating each folder and unzip the zip file.
for zipfile in *.zip; do
mkdir "${zipfile%.*}"
unzip "$zipfile" -d "${zipfile%.*}"
done
this works for me
def unzip(zip_file, path_to_extract):
"""
Decompress zip archives recursively
Args:
zip_file: name of zip archive
path_to_extract: folder where the files will be extracted
"""
try:
if is_zipfile(zip_file):
parent_file = ZipFile(zip_file)
parent_file.extractall(path_to_extract)
for file_inside in parent_file.namelist():
if is_zipfile(os.path.join(os.getcwd(),file_inside)):
unzip(file_inside,path_to_extract)
os.remove(f"{zip_file}")
except Exception as e:
print(e)

Resources