Solved

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

Posted on 2012-03-12
9
290 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
  • 5
  • 4
9 Comments
 
LVL 68

Expert Comment

by:woolmilkporc
Comment Utility
#!/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
Comment Utility
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
Comment Utility
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
 

Author Comment

by:bakerg1
Comment Utility
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
Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

 
LVL 68

Accepted Solution

by:
woolmilkporc earned 500 total points
Comment Utility
#!/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
Comment Utility
Phenomenal help, both in quality and timeliness.  Thanks woolmilkporc!
0
 

Author Comment

by:bakerg1
Comment Utility
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
Comment Utility
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
Comment Utility
That worked.  Thanks again!
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

The purpose of this article is to fix the unknown display problem in Linux Mint operating system. After installing the OS if you see Display monitor is not recognized then we can install "MESA" utilities to fix this problem or we can install additio…
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 fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …
In this sixth video of the Xpdf series, we discuss and demonstrate the PDFtoPNG utility, which converts a multi-page PDF file to separate color, grayscale, or monochrome PNG files, creating one PNG file for each page in the PDF. It does this via a c…

728 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now