rename adding prefix to all files

Dell / Inpiron 1525
January 19, 2009 at 03:43:57
Specs: Windows Vista, 3 gb
Simple query - I hope

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

The folder address:

Inside are many files.



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

Thanks a million

See More: rename adding prefix to all files

Report •

January 19, 2009 at 05:07:01
can anyone tell me why this doesn't work?
I just can't figure it out, but it looks like it will help Fish.

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

Report •

January 19, 2009 at 05:33:17
fish&lee, for /d is to list directory names.
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.

Report •

January 19, 2009 at 14:20:11
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!

Report •

Related Solutions

January 19, 2009 at 23:48:52
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

Report •

January 20, 2009 at 01:21:11
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.

Report •

January 20, 2009 at 01:39:01
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:

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,

Report •

January 20, 2009 at 02:47:28
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?????

Report •

January 20, 2009 at 03:24:58
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.

Report •

January 20, 2009 at 04:03:12
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"

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.

Report •

January 20, 2009 at 05:30:40
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"

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

Report •

January 20, 2009 at 07:08:38
Great! Thanks Reno

Fish, any luck?

Report •

January 20, 2009 at 10:22:23
dir /a-d will include hidden/system files such as Desktop.ini if there is one. Use dir /a-d-h instead.

Report •

January 21, 2009 at 00:56:05
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

Report •

January 21, 2009 at 04:48:40
Glad it works now!

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

Report •

Ask Question