troubleshooting Question

"Convert" LINUX Shell Script to Windows CMD Script

Avatar of D B
D BFlag for United States of America asked on
Windows BatchShell Scripting
6 Comments1 Solution1502 ViewsLast Modified:
I have the following script that runs in LINUX and need to get it to run in Windows as a .bat file.
Basically, I have a number of files in a directory that have the same filename with the exception of a date component (e.g. "bubbleicious_daily_20151228.zip", "bubbleicious_daily_20151221.zip", ...) and I need to parse out the date portion, compare it with the 'next' file in the ring, and determine whether to keep it, (and set comparer to that date value) or delete it. When done, I will have only one file left, and it will be the one with the most current date. I cannot use the actual file creation/update dates, since the files could all be dumped at the same time and even out of actual date order. I have to go by the filename.

If necessary, I can use substring parsing instead of token parsing as all files will be identical in name except for the date.

#!/bin/bash

FILES=*.zip
comparer=""
for f in $FILES
do
  a="${f%.*}"
  b=`echo $a|awk -F "_" '{print $3}'|awk -F "." '{print $1}'`
  if [ "$comparer" == "" ]; then
        comparer=$b
        continue
  fi
  if [ $(date -d $b +"%Y%m%d") -lt $(date -d $comparer +"%Y%m%d") ]; then
        echo "File name containing $b is older than the file containing $comparer. Keeping the file containing $comparer and deleting the file containing $b. "
#        rm *$b*.zip
  elif [ $(date -d $b +"%Y%m%d") -eq $(date -d $comparer +"%Y%m%d") ]; then
        echo "Cannot decide which one is newer for ending with $b"
  else
        echo "File name containing $b is newer than the file containing $comparer. Keeping the file containing $b and deleting the file containing $comparer."
#        rm *$comparer*.zip
        comparer=$b
  fi
done

Open in new window

ASKER CERTIFIED SOLUTION
NVIT
End-user support

Our community of experts have been thoroughly vetted for their expertise and industry experience.

Log in to continue reading
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform for $9.99/mo
View membership options
Unlock 1 Answer and 6 Comments.
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
See how we're fighting big data
The Value of Experts Exchange in My Daily IT Life

Experts Exchange (EE) has become my company's go-to resource to get answers. I've used EE to make decisions, solve problems and even save customers. OutagesIO has been a challenging project and... Keep reading >>

Mike

Owner of Outages.IO
Phoenix, Arizona, United States
Member Since 2016
Join a full scale community that combines the best parts of other tools into one platform.
Unlock 1 Answer and 6 Comments.
View membership options
“All of life is about relationships, and EE has made a virtual community a real community. It lifts everyone's boat.”
William Peck

Member since 2004