Cutting numbers off a file name

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
avalancha
Posts: 8
Joined: 25 Mar 2010 03:57
Location: Stuttgart - Germany

Cutting numbers off a file name

#1 Post by avalancha » 23 Aug 2010 05:42

Hey everyone,

I need a batch-file that modifies some file names for me. The files all look like this:
abcd12345.csv
and I have to get rid of the numbers. It's also always the same amount of numbers that I have to cut off.
So far I found this:

Code: Select all

for %%i in (D:\"Documents and Settings"\....\test\*.csv) do @(
for /f "delims=1234567890" %%a in ("%%~ni") do @(
ren "%%i" "%%a%%~xi"
))

and it works... kind of... it does the cutting, but only on one file. So something must be wrong with the for-loop
The echo prints out: "A duplicate file name exists or the file cannot be found"

Anyone see my mistake?

Best regards,
Patrick

avery_larry
Expert
Posts: 391
Joined: 19 Mar 2009 08:47
Location: Iowa

Re: Cutting numbers off a file name

#2 Post by avery_larry » 23 Aug 2010 11:17

First, get rid of the "@"'s and turn echo on and see what it's trying to do.

Second, I'm not sure that the first for loop works with embedded double quotes. (But I'm not sure -- I've never tried the way you have it.)

I would do:
*untested*

Code: Select all

cd /d "d:\documents and settings\.....\test"
for /f "delims=" %%i in ('dir /b *.csv') do (
   for /f "tokens=* delims=0123456789" %%a in ("%%~ni") do (
      ren "%%~i" "%%~a%%~xi"
   )
)

ghostmachine4
Posts: 319
Joined: 12 May 2006 01:13

Re: Cutting numbers off a file name

#3 Post by ghostmachine4 » 23 Aug 2010 18:04

avalancha wrote:Hey everyone,

I need a batch-file that modifies some file names for me. The files all look like this:
abcd12345.csv
and I have to get rid of the numbers. It's also always the same amount of numbers that I have to cut off.
So far I found this:


but do you have other numbers besides the 5, eg a1cd12345.csv

avalancha
Posts: 8
Joined: 25 Mar 2010 03:57
Location: Stuttgart - Germany

Re: Cutting numbers off a file name

#4 Post by avalancha » 24 Aug 2010 00:30

avery_larry wrote:First, get rid of the "@"'s and turn echo on and see what it's trying to do.

Second, I'm not sure that the first for loop works with embedded double quotes. (But I'm not sure -- I've never tried the way you have it.)

I would do:
*untested*

Code: Select all

cd /d "d:\documents and settings\.....\test"
for /f "delims=" %%i in ('dir /b *.csv') do (
   for /f "tokens=* delims=0123456789" %%a in ("%%~ni") do (
      ren "%%~i" "%%~a%%~xi"
   )
)

naaaah we are somehow close... it runs and gives me the following output:

Code: Select all

D:\Documents and Settings\.......\test>(for /F "tokens=* delims=0123456789" %
a in ("test12345") do (ren "test12345.csv" "%~a.csv ) )

D:\Documents and Settings\.......\test>(ren "test12345.csv" "test12345.csv" )

So the variables are read in correctly... but it does not cut it off :roll:
the files stay untouched


ghostmachine4 wrote:but do you have other numbers besides the 5, eg a1cd12345.csv

Well yes, there sometimes is a number within the name of the document, the names differ, but it's always the same ending: 5 random digits that I need to put away

ghostmachine4
Posts: 319
Joined: 12 May 2006 01:13

Re: Cutting numbers off a file name

#5 Post by ghostmachine4 » 24 Aug 2010 01:48

download sed for windows, then you can do this

Code: Select all

C:\test>dir /B *csv
a10bc12345.csv
abc12345.csv

C:\test>dir /B *csv | sed -r "s/[0-9]{5}\.csv/.csv/"
a10bc.csv
abc.csv


use a for loop if desired. Note, this will not affect the digits mixed together with the alphabets.

avalancha
Posts: 8
Joined: 25 Mar 2010 03:57
Location: Stuttgart - Germany

Re: Cutting numbers off a file name

#6 Post by avalancha » 24 Aug 2010 02:04

ghostmachine4 wrote:download sed for windows, then you can do this

Code: Select all

C:\test>dir /B *csv
a10bc12345.csv
abc12345.csv

C:\test>dir /B *csv | sed -r "s/[0-9]{5}\.csv/.csv/"
a10bc.csv
abc.csv

use a for loop if desired. Note, this will not affect the digits mixed together with the alphabets.


Damn, looks nice, but I can't use this, I'm sorry. See I have to implement all this on a company computer system where I'm not able to add additional programs e.g.
I have to get this done with what is provided by Windows (XP) as it is...

Thx a lot so far

ghostmachine4
Posts: 319
Joined: 12 May 2006 01:13

Re: Cutting numbers off a file name

#7 Post by ghostmachine4 » 24 Aug 2010 02:34

Note you only have to download sed once (from another comp) and use it everywhere. ITs only a small .exe file. Otherwise, another native alternative is vbscript

Code: Select all

Set objFS = CreateObject("Scripting.FileSystemObject")
strFolder="c:\test"
Set objFolder = objFS.GetFolder(strFolder)
Set objRE = New RegExp
objRE.Global     = True
objRE.IgnoreCase = False
objRE.Pattern = "\d{5}.csv"
For Each strFile In objFolder.Files
   If objFS.GetExtensionName(strFile) = "csv" Then
      Set Matches = objRE.Execute(strFile.Name)
      For Each Match in Matches   ' Iterate Matches collection.           
           strNewFile=objRE.Replace(strFile.Name,".csv")   
           strFile.Name=strNewFile 'this is to rename the file
      Next                  
   End If   
Next

save the above as myrename.vbs , and on the command line, run it as

Code: Select all

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

avalancha
Posts: 8
Joined: 25 Mar 2010 03:57
Location: Stuttgart - Germany

Re: Cutting numbers off a file name

#8 Post by avalancha » 24 Aug 2010 02:56

I KNEW it'd be smarter to do it with VBS... Thanks a lot ghost, you saved my day :D

avery_larry
Expert
Posts: 391
Joined: 19 Mar 2009 08:47
Location: Iowa

Re: Cutting numbers off a file name

#9 Post by avery_larry » 25 Aug 2010 10:27

Oops -- I messed up the tokens part. You'd have to specify a max. number of tokens for it to work. In this case, I think 2 would work:

*untested*

Code: Select all

cd /d "d:\documents and settings\.....\test"
for /f "delims=" %%i in ('dir /b *.csv') do (
   for /f "tokens=1,2 delims=0123456789" %%a in ('%%~ni") do (
      ren "%%~i" "%%~a%%~b%%~xi"
   )
)


For an unspecified number of tokens, you'd have to do a call/goto loop or a recursive call to the for loop, stripping 1 token off each time until you process all of them.

orange_batch
Expert
Posts: 442
Joined: 01 Aug 2010 17:13
Location: Canadian Pacific
Contact:

Re: Cutting numbers off a file name

#10 Post by orange_batch » 25 Aug 2010 11:06

Jeez guys, help him out with the obvious solution.

If every file ends with 5 numbers, all you have to do is:

Code: Select all

setlocal enabledelayedexpansion

for /r "C:\...\My Folder" %%x in (*.csv) do (
set "truncatedname=%%~nx"
set "truncatedname=!truncatedname:~0,-5!"
ren "%%x" !truncatedname!.csv
)
That scans the target folder and all subfolders for *.csv. Change C:\...\My Folder to your target folder to scan, obviously.

If you don't want to include subfolders, replace the "for /r ..." line with this:

Code: Select all

for %%x in ("C:\...\My Folder\*.csv") do (
Again, change C:\...\My Folder to your target folder to scan.

ghostmachine4
Posts: 319
Joined: 12 May 2006 01:13

Re: Cutting numbers off a file name

#11 Post by ghostmachine4 » 25 Aug 2010 19:37

If EVERY file ends with 5 numbers in that particular folder, that should be fine. But if not, it will truncate every last 5 characters for files that doesn't need to change. It will be good if there is a wildcard (regex) search on 5 numbers before truncating.

Post Reply