Computing.Net > Forums > Programming > rename adding prefix to all files

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

rename adding prefix to all files

Reply to Message Icon

Name: fish
Date: January 19, 2009 at 03:43:57 Pacific
OS: Windows Vista
CPU/Ram: 3 gb
Product: Dell / Inpiron 1525
Subcategory: Batch
Comment:

Simple query - I hope

I would like to add a prefix to all files in the same folder. e.g.

The folder address:
C:\GIS_shp_&_dbf_files\KZNSPLN_working\Processses\faa\kzn

Inside are many files.

e.g.

faa10_09wdd.rst
faa10_09wdd.ird
faa27_09wdd.rst
faa27_09wdd.ird

I would like add the prefix 'kzn' to all the files inside the folder.

Thanks a million



Sponsored Link
Ads by Google

Response Number 1
Name: lee123abc
Date: January 19, 2009 at 05:07:01 Pacific
Reply:

can anyone tell me why this doesn't work?
I just can't figure it out, but it looks like it will help Fish.
Cheers

setlocal enabledelayedexpansion
for /d %%j in (C:\GIS_shp_&_dbf_files\KZNSPLN_working\Processses\faa\kzn\) do (
rename %%j kzn%%j
)


0

Response Number 2
Name: reno
Date: January 19, 2009 at 05:33:17 Pacific
Reply:

fish&lee, for /d is to list directory names.
eg.
for /d %a in (*) do @echo "%a" is directory.

to list file(s):
for %a in (*) do @echo "%a" is a file

type the above in command prompt. if you make it into batch file, use double percentage (%%) instead of single %.
i let you figure out the rest =)

type for /?, a lot of technique can be learn from the help file.


0

Response Number 3
Name: lee123abc
Date: January 19, 2009 at 14:20:11 Pacific
Reply:

Guys, please check the following code. Reno, I tried this and it worked a charm on my pc. Thanks for not giving away the code, I enjoyed trying to figure it out.

setlocal enabledelayedexpansion
for %%j in (*) do (
set filename=%%j
rename !filename! kzn!filename!
)


0

Response Number 4
Name: fish
Date: January 19, 2009 at 23:48:52 Pacific
Reply:

Hi Lee

some questions on the code that you gave last:

setlocal enabledelayedexpansion
for %%j in (*) do (
set filename=%%j
rename !filename! kzn!filename!
)

Would this not add a prefix to all files?
In the first code you queried:

setlocal enabledelayedexpansion
for /d %%j in (C:\GIS_shp_&_dbf_files\KZNSPLN_working\Processses\faa\kzn\) do (
rename %%j kzn%%j
)

Does the part: in (C:\GIS_shp_&_dbf_files\KZNSPLN_working\Processses\faa\kzn\)
specify that you want the renaming only to apply to the files that folder?

As I ran the code in CMD as C:rename_filename
It recognised the batch file, but then responded: access denied.

Thanks so much for your time



0

Response Number 5
Name: lee123abc
Date: January 20, 2009 at 01:21:11 Pacific
Reply:

Hi Fish, I am sorry about the first code, it was a mistake. The second code uses '(*)' which actually means everything inside that particular folder.

About running the code, did you try putting it into a bat file and running it from within the 'kzn' folder? You see, I wrote that code specifically for that task. Once it is in a bat file, and in the 'kzn' folder, just double click the file and it should run itself! If you put it straight into your c drive then it willl try rename all files on your c: root.

If I havent answered your question properly, please let me know.
Cheers


0

Related Posts

See More



Response Number 6
Name: fish
Date: January 20, 2009 at 01:39:01 Pacific
Reply:

Hi Lee,

Thanks so much for the last email. It cleared things up a bit.

It worked - but a bit too good.haha

This was my result:
kznkznkznkznkznkznkznkznkznkznkznkznkznkznkznkznkznkznkznkznkznkznkznkznkznkznkznkznkznkznkznkznkznkznkznkznkznkznkznkznkznkznkznkznkznkznkznkznkznkznkznkznkznkznkznkznkznkznkznkznkznfaa116_09wdd

It copied the 'kzn' part 61 times

I thought it had something to do with the amount of files....but then there were only 46 files in the folder...

Any advice,
thanks


0

Response Number 7
Name: lee123abc
Date: January 20, 2009 at 02:47:28 Pacific
Reply:

Oh god, I am sorry. It worked on my pc, I will have to stand down here. I appologise again. Don't know why that happened.

Anyone got any ideas?????


0

Response Number 8
Name: reno
Date: January 20, 2009 at 03:24:58 Pacific
Reply:

geez, lee&fish, sorry for misleading info. it seems that the for loop read over and over again on renamed file.

use the for /f loop & dir combo:

for /f "tokens=*" %a in ('dir /b /a-d c:\yourfolderhere\*.*') do @echo "%a" is a file

for file name with space, dont forget to surround it with quote.


0

Response Number 9
Name: lee123abc
Date: January 20, 2009 at 04:03:12 Pacific
Reply:

Hi Reno, thanks for replying.

I tried what you said... here is what I wrote

setlocal enabledelayedexpansion
for /f "tokens=*" %%a in ('dir /b /a-d C:\GIS_shp_&_dbf_files\KZNSPLN_working\Processses\faa\kzn\*.*') do (
rename "C:\GIS_shp_&_dbf_files\KZNSPLN_working\Processses\faa\kzn\%%a" "kzn%%a"
pause
)

It doesn't work with '_' in the folder path.
It works when I took them out, however Fish need working code for this. Could you just write exactly what he needs?

Fish, sorry again.


0

Response Number 10
Name: reno
Date: January 20, 2009 at 05:30:40 Pacific
Reply:

sorry, this is tougher than i thought. i am also having trouble making it works. folder name containing special character does make a different.

for /f "tokens=*" %%a in ('dir /b /a-d "C:\GIS_shp_&_dbf_files\KZNSPLN_working\Processses\faa\kzn\"') do (
rename "C:\GIS_shp_&_dbf_files\KZNSPLN_working\Processses\faa\kzn\%%a" "kzn%%a"
)


or another way, can also do like this:
- using pushd & for /f loop
- using pushd & for in (*) loop

@echo off
pushd C:\GIS_shp_^&_dbf_files\KZNSPLN_working\Processses\faa\kzn\
::for /f "tokens=*" %%a in ('dir /b /a-d') do rename "%%a" "kzn%%a"
for %%a in (*) do rename "%%a" "kzn%%a"
popd

the above code is tested on xp with as example folder name.


0

Response Number 11
Name: lee123abc
Date: January 20, 2009 at 07:08:38 Pacific
Reply:

Great! Thanks Reno

Fish, any luck?


0

Response Number 12
Name: klint
Date: January 20, 2009 at 10:22:23 Pacific
Reply:

dir /a-d will include hidden/system files such as Desktop.ini if there is one. Use dir /a-d-h instead.


0

Response Number 13
Name: fish
Date: January 21, 2009 at 00:56:05 Pacific
Reply:

Thanks so much Lee, Reno for all your time and effort. And to Klint for the important note on the code.

The result is: It works like a charm!
all files have now been correctly labelled.

-----------------------
A side note to you guys - I have also posted a question regarding a Visual Basic application - a macro that I want to run in Excel. So far I have had no response - do you know to whom I should address the questions?
-----------------------

Thanks again so much
Fish


0

Response Number 14
Name: lee123abc
Date: January 21, 2009 at 04:48:40 Pacific
Reply:

Glad it works now!

Funny thing is the first thing you said "Simple query - I hope"!!


0

Sponsored Link
Ads by Google
Reply to Message Icon






Post Locked

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.


Go to Programming Forum Home


Sponsored links

Ads by Google


Results for: rename adding prefix to all files

Adding date to all files ina folder www.computing.net/answers/programming/adding-date-to-all-files-ina-folder/16701.html

File rename in CMD or Bat script www.computing.net/answers/programming/file-rename-in-cmd-or-bat-script/14672.html

adding keywords to jpg files? www.computing.net/answers/programming/adding-keywords-to-jpg-files/14591.html