1

I know this has been covered many times, but i still can't seem to get a good solution to this particular aspect. I have the same problem in both bash and DOS. I have a file of many .csv files named abcYYMMDDf.csv and I want to change the file name to the YYMMDD part which corresponds to the date it was created. Note the "f" after the YYMMDD, so really i would like an equivalent of excel's mid() to take the middle date.

In bash I understand the rename function, and the * wildcard I tried variations on

rename abc*f.csv *.csv abc*

thinking that the * would stand in for YYMMDD but obviously it didn't. I saw another method involving selecting s/([a-z]*) but that would just take the whole file name surely?

In DOS I tried

ren abc*f.csv *.csv

4 Answers4

2

Bash parameter expansion can be used to strip the 'abc' and the 'f' in two steps, using prefix removal and pattern substitution, respectively.

for f in abc*f.csv; do
    new_f=${f#abc}
    new_f=${new_f/%f.csv/.csv}
    mv $f $new_f
done

You could use sed in place of parameter expansion to remove 'abc' and 'f' in one step:

for f in abc*f.csv; do
    new_f=$(echo $f | sed 's/abc\(.*\)f/\1/')
    mv $f $new_f
done

I cannot answer the DOS version; you may be better off asking that in a separate question.

chepner
  • 497,756
  • 71
  • 530
  • 681
  • Excellent, thanks very much. I used a combination of the rename for removing the abc then the first part of your answer for the f. I tried the second loop first time around and it kept giving me an error message about there being no output file, is there something i didn't specify within that loop which would return that message? – stackmeistergeneral Jul 31 '12 at 13:04
  • As usual for me, I made a mistake in the sed call. Fixed. – chepner Jul 31 '12 at 13:12
2

For the "DOS" part of the question: Presuming you are on Windows using a command prompt (cmd.exe), and not on true DOS using command.com

@echo off
setlocal enableDelayedExpansion
for %%F in (abc*f.csv) do (
  set "name=%%F"
  ren "%%F" "!name:~1,6!.csv"
)


Update

The rules for how REN works with wildcards can be found at How does the Windows RENAME command interpret wildcards?. Unfortunately, the rules don't help in this situation.

Community
  • 1
  • 1
dbenham
  • 127,446
  • 28
  • 251
  • 390
0

For the bash part (with two steps):

rename abc "" abc*
rename f.csv .csv *f.csv

should work, since you want to substitute the "abc" part with the empty string (i.e., delete the abc part). With rename you have to specify only the part you would like to replace, not the whole filename (see the man page of rename for details).

The second step takes care of removing the trailing 'f'.

For the DOS part:

I don't think you'll be able to solve that with rename only. It might be worthwhile to look at some Powershell commands...

  • OK, that sounds straightforward, but in this case there is also an f right after the YYMMDD i was hoping to remove that at the same time. Also, do i not need to specify the file extension with these commands? – stackmeistergeneral Jul 31 '12 at 10:17
  • No, you don't need to specify the file extensions: File extensions have no inherent meaning under linux like systems (as far as I know). Of course you can use the trailing extensions for filtering via glob patterns (as done in the last command line parameter *f.csv in the second rename invocation). – Valentin Huber Jul 31 '12 at 15:05
0

I have a solution for DOS, You can create the strings you need in excel then copy and paste them into the command line. It will automatically execute every line that you copied into the command line.

REN "folderpath\filename" "newName"

REN "C:\Users\Charlie\Desktop\_Working General\L2795187.HH293.11.xml.csv" "HH293-11.csv"

I created the above in excel with formulas and just pasted it into cmd. A cell for every single file I want to rename in column A and it worked perfectly.

drunkenvash
  • 37
  • 11