Tom's Guide | Tom's Hardware | Tom's Games
![]() |
![]() |
![]() |
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.*"
popdI 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

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

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.rstbut if the files name is:
101_bool_est_depth_0_152.rst,
I would like to keep the 101.rstSo 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

Are the 2 or 3 numbers always followed by _?
=====================================
If at first you don't succeed, you're about average.M2

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

@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

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"
popdDo you know how I can avoid this from happening?
I will test the code of the second question now.
Thanks so much

What is this about?
rename "kzfspa%%~Na_09wdd%%~Xa"
=====================================
If at first you don't succeed, you're about average.M2

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

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

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