topbanner_forum
  *

avatar image

Welcome, Guest. Please login or register.
Did you miss your activation email?

Login with username, password and session length
  • Thursday October 31, 2024, 6:50 pm
  • Proudly celebrating 15+ years online.
  • Donate now to become a lifetime supporting member of the site and get a non-expiring license key for all of our programs.
  • donate

Author Topic: Easy way to rename a file using the contents of a text file in batch or script  (Read 10484 times)

questorfla

  • Supporting Member
  • Joined in 2012
  • **
  • Posts: 570
  • Fighting Slime all the Time
    • View Profile
    • Donate to Member
For example.  I need to rename a file photo.jpg using the contents of a text file "whose.txt". 
In all cases, the text file will only contain one word with no special characters or spaces only normal alphabetic letters. 
For example: whose.txt could be the word "mypicture".  Running the script would rename photo.jpg to mypicture.jpg and do it even if it overwrites an existing file with the name "mypicture.jpg".

I know this is probably something simple but it is late i can't think :(
I tried to set a variable to the contents of the text file then use the variable to rename the jpg file but can't get things in the right order to work.

tomos

  • Charter Member
  • Joined in 2006
  • ***
  • Posts: 11,964
    • View Profile
    • Donate to Member
Not exactly what you're looking for, but may be of interest:

Hi all,
How could I do the following?
I have a folder with files and a text file with a list of names (actually it's a spreadsheet with two columns)
What I'd want to do is to drag and drop each file to a listbox so it gets renamed

I had to rename a bunch of files using a text list just this week.
Used Oscar's Renamer after a 2006 (!) recommendation here on dc.
it isnt in development, but is still being sold :down:
I used the trial -- it worked on Windows 7 x64
it was very fussy: wouldn't work for a while, I think possibly because of an extra carriage return at the bottom of the list of new names. But it did work in the end.

Also found a couple of sites showing how to do it using spreadsheet (and?). Can check my desktop history later and post link/s.
Tom

tomos

  • Charter Member
  • Joined in 2006
  • ***
  • Posts: 11,964
    • View Profile
    • Donate to Member
Not exactly what you're looking for, but may be of interest:

actually best go read that thread: there are various other suggestions there
Tom

jity2

  • Charter Member
  • Joined in 2006
  • ***
  • default avatar
  • Posts: 126
    • View Profile
    • Donate to Member
Maybe try with Bulk Rename Utility / actions/ import rename pairs (see their help file) + renaming options/prevent duplicates ?

Nod5

  • Supporting Member
  • Joined in 2006
  • **
  • Posts: 1,169
    • View Profile
    • Donate to Member
I need to rename a file photo.jpg using the contents of a text file "whose.txt". 
In all cases, the text file will only contain one word with no special characters or spaces only normal alphabetic letters. 

Do each txt filename have some pattern that link them to a unique original jpg filename? For example the new name for photo.jpg is in photo.txt , the new name for another_picture.jpg in another_picture.txt and so on. Then it would be very easy to make a batch script to handle the renaming.

But if the txt and jpg files have no such matching pattern, like in your whose.txt and photo.jpg example, then I guess the only option is a script that accept two files (the txt and jpg) as input. You'd then manually run that on each txt/jpg pair you have. If you can remember which txt belongs to which jpg that is  ;D

4wd

  • Supporting Member
  • Joined in 2006
  • **
  • Posts: 5,644
    • View Profile
    • Donate to Member
Code: Text [Select]
  1. set /p name=<whose.txt
  2. move /Y photo.jpg %name%.jpg

But if the txt and jpg files have no such matching pattern, like in your whose.txt and photo.jpg example, then I guess the only option is a script that accept two files (the txt and jpg) as input. You'd then manually run that on each txt/jpg pair you have. If you can remember which txt belongs to which jpg that is  ;D

Given @questorfla's previous requests it's likely to be an automated/timed script that only has to do one thing, (probably something to do with internal website generation), and this is just one step in it.

My guess anyway  :)
« Last Edit: March 13, 2018, 06:44 AM by 4wd »

questorfla

  • Supporting Member
  • Joined in 2012
  • **
  • Posts: 570
  • Fighting Slime all the Time
    • View Profile
    • Donate to Member
Absolutely correct 4WD.  It would be an automatic setup that I would add to another "master housekeeping" routine.  I have a lot of submissions from people that contain the necessary data for a specific job.  Part of this is a text file that contains the name that the final output should use.  Once the first program has it all compiled, it is just named "finished".jpg  The final step is to rename finished to the name of the person t belongs to which is included in their file name.txt.  I was hoping to find a way to use the contexts of the text file to rename the jpg file which , currently, i just go through foldcer by folder for each "finished".jpg and copy the contents of the name.txt file to use for renaming the finished.jpg file.
Tedious but gets the job done.
If i find a better way, i will be sure to post it in case anyone else could ever use as it appears there is a lack of utilities or scripts with that ability. I'm probably the only one who ever asked for one.
Thanks All.


4wd

  • Supporting Member
  • Joined in 2006
  • **
  • Posts: 5,644
    • View Profile
    • Donate to Member
I was hoping to find a way to use the contexts of the text file to rename the jpg file ...

It's there:

Code: Text [Select]
  1. set /p name=<whose.txt
  2. move /Y photo.jpg %name%.jpg

set reads the contents of the text file into a variable, move renames the file to the variable value with .jpg as the extension.

questorfla

  • Supporting Member
  • Joined in 2012
  • **
  • Posts: 570
  • Fighting Slime all the Time
    • View Profile
    • Donate to Member
Everything works BUT I end up with two extra spaces in the filename which i need to avoid if i can.
<edit>  the leading space in the name turned out to be from the text file itself.  a glitch.  The only problem is the center space between the variable and the added text  <edit>

Your code to extract the text file contents into a variable works but for some reason it leaves a leading space in the variable/  At the end,  i need to add the words "menucopy" to that variable without a space between that and original value

the output of the script gets to a file named  "%dmn%menucopy.png"
where %dmn% was the variable created from the text file and "menucopy" is just added text
But i cannot seem to get rid of a leading space added in the variable %dmn% and another one between it and the text "menucopy".

<Edit>  I forgot to setlocal EnableDelayedExpansion before adding the variables.  Problem solved.  Thanks for the input 4wd i needed your code to load the text into variables  <Edit>

I was gettong something like " jimsmith menucopy.png"  :)  Now I get "jimsmithmenucopy.png" !"!!  Perfect
« Last Edit: March 25, 2018, 07:26 PM by questorfla, Reason: added info as i remembered it ... Sadly.. :( »