Tom's Guide | Tom's Hardware | Tom's Games
![]() |
![]() |
![]() |
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\kznInside are many files.
e.g.
faa10_09wdd.rst
faa10_09wdd.ird
faa27_09wdd.rst
faa27_09wdd.irdI would like add the prefix 'kzn' to all the files inside the folder.
Thanks a million

can anyone tell me why this doesn't work?
I just can't figure it out, but it looks like it will help Fish.
Cheerssetlocal enabledelayedexpansion
for /d %%j in (C:\GIS_shp_&_dbf_files\KZNSPLN_working\Processses\faa\kzn\) do (
rename %%j kzn%%j
)

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 filetype 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.

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

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

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

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_09wddIt 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

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?????

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 filefor file name with space, dont forget to surround it with quote.

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.

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" popdthe above code is tested on xp with as example folder name.

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

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

![]() |
![]() |
![]() |

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