renaming files using .bat file

Dell / Inpiron 1525
January 29, 2009 at 23:58:05
Specs: Windows Vista, 3 gb
Hi again.

I want to create a batch file that adds a suffix onto a file name: "_09wdd", such that the file would read: filename_09wdd.*

So I tried:

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

I tried various nuances of this recipe (like leaving off the .* in the code) but the suffix remains after the .* (filetype), so the file reads: filename.*_09wdd

Any ideas
sorry if I am being a twit
Thanks so much


See More: renaming files using .bat file

Report •


#1
January 30, 2009 at 00:31:28
for /f "tokens=* delims= " %%a in ('dir/b/a-d') do (
ren %%a %%~Na_09wdd%%~Xa
)


=====================================
If at first you don't succeed, you're about average.

M2


Report •

#2
January 30, 2009 at 01:06:48
Dear M2,

That works very well!
Thanks very much - it takes a while to learn the language.

I do have a follow up question - w.r.t. renaming files using batch files. (Can I post it here, or is it a new post?)

I would like to keep only the first two and in some case three digits of the original name and delete the rest of the name. The digits are always at the start of the file name.

So if the file name was:
10_bool_est_depth_0_152.rst
I would like to keep only the 10.rst

but if the files name is:
101_bool_est_depth_0_152.rst,
I would like to keep the 101.rst

So I need the code to recognise numbers, and limit it to the first three characters of the file name; if true, keep it, if false loose it.

Any suggestions
Thanks so much again


Report •

#3
January 30, 2009 at 02:02:02
Are the 2 or 3 numbers always followed by _?


=====================================
If at first you don't succeed, you're about average.

M2


Report •

Related Solutions

#4
January 30, 2009 at 02:22:50
Dear M2,

Yes the digits are always followed by a "_".
Does it not matter that the "_" reappears several times in the name though?

Thanks


Report •

#5
January 30, 2009 at 04:17:29
@echo off & setLocal EnableDelayedExpansion

for /f "tokens=* delims= " %%a in ('dir/b/a-d ^| find "_"') do (
for /f "tokens=1 delims=_" %%f in ("%%a") do (
ren %%a %%f%%~Xa
)
)


=====================================
If at first you don't succeed, you're about average.

M2


Report •

#6
January 30, 2009 at 05:27:29
Dear M2,

I have now run into a problem which I did not pick up on earlier - with the first question. When I tested it at first on 16 files or so it works fine - but at 100's of files it goes into a loop and I get the following:

kznfspakznfspakznfspakznfspakznfspakznfspakznfspakznfspakznfspakznfspakznfspakznfspakznfspakznfspakznfspa10_09wdd_09wdd_09wdd_09wdd_09wdd_09wdd_09wdd_09wdd_09wdd_09wdd_09wdd_09wdd_09wdd_09wdd_09wdd

Admittedly, I have tried to make the batch file add a prefix and suffix in one go - but I guess that should not make a difference.

So the code now looks as follows:

@echo off
pushd C:\GIS_shp_&_dbf_files\KZNSPLN_fsp_working\test
::for /f "tokens=* delims= " %%a in ('dir /b /a-d-h') do rename "kzfspa%%~Na_09wdd%%~Xa"
for %%a in (*) do rename "%%a" "kznfspa%%~Na_09wdd%%~Xa"
popd

Do you know how I can avoid this from happening?

I will test the code of the second question now.

Thanks so much


Report •

#7
January 30, 2009 at 05:45:29
What is this about?

rename "kzfspa%%~Na_09wdd%%~Xa"


=====================================
If at first you don't succeed, you're about average.

M2


Report •

#8
January 30, 2009 at 05:57:17
Dear M2,

I apologise - ignore all said in the previous reply, simply following what you suggested sorted it out!

for /f "tokens=* delims= " %%a in ('dir/b/a-d') do (
ren %%a kznfspa%%~Na_09wdd%%~Xa
)

And the second question on rendering only the digits also worked perfectly

Thanks ever so much


Report •


Ask Question