Computing.Net > Forums > Programming > renaming files using .bat file

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.

renaming files using .bat file

Reply to Message Icon

Name: fish
Date: January 29, 2009 at 23:58:05 Pacific
OS: Windows Vista
CPU/Ram: 3 gb
Product: Dell / Inpiron 1525
Subcategory: Batch
Comment:

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



Sponsored Link
Ads by Google

Response Number 1
Name: Mechanix2Go
Date: January 30, 2009 at 00:31:28 Pacific
Reply:

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


0

Response Number 2
Name: fish
Date: January 30, 2009 at 01:06:48 Pacific
Reply:

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


0

Response Number 3
Name: Mechanix2Go
Date: January 30, 2009 at 02:02:02 Pacific
Reply:

Are the 2 or 3 numbers always followed by _?


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

M2


0

Response Number 4
Name: fish
Date: January 30, 2009 at 02:22:50 Pacific
Reply:

Dear M2,

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

Thanks


0

Response Number 5
Name: Mechanix2Go
Date: January 30, 2009 at 04:17:29 Pacific
Reply:

@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


0

Related Posts

See More



Response Number 6
Name: fish
Date: January 30, 2009 at 05:27:29 Pacific
Reply:

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


0

Response Number 7
Name: Mechanix2Go
Date: January 30, 2009 at 05:45:29 Pacific
Reply:

What is this about?

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


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

M2


0

Response Number 8
Name: fish
Date: January 30, 2009 at 05:57:17 Pacific
Reply:

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


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: renaming files using .bat file

Using bat file to open another fil www.computing.net/answers/programming/using-bat-file-to-open-another-fil/18641.html

Renaming files using a batch www.computing.net/answers/programming/renaming-files-using-a-batch/11819.html

consecutive file rename www.computing.net/answers/programming/consecutive-file-rename/16742.html