Computing.Net > Forums > Programming > Batch Script To Rename File

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to get for your free account now!

Batch Script To Rename File

Reply to Message Icon

Name: Jawn
Date: August 6, 2008 at 06:45:19 Pacific
OS: XP
CPU/Ram: 2048
Manufacturer/Model: homemade
Comment:

This topic seems beaten into the ground, however, any help would be appreciated.

I'm trying to piece together a script that'll rename a season of a show.

I've been trying to learn myself. But, am still confused on several areas. For my learning experience if I am wrong could you please clarify?

:: renamer.bat
@echo off

:main
for %%f in (deadwood*.avi) do call :renamer %%f
goto :eof

:renamer
set old=%1
ren %1 %old:~3,9%
:: end

If the variable %%f is true(if there is a file named deadwood*.avi). Call's 'renamer'.
What is the second %%f stand for? Does this check if the variable is still true? If true repeat, if false go to end of file.

I don't understand the 'set old=%1' line. I think it means set variable old to the first character of the file name. My question is this the full file name? What part of the file name is sent to 'renamer'?

'ren' is the rename command. However, '%1 %old' I'm not sure what this all means - first character of old?
The last part '~3,9%': remove the following 3 chars and keep 9.

The an example file name would be 'deadwood.s02e01.ws.dvdrip.xvid-bia.avi' to be renamed to 'Deadwood S02E01.avi'

Thanks for any help, pointers, advice I receive. Regards


Report Offensive Message For Removal

Sponsored Link
Ads by Google

Response Number 1
Name: Hans Henrik
Date: August 6, 2008 at 07:33:15 Pacific
Reply:

i suggest you use "if exist" instead..

@echo off
cls
:sets
set /A Episode=0
:begin
set /A Episode=%Episode%+1
if exist "deadwood*Ep%Episode%*.avi" ren "deadwood*Ep%Episode%*.avi" "Deadwood Episode %Episode%.avi">>Log.log & goto begin
echo all done
pause
exit

(btw in case u didnt notice, i added a "log" instead of showing it on the CMD each rename)


Report Offensive Follow Up For Removal

Response Number 2
Name: ghostdog
Date: August 6, 2008 at 08:21:54 Pacific
Reply:

vbscript


Set objFS = CreateObject("Scripting.FileSystemObject")
strPath = "c:\test"
Set objFolder = objFS.GetFolder(strPath)
For Each strFiles In objFolder.Files
If InStr(strFiles.Name, "deadwood") > 0 And objFS.GetExtensionName(strFiles) = "avi" Then
strName = strFiles.Name
ArrSplit = Split(strName,".")
strExt = ArrSplit(UBound(Arrsplit))
strFirstLetterUpper = UCase(Mid(ArrSplit(0) ,1,1))
strTheRest = Mid(ArrSplit(0) ,2)
strNewName = strFirstLetterUpper&strTheRest&" "&ArrSplit(1)&"."&strExt
strFiles.Name = strNewName 'rename
End If
Next

save the above as script.vbs and on command line

c:\test> cscript /nologo script.vbs


Report Offensive Follow Up For Removal

Response Number 3
Name: Jawn
Date: August 6, 2008 at 09:38:14 Pacific
Reply:

Excellent. I tried the vbscript and it worked correctly. I also added two extra lines:

strSeason = UCase(Mid(ArrSplit(1) ,1,6))

And again,

strNewName = strFirstLetterUpper&strTheRest&" "&strSeason&"."&strExt

Output: Deadwood SxxExx.avi

Initially I couldn't get the batch code to work. However, for the sake of learning I'll try to use 'if exist' statement.

Many thanks Hans and Ghostdog.


Report Offensive Follow Up For Removal
Reply to Message Icon

Related Posts

See More







Post Locked

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.


Go to Programming Forum Home


Sponsored links

Ads by Google


Results for: Batch Script To Rename File

XP batch script to rename files www.computing.net/answers/programming/xp-batch-script-to-rename-files/14608.html

batch script to upload to ftp www.computing.net/answers/programming/batch-script-to-upload-to-ftp/16987.html

Batch file to rename without spaces www.computing.net/answers/programming/batch-file-to-rename-without-spaces/11415.html