Page 1 of 1
Cutting numbers off a file name
Posted: 23 Aug 2010 05:42
by avalancha
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
Re: Cutting numbers off a file name
Posted: 23 Aug 2010 11:17
by avery_larry
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"
)
)
Re: Cutting numbers off a file name
Posted: 23 Aug 2010 18:04
by ghostmachine4
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
Re: Cutting numbers off a file name
Posted: 24 Aug 2010 00:30
by avalancha
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
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
Re: Cutting numbers off a file name
Posted: 24 Aug 2010 01:48
by ghostmachine4
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.
Re: Cutting numbers off a file name
Posted: 24 Aug 2010 02:04
by avalancha
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
Re: Cutting numbers off a file name
Posted: 24 Aug 2010 02:34
by ghostmachine4
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
Re: Cutting numbers off a file name
Posted: 24 Aug 2010 02:56
by avalancha
I KNEW it'd be smarter to do it with VBS... Thanks a lot ghost, you saved my day
Re: Cutting numbers off a file name
Posted: 25 Aug 2010 10:27
by avery_larry
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.
Re: Cutting numbers off a file name
Posted: 25 Aug 2010 11:06
by orange_batch
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.
Re: Cutting numbers off a file name
Posted: 25 Aug 2010 19:37
by ghostmachine4
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.