remove characters from middle of a filename

August 26, 2010 at 10:19:35
Specs: Windows XP
I have the following file name:
0000070970302000-DAIOUTPUTHEADER_CODE_RTM(20100730_RTM_INITIAL1).csv

I would like to execute a batch file to rename all the files in the directory removing the text between the ( ). I would also like to remove all text before the "-"

I believe I need to use a for stmt. Please provide a brief description of what your solution does to fix my problem.

Thank You


See More: remove characters from middle of a filename

Report •


#1
August 26, 2010 at 11:18:41
I am not sure how this works but I got the (20100730_RTM_INITIAL1) part of the file to remove, but I am not sure how to combine the removing of the text before the "-"

for /f "tokens=1-2 delims=(" %i in ('dir *.csv /b') do ren "%i(%j" "%i.csv"


Report •

#2
August 26, 2010 at 18:51:03
download gawk for windows then do this

BEGIN{
 t=0
 for(i=1;i<=ARGC;i++){
   fn1=fn2=ARGV[i]
   if(fn1 ~ /\(/){
    gsub(/^.[^-]*-|\(.*/,"",fn1)
    newfile=fn1".csv"
    cmd="ren "fn2" "newfile
    print cmd
    # system(cmd)  #remove # to do actual renaming.
   }
 }
}

save the above as myrename.awk and then on commandline

C:\test>gawk -f myrename.awk *.csv
ren 0000070970302000-DAIOUTPUTHEADER_CODE_RTM(20100730_RTM_INITIAL1).csv DAIOUTPUTHEADER_CODE_RTM.csv

GNU win32 packages | Gawk


Report •

#3
August 26, 2010 at 20:06:35
Thanks for the reply however I can't download anything to the server that I am working on. Therefore I need a straight DOS batch solution.

Report •

Related Solutions

#4
August 27, 2010 at 03:00:17
Try this - not fully tested.

@echo off
cls
setlocal enabledelayedexpansion

pushd Path\to\.csv\files\||echo Pushd failed - job terminated&& exit /b

for /f "tokens=1-3* delims=-()" %%1 in ('dir /b *.csv') do (
    set oldfilename=%%1-%%2(%%3^)%%4
    set out=%%3&set out=!out:~0,8!
    set newfilename=%%2(!out!^)%%4
    echo Oldfilename=!oldfilename!
    echo Newfilename=!newfilename!
    pause>nul
    echo.&echo.
    rem ren !oldfilename! !newfilename!
    )
popd

Brief description - Pushd change default directory to the directory containing .csv files, if pushd fails the job will be terminated.. Create a bare directory listing of .csv filenames, remove unwanted sections, display old and new filenames for your approval in testing, rename files when the rem(ark) command is removed after testing. Popd return to original default directory.


Report •

#5
August 27, 2010 at 04:42:06
@wahine, i tested for you, it doesn't remove all the text between brackets
Oldfilename=0000070970302000-DAIOUTPUTHEADER_CODE_RTM(20100730_RTM_INITIAL1).csv
Newfilename=DAIOUTPUTHEADER_CODE_RTM(20100730).csv

GNU win32 packages | Gawk

Report •

#6
August 27, 2010 at 04:51:45
@OP, an alternative in vbscript

Set objFS = CreateObject("Scripting.FileSystemObject")
strFolder="c:\test"
Set objFolder = objFS.GetFolder(strFolder)
For Each strFile In objFolder.Files
	If objFS.GetExtensionName(strFile) = "csv" Then	   
		strFileName = strFile.Name
		If InStr(strFileName,")") > 0 Then		  
		  s= Split(strFileName,")")
		  For i=LBound(s) To UBound(s)
		    index=InStr(s(i),"(")
		    If index>0 Then
		      s(i)=Mid(s(i),1,index-1)
		    End If 
		  Next		  
		End If	  	   	 
		strFile.Name=Join(s,"") 
	End If	
Next 

save as myscript.vbs and on command line
c:\test> cscript //nologo myscript.vbs

GNU win32 packages | Gawk


Report •

#7
August 27, 2010 at 15:22:49
O.P. - I would like to execute a batch file to rename all the files in the directory

Ghostdog - @wahine, i tested for you, it doesn't remove all the text between brackets

Oldfilename=0000070970302000-DAIOUTPUTHEADER_CODE_RTM(20100730_RTM_INITIAL1).csv
Newfilename=DAIOUTPUTHEADER_CODE_RTM(20100730).csv

Yes, that was totally intentional. I read the OP's post several times and came to the conclusion that ALL input filenames had the same format and the same content, the only thing I could see which might differ was the date so I included that in the NewFilename so that the Ren command wouldn't try to rename all files with the same filename with the obvious results.

Perhaps the O.P. will comment.

Thanks


Report •

#8
August 28, 2010 at 03:54:39
Wahines solution helped me solve a problem that I have been working on all day. I am not too sure how it all works, but it does work.

I have various movie files from my media center. The files needed to be detected as movies instead of TV. The only distingishing factors were a M- at the start of a file, or the date in brackets after the title. I then needed to crop all the text aroudn the movie title so that cover art program could recognise the movie.

EG
Beetlejuice (1988)_GO!_2010_08_28_20_25_00.dvr-ms
M-The Rookie_7TWO_2010_08_27_14_24_40.dvr-ms

This looked after the 1st type of file

rename *)*.dvr-ms *).dvr-ms

This looked after the second type of file

for /f "tokens=1-3* delims=-_." %%1 in ('dir /b *.dvr-ms') do (
rename "%%1-%%2_%%3_%%4" "%%2.dvr-ms"
)

Now either type of file shows up as Almost Famous.dvr-ms

It took me a day to get there, and I would still be going without this site. So a big thankyou. I hope these solutions may help someone else.


Report •


Ask Question