Page 1 of 1

Adding a prefix to a multiple files located in a folder

Posted: 02 Sep 2009 17:46
by rabantot
I need some help in order to resolve this:

In a folder I have multiple files and I need to add a prefix to all of them
For example,

cd c:\a (a=directory)
inside a directory let's suppose I have 3 files

file1.txt
file2.xls
files.ppt

Finally I need the files keep in the same directory, but with a prefix I defined previously, for instance "BC001_N-"

BC0001_N-file1.txt
BC0001_N-file2.xls
BC0001_N-files.ppt

I hope someone can help me to haddle this

Thanks in advance buddies

Regards,
rabanto :wink:

Posted: 03 Sep 2009 13:20
by avery_larry
*untested*

Code: Select all

cd c:\whatever folder
for %%a in (*) do ren "%%~a" "your_prefix_%%~a"

Thanks a lot!!! :P

Posted: 03 Sep 2009 17:42
by rabantot
avery_larry,

Thanks a lot for your help buddy. I tested it and works perfectly :-)
I have the last question about this:

Let's suppose I have in a folder the following files

-BC0001_N-file1.txt
-BC0001_N-file2.xls
-BC0001_N-files.ppt
-resume.doc
-salaries.xls

How could I do in order to extract the prefix 'BC001-N' for only the files that have the prefix??????

The result would be:
-file1.txt
-file2.xls
-files.ppt
-resume.doc
-salaries.xls


Thanks again for your help!!!

I am newbie at DOS batch and I really want to know more about it
How can I start to learn this???

Regards,
rabantot

Re: Thanks a lot!!! :P

Posted: 03 Sep 2009 18:40
by avery_larry
rabantot wrote:avery_larry,

Thanks a lot for your help buddy. I tested it and works perfectly :-)
I have the last question about this:

Let's suppose I have in a folder the following files

-BC0001_N-file1.txt
-BC0001_N-file2.xls
-BC0001_N-files.ppt
-resume.doc
-salaries.xls

How could I do in order to extract the prefix 'BC001-N' for only the files that have the prefix??????

The result would be:
-file1.txt
-file2.xls
-files.ppt
-resume.doc
-salaries.xls


Thanks again for your help!!!



*untested*

Code: Select all

@echo off
setlocal enabledelayedexpansion
set "prefix=-BC0001_N"
set prefix_length=9
set directory="c:\whatever you want"
::Note that you could write code to determine the length of the variable,
::but for this it's just easier to manually enter it.
for /f "delims=" %%a in ('dir /b "%directory%\%prefix%*') do (
   set "fname=%%~a"
   ren "%directory%\%%~a" "!fname:~%prefix_length%,999999!"
)


I am newbie at DOS batch and I really want to know more about it
How can I start to learn this???

Regards,
rabantot
Lots of stuff on this site to read and look at. www.ss64.com has good stuff also. [/code]

Hi

Posted: 03 Sep 2009 23:01
by rabantot
avery_larry,

I tested the batch and I am afraid is not working properly :-(

It might be a sintaxis problem in some line, for instance where u define the variable prefix???

@echo off
setlocal enabledelayedexpansion
set "prefix=-BC0001_N"
set prefix_length=9
set directory="c:\whatever you want"
::Note that you could write code to determine the length of the variable,
::but for this it's just easier to manually enter it.
for /f "delims=" %%a in ('dir /b "%directory%\%prefix%*') do (
set "fname=%%~a"
ren "%directory%\%%~a" "!fname:~%prefix_length%,999999!"
)

Could you please give me a hand with this again??

Thanks in advance

Regards,
rabantot

Posted: 04 Sep 2009 08:56
by avery_larry
Had a couple of incorrect double quotes. Here you go:


Code: Select all

@echo off 
setlocal enabledelayedexpansion
set "prefix=tes"
set "prefix_length=3"
set "directory=c:\tmp"
::Note that you could write code to determine the length of the variable,
::but for this it's just easier to manually enter it.
for /f "delims=" %%a in ('dir /a-d /b "%directory%\%prefix%*"') do (
   set "fname=%%~a"
   ren "%directory%\%%~a" "!fname:~%prefix_length%,999999!"
)