0

I have files in folder named unix_aaa.txt unix_bbb.txt unix_ccc.txt and so on.

How do I change all the files that start with unix to names like

001_unix_aaa.txt

002_unix_bbb.txt

003_unix_ccc.txt
4
  • 1
    is it batch or bash?
    – anishsane
    Dec 21, 2012 at 6:50
  • is it not the same i dont know both are fine i think > <
    – eathapeking
    Dec 21, 2012 at 7:10
  • 2
    batch=>windows, bash=>linux. The script answer would change completely.
    – anishsane
    Dec 21, 2012 at 8:32
  • ahhh it's batch :)
    – eathapeking
    Dec 21, 2012 at 9:30

2 Answers 2

1

Here is a batch (windows) script that will do this

setlocal enabledelayedexpansion
set n=1
for %%a in (unix_*.txt) do (
if !n! lss 10 (
ren "%%a" "00!n!_%%a"
) else (
if !n! lss 100 (
ren "%%a" "0!n!_%%a"
) else (
if !n! geq 100 (
ren "%%a" "!n!_%%a"
)
)
)
set /a n+=1
)
8
  • can i ask more output came out as 001 - 010 then it is 010 instead of 011 how should i fix it or there is no way
    – eathapeking
    Dec 21, 2012 at 9:38
  • 1
    What do you mean? I don't follow :)
    – Bali C
    Dec 21, 2012 at 9:54
  • :D meanthat 001_unix_aaa.txt until 009_unix_aaa.txt then at 010 it comeout like 0010_unix_aaa.txt it should be 010_unix_aaa.txt i'm try to explain as much as my english can >_<
    – eathapeking
    Dec 21, 2012 at 10:00
  • 1
    Oh right, I get you now :D It should already do that, I made it check so it would go like 001 010 100 etc I will do some testing!
    – Bali C
    Dec 21, 2012 at 10:02
  • hey hey i got it now thank you becuase of i didnt type it all correct that why its error thank You :D :D :D :D :D
    – eathapeking
    Dec 21, 2012 at 10:03
1

This is for Bourne shells.

n=1; for f in unix*
do
    mv "$f" "`printf %03d $n`_$f"
    n=$((n+1))
done
5
  • it's not working i put in text document then change it .bat everyfile name stil be the same but thank you i found the problem what is it printf in cmd command
    – eathapeking
    Dec 21, 2012 at 7:13
  • just normal command line i dont know much about it all i used to do is type in .txt file then change it .bat and run
    – eathapeking
    Dec 21, 2012 at 7:19
  • @eathapeking what does echo $0 say ?
    – kmkaplan
    Dec 21, 2012 at 7:30
  • nothing change :) here example of filename unixinv_RSSPA_16Nov2012.csv
    – eathapeking
    Dec 21, 2012 at 7:40
  • 2
    From 1st comment, looks like it's batch & not bash
    – anishsane
    Dec 21, 2012 at 8:40

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .