Page 1 of 1
Newbie here needs help to manipulate file names
Posted: 27 Aug 2009 13:14
by Jc_oz
5am here in Sydney and I have to put up my hand & say HELP!
What I need to do is straight forward but proving to be impossible for me.
I have found different threads here and tried cutting and pasting but I can't get the finished result.
So here is my humble request if anyone could help or point me in the right direction, I would be very grateful.
I have thousands of images which all need their file format changed.
lets say I have 3 files like below;
picture00001.tif
picture00002.tif
picture00003.tif
I need to take the numerals (last 5 digits excluding file type) and add them to the front with an "_" like this;
00001_picture.tif
00002_picture.tif
00003_picture.tif
finally, I want to move them from 1 directory to another as they are renamed...just incase
This part I have working but obviously I would like it working with the renaming functionality.
If possible... but not necessary... it would be nice if it had a counter, just so I can see the progress...
Any help that anyone can offer, is truly appreciated.
Thank you!
John
P.S. The batch job that I managed to put together (thanks to the posts here) is as follows (with all of it's imperfections);
@echo off
setlocal enabledelayedexpansion
set "sourcefolder=c:\ToBeRenamed"
set "destinationfolder=c:\Renamed"
:MAINLOOP
cd /d "%sourcefolder%"
move /y "*.txt" "%destinationfolder%
pause
IF NOT EXIST *.txt GOTO
ONE
goto MAINLOOP
ONE
ECHO ALL FILES MOVED - time to party
pause
Posted: 27 Aug 2009 16:43
by jaffamuffin
you want a for loop and some string manipulation
something like:
Code: Select all
setlocal enabledelayedexpansion
set indir=X:\toberenamed\
set outdir=X:\renamed\
for /f %%A in ('dir %indir%\*.tif /b /a-d /on') DO (
set filename=%%~nA
set filenumber=!filename:0,-5!
set newname =!filenumber!_picture.tif
copy %indir%!filename! %outdir%!newname!
echo %indir%!filename! copied to %outdir%!newname!
)
Note I always mix up the filenumber=!filename:0,-5! so you should check what you need there. I think that takes the last 5 charcters from the end of the string.
Posted: 27 Aug 2009 23:36
by Jc_oz
Thank you very much...
A very elegant script (too elegant for me to understand
)
Unfortunately it did not work for me and I have tried to debug it but really I am too green (complete dumb newbie - but I am trying)
This is the error I am getting;
The system cannot find the file specified.
C:\ToBeRenamed\picture00001 copied to C:\Renamed\
This is probably a simple problem, I think it is just dropping the file extension when it tries to copy but I can't figure out how to change your elegant script to fix it...
there is 1 more thing that I forgot to mention and that is the file name will not be called picture but is a variable, eg elephant001.tif will need to be 001_elephant.tif and cow002.tif will need to be 002_cow.tif and so on
Thank you in advance once again...
Regards,
John
Posted: 28 Aug 2009 09:57
by avery_larry
Oops -- jaffa had a space with a variable:
set newname =!filenumber!_picture.tif
should have been:
set newname=!filenumber!_picture.tif
Now -- you want to move and rename all .tif files in a folder. You want to take <whatevername><whatevernumber>.tif and change it to <whatevernumber>_<whatevername>.tif
Sounds like fun. Try this:
Code: Select all
@echo off
set "sourcedir=c:\tmp"
set "destdir=c:\tmp\test"
cd /d "%sourcedir%"
for %%a in (*.tif) do call :process "%%~a"
goto :eof
:process
set "fname=%~n1"
set "number="
:numloop
if defined fname (
echo %fname:~-1%|findstr /r [0-9] >nul 2>nul
if not errorlevel 1 set "number=%fname:~-1%%number%"&&set "fname=%fname:~0,-1%"&&goto :numloop
)
copy "%~1" "%destdir%\%number%_%fname%.tif"
goto :eof
Posted: 29 Aug 2009 09:42
by Jc_oz
avery_larry, I just want to say a big sincere thank you!!!
Works just like a bought one...BRILLIANT
I hope some other people will also benefit from your script here.
It is so elegant... in time I hope I understand your script better... I am still green as but very enthusiastic when I see how such a small script can make my life easier.
Thank you again and thanks to jaffamuffin for the effort too which got me almost there.
Thank you!!!!!!!!!!!!!!
Posted: 30 Aug 2009 08:26
by Jc_oz
Sorry I still need help...
I have 1 small issue which I did not realise (as I am trying to help a friend with this)
at the end of the file name there is an alpha letter (the ones that did not have this alpha are fine with the previous script, thanks again), eg somepic1234a.tif
I have spent all night trying to decode both Jafa and Avery's code with varying results but without success....
The end result I need for the example above is 12345a_somepic.tif
The numbers are definitely 5 digits and all the remaining files all have a single alpha character of some descript.
there is always a "-" between the words and the numbers BUT there are also -'s amongst the alpha part of the file name....
I really really do appreciate your time and efforts! If either of you could assist, that would be great.
To let you know what I have been trying to do, was simply assign another variable (I called it alpha for want of a better name) and tried to reconstruct the file name as per Avery's format.
Nothing but errors and weird results.
I am so sorry to bother you again.... any tips that you can provide will also be really appreciated as whilst I have not succeeded in what I wanted to achieve for my friend, I have really enjoyed the mental challenge that this exercise has provided and want to learn more....so I think this is not useful but just providing you with the info just in case...
Thank you once again and regards,
John
Posted: 30 Aug 2009 20:24
by DccD
If I understood correctly you want the script to check for a 5-digit sequence at the beginning of the filename and, if only 4 digits present, add the missing digit between the fourth and fifth character (alpha here)? But how can we tell the script what digit to put?
In your example the file somepic1234a.tif should result in 12345a_somepic.tif. But is this really a "5" that need to be added or any digit? If this is a "5" (in this example), then you just completed the sequence from 1 to 5. So you use your intelligence, and scripts are not intelligent
They do what you tell them to do.
Posted: 31 Aug 2009 08:14
by Jc_oz
Sorry... it was very late when I gave up trying to modify the scripts that have been supplied.
So allow me to be clear this time;
file somepic12345a.tif
needs to be converted to 12345a_somepic.tif
that is it in a nut shell.
There are some guarantees.
1) all files are TIFF images
2) the first part of the file name, ie "somepic" can be any length
3) the numbers are always 5 digits and will have preceding 0's if the number is small eg 00123
DccD, I hope this clarifies my need.
Thank you too all who read this post and a bigger thank you to any person who can shed some light and help me out.
Kind regards,
John
Posted: 31 Aug 2009 14:44
by avery_larry
OK --
variable who knows what to start the filename.
Exactly 5 digits. No more, no less, ever.
Sometimes an additional alphacharacter. Is this NEVER 2 or more characters?
Posted: 31 Aug 2009 18:56
by Jc_oz
Hi Avery_Larry,
Yes that is correct, in that the alpha after the 5 digit number will never be more than a single alpha character...
and because the previous script you provided works exactly as it should have, that means it can ignore/skip any file that does not have an alpha after the 5 digit number
Many thanks and regards,
John
Posted: 01 Sep 2009 15:20
by avery_larry
Untested, but should do the trick:
Code: Select all
@echo off
set "sourcedir=c:\tmp"
set "destdir=c:\tmp\test"
cd /d "%sourcedir%"
for %%a in (*.tif) do call :process "%%~a"
goto :eof
:process
set "fname=%~n1"
set "number=%fname:~-1%"
set "fname=%fname:~0,-1%"
:numloop
if defined fname (
echo %fname:~-1%|findstr /r [0-9] >nul 2>nul
if not errorlevel 1 set "number=%fname:~-1%%number%"&&set "fname=%fname:~0,-1%"&&goto :numloop
)
copy "%~1" "%destdir%\%number%_%fname%.tif"
goto :eof
This will work for any length of digits, 0 or 1 alpha character.
Could also do it like this for definite length of digits:
untested
Code: Select all
@echo off
set "sourcedir=c:\tmp"
set "destdir=c:\tmp\test"
cd /d "%sourcedir%"
for %%a in (*.tif) do call :process "%%~a"
goto :eof
:process
set "fname=%~n1"
echo %fname:~-1%|findstr /r [0-9] >nul 2>nul
if errorlevel 1 (
copy "%~1" "%destdir%\%fname:~-6%_%fname:~0,-6%.tif"
) else (
copy "%~1" "%destdir%\%fname:~-5%_%fname:~0,-5%.tif"
)
goto :eof
This allows for 0 or 1 alpha characters, but it must have exactly 5 digits.
Posted: 07 Sep 2009 06:08
by Jc_oz
Just a quick note to say a very sincere thank you.
Works PERFECTLY!
All the best to you!