Solved

Need script to change portions of filenames in a directory from user input

Posted on 2012-03-12
9
294 Views
Last Modified: 2012-03-12
We use a program that creates multiple files based on an event number that is manually entered.  If the event number is entered incorrectly, we have to change the name on many created files; however, the event number is only a portion of the file name.  I only need to change the event number in the file name, is there a way to do that on all files within a specific directory through a script that requests the wrong event number and the correct event number from the user?  I have researched sed for this but have not been able to get it to work.  I am using Bash to write the script.
0
Comment
Question by:bakerg1
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 4
9 Comments
 
LVL 68

Expert Comment

by:woolmilkporc
ID: 37711401
#!/bin/bash

OLD="1234"
NEW="4567"
DIR="/path/to/dir"

ls $DIR/*OLD* | while read FILE
    do
      echo mv $FILE $(echo $FILE |sed "s/$OLD/$NEW/");
    done

Please note that I put echo in front of the "mv" statement so you can do kind of a "dry run".
Remove the bolded echo statement to perform the real operation.

Further, I added the old and new numbers as well as the directory as variables.
If you don't know how to request user input to fill these variables please let me know.

wmp
0
 

Author Comment

by:bakerg1
ID: 37711490
I will give this a try as soon as I can.  Will this work if the filenames are structured like "123456-009-23-051" and I need to change it to "123789-009-23-051"?
0
 
LVL 68

Expert Comment

by:woolmilkporc
ID: 37711543
As long as your intention is changing "456" to "789", yes.

Please note that the old as well as the new values must each consist of one contiguous  string, i.e.
my script cannot change "aaa123abc456zzz" to "aaa321abc654zzz" if you just give "123456" for OLD and "321654" for NEW. It can only work if you give "123abc456" for OLD and "321abc654" for NEW.

A problem can arise when the OLD string appears more than once in the filename. The script will change only the first occurrence. Changing all occurrences is fairly easy to accomplish, changing e.g the second occurrence if there are two but the first if there is just one would require significantly more effort.
0
Ransomware-A Revenue Bonanza for Service Providers

Ransomware – malware that gets on your customers’ computers, encrypts their data, and extorts a hefty ransom for the decryption keys – is a surging new threat.  The purpose of this eBook is to educate the reader about ransomware attacks.

 

Author Comment

by:bakerg1
ID: 37711826
That worked perfectly.  Would it be possible to also scan the contents of each file and make the same change for the event number?  This would then fully update the file with a single script.  Thanks for the help!
0
 
LVL 68

Accepted Solution

by:
woolmilkporc earned 500 total points
ID: 37711931
#!/bin/bash

OLD="1234"
NEW="4567"
DIR="/path/to/dir"

ls $DIR/*OLD* | while read FILE
    do
      FILENEW=$(echo $FILE |sed "s/$OLD/$NEW/")
      sed "s/$OLD/$NEW/" $FILE > $FILENEW
    done

This will rename the files and change their content in one go.

But please be aware that this will work only against the OLD filenames!
So if you already ran the first version of the script please let me know.
0
 

Author Closing Comment

by:bakerg1
ID: 37712001
Phenomenal help, both in quality and timeliness.  Thanks woolmilkporc!
0
 

Author Comment

by:bakerg1
ID: 37712096
The updated script works; however, it doesn't remove the old files.  The new files are listed along with the old ones - the new ones are completely changed though, which is good.  What am I missing?
0
 
LVL 68

Expert Comment

by:woolmilkporc
ID: 37712113
Nothing. It was my mistake. Sorry.

#!/bin/bash

OLD="1234"
NEW="4567"
DIR="/path/to/dir"

ls $DIR/*OLD* | while read FILE
    do
      FILENEW=$(echo $FILE |sed "s/$OLD/$NEW/")
      sed "s/$OLD/$NEW/" $FILE > $FILENEW
       rm $FILE
    done
0
 

Author Comment

by:bakerg1
ID: 37712229
That worked.  Thanks again!
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I. Introduction There's an interesting discussion going on now in an Experts Exchange Group — Attachments with no extension (http://www.experts-exchange.com/discussions/210281/Attachments-with-no-extension.html). This reminded me of questions tha…
When the confidentiality and security of your data is a must, trust the highly encrypted cloud fax portfolio used by 12 million businesses worldwide, including nearly half of the Fortune 500.
In this second video of the Xpdf series, we discuss and demonstrate the PDFimages utility, which, in a single command, is able to extract all the images from a PDF file and save each one in a separate image file (PBM, PPM, or JPG). Download and inst…
In this video, we show how to perform Bates Numbering/Stamping of PDF documents using Power PDF Advanced, the newest product from the Document Imaging division of Nuance Communications. There are two editions of Power PDF — Standard and Advanced. Th…

734 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question