Adding a prefix to a multiple files located in a folder

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
rabantot
Posts: 5
Joined: 02 Sep 2009 17:35

Adding a prefix to a multiple files located in a folder

#1 Post by rabantot » 02 Sep 2009 17:46

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:

avery_larry
Expert
Posts: 391
Joined: 19 Mar 2009 08:47
Location: Iowa

#2 Post by avery_larry » 03 Sep 2009 13:20

*untested*

Code: Select all

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

rabantot
Posts: 5
Joined: 02 Sep 2009 17:35

Thanks a lot!!! :P

#3 Post by rabantot » 03 Sep 2009 17:42

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

avery_larry
Expert
Posts: 391
Joined: 19 Mar 2009 08:47
Location: Iowa

Re: Thanks a lot!!! :P

#4 Post by avery_larry » 03 Sep 2009 18:40

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]

rabantot
Posts: 5
Joined: 02 Sep 2009 17:35

Hi

#5 Post by rabantot » 03 Sep 2009 23:01

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

avery_larry
Expert
Posts: 391
Joined: 19 Mar 2009 08:47
Location: Iowa

#6 Post by avery_larry » 04 Sep 2009 08:56

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!"
)

Post Reply