How to remove 1st n char of filenames

October 15, 2010 at 04:14:52
Specs: Windows XP
I'm having around 150 files in one of my folder and I want to remove the first 2 characters for all file names... pls. help me out...

See More: How to remove 1st n char of filenames

Report •

#1
October 15, 2010 at 04:25:07
@echo off & setLocal EnableDELAYedeXpansion

for /f "tokens=* delims= " %%a in ('dir/b/a-d') do (
set F=%%a
set F=!F:~2!
ren "%%a" "!F!"
)


=====================================
Life is too important to be taken seriously.

M2


Report •

#2
October 15, 2010 at 21:10:39

download gawk for windows then use this one liner

C:\test>dir /B /A-D * | gawk "{o=substr($0,3); cmd=\"ren \042\"$0\"\042 \042\"o\"\042\";system(cmd)}"

GNU win32 packages | Gawk


Report •

#3
October 17, 2010 at 01:29:36
ThanX alot for the useful reply M2... The code is working fine...But what to do if the file name begins with a space or any spl. char??

Report •

Related Solutions

#4
October 17, 2010 at 02:24:41
All bets are off.


=====================================
Life is too important to be taken seriously.

M2


Report •

#5
October 17, 2010 at 03:38:37
>>But what to do if the file name begins with a space or any spl. char??

what if they begin with spaces or special chars? Do you want to trim them off, then remove the next 2 chars? if yes, just add another function to the code i post.

dir /B /A-D * | gawk "{gsub(/^[ \t;!@#$%^]/,""); o=substr($0,3) ...............  }"

Use a good text/string processing tool to do the job.

GNU win32 packages | Gawk


Report •

#6
October 17, 2010 at 03:58:59
Live long and prosper.

And avoid filenames with magical mystery chars.

LOL


=====================================
Life is too important to be taken seriously.

M2


Report •

#7
October 17, 2010 at 04:07:13
But seriously, folks...[as Joe Walsh would say]

If you want to skip a leading space and the next 2, use the script I posted.

If you want a leading space to count as 1 of 2, try this:

============================================

@echo off & setLocal EnableDELAYedeXpansion

for /f "tokens=* delims=" %%a in ('dir/b/a-d') do (
set F=%%a
set F=!F:~2!
echo ren "%%a" "!F!"
)


=====================================
Life is too important to be taken seriously.

M2


Report •

#8
October 17, 2010 at 04:47:21
If you absolutely hate downloading stuff, you can use vbscript, it has at least better string manipulation capabilities than cmd.exe

strFolder= WScript.Arguments(0)
Set objRE = New RegExp
objRE.IgnoreCase = False
objRE.Pattern = "^[ \t~!@#$%^&*()]*"
objRE.Global=True
Set objFS = CreateObject( "Scripting.FileSystemObject" )
Set objFolder = objFS.GetFolder(strFolder)
For Each strFile In objFolder.Files	
    	strNewFile=objRE.Replace(strFile.Name,"")  
    	WScript.Echo strNewFile
    	'strFile.Name = strNewFile  'uncomment to use    
Next

usage:

C:\test>cscript //nologo myrename.vbs c:\folder_to_process

GNU win32 packages | Gawk


Report •

Ask Question