Converting .sh shellscript file to .bat batch file - batch-file

I have a .sh script
#!/bin/sh
LOOK_FOR="codehaus/xfire/spring"
for i in `find . -name "*jar"`
do
echo "Looking in $i ..."
jar tvf $i | grep $LOOK_FOR > /dev/null
if [ $? == 0 ]
then
echo "==> Found \"$LOOK_FOR\" in $i"
fi
done
can someone help me in convert this to .bat script which runs on windows.
Regards,
DH

#echo off
setlocal enableextensions
set "LOOK_FOR=codehaus/xfire/spring"
for %%a in ("*.jar") do (
echo Looking in %%a
jar tvf "%%a" | find "%LOOK_FOR%" >nul && echo Found in %%a
)
Assuming, of course, you have jar.exe in the path and the current folder contains the .jar files (the same assumptions in the .sh file)

Related

Keep the same file name on output file on a batch file

I'm currently working on an encrypting/decrypting automatization project and I need to create batch files for the task scheduler
And I'm facing the issue with the decrypting command
--passphrase "password" --batch -d --output ".JOAARPT.out" "*.JOAARPT"
the command decrypts all the JOAARPT files and changes the output file to JOAARPT.out but I cannot make the created decrypted file keep the same name as the source file
What wildcard should I use?
In cmd.exe a FOR loop is needed. Yes, it is awkward, but it is what it is. Use the FOR /? command to learn all about it.
FOR /F "delims=" %%A IN ('DIR /B "*.JOAARPT"') DO (
decry.exe --passphrase "password" --batch -d --output "%%~nA.JOAARPT.out" "%%~A"
)
If you wanted to step up to PowerShell, you could:
Get-ChildItem -File -Filter "*.JOAARPT" |
ForEach-Object {
& decry.exe --passphrase "password" --batch -d --output $($_.BaseName + '.JOAARPT.out') "$_.FullName"
}

How to find files NOT containing a defined text and output their names into a file?

I am having an application folder with sub-folders and thousands of files in it. I want to write a batch script which lists all the files which DO NOT contain particular text, say SAMPLE_TEXT and redirect output to a file. Please help with the script.
Inspired by http://tobint.com/blog/powershell-selecting-files-that-dont-contain-specified-content/ this powershell worked well for me;
Get-ChildItem -include *.sql -recurse | ForEach-Object { if( !( select-string -pattern "USE " -path $_.FullName) ) { $_.FullName}} > FilesMissingUse.txt
In my case I was searching for database scripts (.sql files) which were missing "USE " string.
This may help you - launch it in the top level folder.
#echo off
(for /r %%a in (*) do find "SAMPLE_TEXT" "%%a" >nul || echo %%a)>file.log
#echo off
findstr /S /M /V "SAMPLE_TEXT" *.* > output.txt
With grep you can use
grep -L Font *.pdf > list_of_files.txt
The -L switch returns only files that do not contain the string "Font."

Writing a batch utility to archive files on Windows Server 2008 R2

I need to write a command line (PowerShell or DOS) utility that will
archive all files created in between specified dates present in folder
X and store them as a .zip package in the same folder.
This utility will be scheduled into windows scheduler where arguments like to and from dates, store location will be provided to it and it will run at specified duration for e.g. at 12:00 noon daily.
Is it possible to write it as a batch .bat file?
Are there any built-in functionalities inside windows to zip files or I would need to use a third party program like 7-Zip etc for it.
I am not looking for any spoon feeding but just a direction, can anyone guide me to a tutorial or something. Both DOS based and PowerShell based solution would work for me.
Please help me out with this.
Thank you.
I would go for PowerShell.
With PowerShell you can easyly build and compare dates with the Get-Date commandlet and comparison operators. You could try something like:
$folderPath = 'C:\Temp'
$date1 = Get-Date '2014-05-01'
$date2 = Get-Date '2014-05-31'
foreach( $item in (Get-ChildItem $folderPath) )
{
if( $item.LastWriteTime -ge $date1 -and $item.LastWriteTime -le $date2 )
{
# Compression step
}
}
As for compression you have several options like stated in this question.
And for the script parameters you can check this blog article.
Here is a batch file that builds on Cristophe C's powershell script
It takes a folder and two dates on the command line and writes the last-modified filenames from date1 to date2 into a text file called daterange.txt
Some code can be added to zip up the files if further details are added to the question.
#echo off
if "%~3"=="" (
echo "%~0" "c:\folder" yyyy-mm-dd1 yyyy-mm-dd2
echo(
echo( This returns last-modified files in the folder from the two dates inclusive
echo( and puts them in a file called "daterange.txt"
echo(
echo( Requires Powershell V3+ (Powershell scripting also needs to be enabled^)
echo(
pause
goto :EOF
)
set "file=%temp%\psdaterange.ps1"
(
echo( $folderPath = '%~1\'
echo(
echo( $date1 = Get-Date '%~2'
echo( $date2 = Get-Date '%~3'
echo(
echo( foreach( $item in (Get-ChildItem -file $folderPath^) ^)
echo( {
echo( if( $item.LastWriteTime -ge $date1 -and $item.LastWriteTime -lt $date2.AddDays(1^) ^)
echo( {
echo( Write-Host $folderPath$item
echo( }
echo( }
) >"%file%"
powershell "%file%" > "daterange.txt"
del "%file%"
THere is Compress-Archive utility available in powershell 5.0.
# Create a zip file with the contents of C:\Stuff\
Compress-Archive -Path C:\Stuff -DestinationPath archive.zip
# Add more files to the zip file
# (Existing files in the zip file with the same name are replaced)
Compress-Archive -Path C:\OtherStuff\*.txt -Update -DestinationPath archive.zip
# Extract the zip file to C:\Destination\
Expand-Archive -Path archive.zip -DestinationPath C:\Destination
See this answer for more details:
How to create a zip archive with PowerShell?

DOS Batch file - Copy file based on filename into folder

I would like to use a batch file to put them into default folder, but the account name is in the middle of the folder. Have any script I can use in dos command prompt?
888123AA.pdf
888123BB.pdf
888123CC.pdf
777456AA.pdf
777456BB.pdf
777456CC.pdf
Default folder:
999-888123-03
666-777456-01
#echo off
setlocal EnableDelayedExpansion
rem Process all .pdf files
for %%a in (*.pdf) do (
rem Get just the file name, ie: "888123AA"
set fileName=%%~Na
rem Using the file name minus two last chars, ie: "888123"
rem get the default folder with that name
for /D %%b in (*-!fileName:~0,-2!-*) do (
rem And copy the file to that folder
copy "%%a" "%%b"
)
)
I don't remember any apparent way to do it other than a UNIX shell... Maybe get MSYS and use that (outdated) bash to help?
Here is a bash script that can use after you installed bash from MSYS (or you can sort it with a Linux box - Ubuntu is no bigger than 800MB and can run as LiveCD without interfering your current Windows system, and the LiveCD can double as a system saver when needed. :-)
#!/bin/bash
for each in ./*; do
if [ -d $each ]; then # Only folders are minded.
# Extract the second part of the folder name.
ACCOUNT_NAME=`echo $each | sed "s/\\-/\n/" | head -n 2 | tail -n 1`
cp -v ./$ACCOUNT_NAME*.pdf $each
fi
done

Rename all files in a folder using batch

I would like to create a batch file to rename all the files with extension ".log" in a folder to append with today's date.
For example :
App.log will be appended to App.log06112010
where date is 06112010.
Please suggest
forfiles /m *.log /c "cmd /c ren #file #file06112010"
#!/usr/bin/ksh
export TODAYSDATE=`date "+%m%d%Y"`
umask 000
for filename in $1
do
if [ ! -f $1 ]; then
echo "$filename doesn't exist!"
else
if [ -d $1 ]; then
echo "Skipping directory $filename..."
else
mv $filename $filename$TODAYSDATE
fi
fi
done
Usage: move.sh "*.log"

Resources