|
Answer» For me to rename a set of rar files to remove a single space and replace it with a more compliant underscore so that I could then do a batch unrar to individual folders with names that I wanted to specify with a loop;
I did this:
Code: [Select]@setlocal @echo off echo renaming archives to remove SPACES... for /F "tokens=1-3 delims=. " %%i in ('dir /b *.rar') do call :do_rename "%%i" "%%j" "%%k" echo extracting archives to separate directories... for /F "tokens=1,2 delims=." %%i in ('dir /b *.rar') do call :do_extract "%%i" "%%j" goto :eof
:do_rename ren "%~1 %~2.%~3" "%~1_%~2.%~3"
:do_extract echo extracting %~1.%~2 mkdir "%~1" unrar e -inul "%~1.%~2" "%~1" But what if I have FILENAMES with an arbitrary number of spaces in the filename? Obviously I can't pass in an arbitrary number of parameters into the label call. Or can I? I would like to avoid parsing the data and storing count data in a temporary file. Ideally, it would best to do it all in a single loop traversal but is batch file processing that powerful? I am guessing it is but I don't know. Any help would be greatly appreciated. Have I made myself CLEAR enough?
Egads,
JoshIf you are using Windows XP, then you could do something like this...? Code: [Select]@echo off echo Renaming archives to remove spaces... for /F "eol= delims=" %%i in ('dir /b *.rar') do call :do_rename "%%i" goto :eof
:do_rename set RENVAR=%1 ren %RENVAR% %RENVAR: =_%
Here is an example of this code being run... Code: [Select]D:\test>dir /b a b c dddd e.rar a b c dddd.rar a b c.rar a b.rar a.rar test.bat
D:\test>test Renaming archives to remove spaces...
D:\test>dir /b a.rar a_b.rar a_b_c.rar a_b_c_dddd.rar a_b_c_dddd_e.rar test.bat
D:\test>
Please NOTE this code fails to rename filenames which contain percent (%) ... Code: [Select]D:\test>dir /b % b.rar a b c dddd e.rar a b c dddd.rar a b c.rar a.rar test.bat
D:\test>test Renaming archives to remove spaces... The system cannot find the file specified.
D:\test>dir /b % b.rar a.rar a_b_c.rar a_b_c_dddd.rar a_b_c_dddd_e.rar test.bat
D:\test>
Hope that helps, JamesA very elegant solution. I was not familiar with the SET function nor the use of the surrounding % character so I'll look them up to understand how it works. Thanks for the assistance. =)
Josh.how about a vbscript. doesn't break on % Code: [Select]Dim Spaces, ReplaceMent,FSO ,sDir Spaces=" " ReplaceMent = "_" sDir="c:\temp" 'Directory to search files Set FSO = CreateObject("Scripting.FileSystemObject") Set objFolder = FSO.GetFolder(sDir) For Each Files In objFolder.Files If Right(Files.Name,4) = ".rar" Then If InStr(1,Files.Name,Spaces) > 1 Then NewName = RenameFile(Files.Name,Spaces,ReplaceMent) WScript.Echo "NewName is ",NewName Files.Name = NewName DoUnrar(Files.Path) End If End If Next
Function RenameFile(TheFile,Pattern,ReplacePattern) FileBaseName = FSO.GetBaseName(TheFile) FileExt = FSO.GetExtensionName(TheFile) If Len(FileExt) = 0 Then FileExt = "" Else FileExt = "."&FileExt End If NewFileName = Replace(FileBaseName,Pattern,ReplacePattern,vbTextCompare) NewFileName = NewFileName & NewChars & FileExt RenameFile = NewFileName End Function
Function DoUnrar(TheFile) Dim Command Set WSHShell = CreateObject("Wscript.shell") Command = "unrar e -inul " & TheFile & " c:\temp" 'extract to c:\temp WScript.Echo Command Set execObj= WshShell.Run(Command) End Function
|