Reputation: 117
Here's an example script with enabledelayedexpansion for renaming files:
How to write a batch file to rename files in numerically order within a particular folder?
How to retain the original file order though? I need a possible solution for Bash and for Batch (DOS/Windows).
Edit: OK - I think a specific example of my situation for batch is necessary to understand the sorting issue.
What I have are .cab files in a folder, like this:
"1_1.cab" - "1_120.cab"
I try to understand how to rename the files for the result:
"1.cab" - "120.cab"
In theory there must be a way to take care about all "1_" while renaming to achieve the original order.
possible solution for batch in this specific case:
for /F "tokens=1,2 delims=_" %%G in ('dir /b /A:-D *.cab') do rename %%G_%%H %%H
Upvotes: 0
Views: 784
Reputation: 6061
After the OP edited its question saying he only wants to strip the file names from their prefix 1_
, I suggest a new solution:
for file in *; do mv "$file" "${file#1_}"; done
If the prefix can be any number followed by an underscore, it is possible to adapt this latter solution.
Upvotes: 0
Reputation: 6061
As far as I understand your question, I suggest you to try:
for file in *; do ((i++)); mv "$file" "$i.${file##*.}"; done
This will rename all the not hidden files of the current directory to files having as name a number followed by the same suffixe as the original file. The numbers will respect the existing order of the files.
Also be sure you have no file having a number as basename: you risk to erase it.
If you are not sure of the result, change mv
into echo mv
for seing what is going to happen before you actually do it.
Upvotes: 1