1.

Solve : rename file with file counter?

Answer»

i wanted to rename all my img001.jpg files to a more descriptive name like bday_001.jpg, bday_002.jpg

here is the LOGIC that I have in mind...

counter=000
for JPG files in folder PICTURE
do
counter = counter + 001
rename *.JPG to BDAY_.JPG
done

I can only code this in UNIX... I do not know how to do it in DOS. Please help.

Thank you.
Just save as filename.bat...

Code: [Select]@Echo Off
Echo.
If not exist %~f1.\*.JPG (Echo no .JPG files found in %~f1 & GoTo

:EOF)

Set Count=0
For %%A in (%~f1.\*.JPG) Do CALL :REN %%A
Set Count=
GoTo :EOF

:REN
Set /A Count=%Count%+1
Ren "%*" BDAY_00%Count%.JPG
Echo Ren "%*" %Count%.JPG
GoTo :EOF
Hope this helps .

8-)fffreakQuote

Just save as filename.bat...

Code: [Select]@Echo Off
Echo.
If not exist %~f1.\*.JPG (Echo no .JPG files found in %~f1 & GoTo

:EOF)

Set Count=0
For %%A in (%~f1.\*.JPG) Do Call :REN %%A
Set Count=
GoTo :EOF

:REN
Set /A Count=%Count%+1
Ren "%*" BDAY_00%Count%.JPG
Echo Ren "%*" %Count%.JPG
GoTo :EOF
Hope this helps .

8-)fffreak


Thanks! it worked!!!

However, after it renames the 10th file to BDAY_0010.jpg instead of BDAY_010.jpg. Is there a way that I can have it set to to start from 000 to 999.

Thank you!how can i specify folder in loop, what command i have to use.

for example i NEED to do this in c:\t3\test1.txt


If not exist %~f1.\*.JPG (Echo no .JPG files found in %~f1 & GoTo

Kindly do the needful.

Regards,
Mohamed Asif KPQuote from: MOHAMED ASIF KP on June 18, 2007, 04:53:40 AM
Kindly do the needful.



Mohammed, you need to note...

You should start a new topic. This belongs to somebody else.

In your new topic, post much more of your code.


sorry ... kindly appologies.... me henceforth i will do it.

Regards,
Mohamed Asif KPQuote from: tads98 on February 14, 2007, 11:58:54 AM
i wanted to rename all my img001.jpg files to a more descriptive name like bday_001.jpg, bday_002.jpg

here is the logic that I have in mind...

counter=000
for JPG files in folder PICTURE
do
counter = counter + 001
rename *.JPG to BDAY_<counter>.JPG
done

I can only code this in UNIX... I do not know how to do it in DOS. Please help.

Thank you.

sorry no dos for you. But vbscript
Code: [Select]Dim FSO,NewPrefix, OldPrefix, oDir, objFolder,NewFileName,counter
NewPrefix="bday_"
Set FSO = CreateObject("Scripting.FileSystemObject")
sDir = "C:\temp"
Set objFolder = FSO.GetFolder(sDir)
counter = 1
For Each Files In objFolder.Files
If FSO.GetExtensionName(Files) = "jpg" Then
NewFileName = NewPrefix & Padding(3,counter) & ".jpg"
Files.Name = NewFileName
counter=counter+1
End If
Next

Function Padding(ValueToPad, TheDigits)
theLen = Len(TheDigits)
If theLen < ValueToPad Then
Padding = STRING(ValueToPad-theLen,"0") & TheDigits
Else
Padding = TheDigits
End If
End Function
if you want to use it, save it as somename.vbs, then on command prompt, type cscript somename.vbs. Of course, you must change the sDir value to suit your case.


You did know, didn't you, that Windows Explorer has a built-in feature that will do 99% of what you want?

Let's say you have these files in a folder

img001.jpg
img002.jpg
img003.jpg

If you highlight them ***all*** and invoke rename (by hitting F2 or by right clicking the selection and choosing rename from the context menu), the first file in the selection opens the rename box. If you type "bday(0).jpg", the others will be renamed bday(1).jpg and bday(2).jpg.

I'm surprised, if you "code" in "UNIX", you don't know about Bash, which is very like DOS and NT, and you could then learn DOS command prompt in about 30 minutes.

Or is this your homework? Sorry if I'm being cynical here.

The NT prompt code for your logic above would look very similar to what you have written.




May This Help :

..TESTED..
Code: [Select]@echo off
REM set counter variable
set first=0
set second=0
set third=0
set path=C:\Picture

for /f "delims==" %%a in ('dir "%path%\*.jpg" /a:-d /b') do (
set pict=%%a
call :next
)
if /i "%first%"=="0" set first=
if /i "%second%"=="0" set second=
set /a third-=1
echo Total : %first%%second%%third% Picture was Succesfully Renamed
pause
exit

:next
if /i "%third%"=="9" set /a second+=1&&set third=0
if /i "%second%"=="9" set /a first+=1&&set second=0
ren "%path%\%pict%" "BDAY_%first%%second%%third%.jpg"
set /a third+=1
goto :eof



Discussion

No Comment Found