title: script to rename folders & files based on input files
body: <p>I exported folders and files from a document management system and what I got is a collection of folders and nested files &amp; folders that have random names. Each folder and file has a descriptor file with extension .esw that contains a variable called SHORTNAME that holds the true folder and file name. My challenge is to now rename those folders and files back to their true name.</p>
  
  <p>I found a script that does this but it stops at line 13 where it says "$0". The only change I made is to rem out the dos2unix utility as I don't have it and don't know what it does either.</p>
  
  <p>Any tips how to get this working in shell or vbs?</p>
  
  <pre><code>#!/bin/sh 
  
  for fl in $(ls) 
  do 
  flo="$(echo $fl | sed s/\.esw//)" 
  if [ -d $flo ] 
  then 
  echo "cd $flo" 
  echo "$0" 
  echo "cd -" 
  cd $flo 
  $0 
  cd - 
  echo "checking $fl" 
  #    dos2unix -q -n $fl $fl 
  # Remove empty spaces and nonsense from description (replace with "_") 
  sd="$(cat $fl | grep SHORTDESC | sed s/\SHORTDESC=// | sed s/\ /_/ | sed s/\ /_/ | sed    
  s/\ /_/ | sed s/\ /_/ | sed s/\ /_/ | sed s/\ /_/ | sed s/\ /_/ | sed s/\ /_/ | sed s/\ 
  /_/ | sed s/\ /_/ | sed s/\ /_/ | sed s/\\./_/)" 
  rm $fl 
  if [[ "$sd" = '' ]] 
  then 
    echo "won't do that" 
  else 
    echo "mv $flo $sd" 
    mv $flo $sd 
  fi 
  else 
  echo "$fl" &gt; $$.tmp 
  if cat $$.tmp | grep esw &gt; /dev/null 
  then 
    rm $$.tmp 
    if cat $fl | grep SHORTDESC &gt; /dev/null 
    then 
      #dos2unix -q -n $fl $fl 
      ex="$(cat $fl | grep DOCEXT | sed s/\DOCEXT=\.//)" 
      sd="$(cat $fl | grep SHORTDESC | sed s/\SHORTDESC=// | sed s/\ /_/ | sed s/\ /_/ | 
   sed s/\ /_/ | sed s/\ /_/ | sed s/\ /_/ | sed s/\ /_/ | sed s/\ /_/ | sed s/\ /_/ | sed 
  s/\ /_/ | sed s/\ /_/ | sed s/\ /_/ | sed s/\\./_/)" 
      rm $fl 
      echo "rm $fl" 
      echo "mv $flo.* $sd.$ex" 
      rm $fl 
      mv $flo.* $sd.$ex 
  #      else 
  #        echo "$fl -- no SHORTDESC!" 
  fi 
  else 
    rm $$.tmp 
  fi 
  fi 
  done
  </code></pre>