Computing.Net > Forums > Programming > batch rename files with a part of..

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.

batch rename files with a part of..

Reply to Message Icon

Name: maxbre
Date: July 29, 2008 at 03:14:11 Pacific
OS: win xp
CPU/Ram: 3 gb
Comment:

Hi all

I worked out this rusty batch to rename a bunch of files resident in a specific directory called “orginali” in order keep just the part of the filename after the blank space (which happened to be of my interest).

::>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
@echo off
dir /b originali\*.txt > originali\file_list.tmp
pushd originali\
for /f "tokens=1* delims= " %%a in (file_list.tmp) do ren "%%a %%b" %%b >nul
del *.tmp
popd
::>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>

Files in question are typically on the form: XXXXX[blank space]zzzzzzz.txt

Now I need to generalise the batch because I would like to be able to rename the files with a given number (at choice) of characters of its proper filename before the file extension (independently of the type of delimters); keeping up on the above mentioned example I want to use, let’s say, the last 3 characters of the filename so that the final product (the new filename) would be like: zzz.txt

Any suggestion?

Second question, much more tricky (!?): is it possible to store the above mentioned string filename (zzz) in a variable and then add it to each row of a new column of the same file content?

Example of the original file content called XXXXX[blank space]zzzzzzz.txt

A B C D
a1 b1 c1 d1
a2 b2 c2 d2
a3 b3 c3 d3
…..

Example of the output file content called zzz.txt

NEW A B C D
zzz a1 b1 c1 d1
zzz a2 b2 c2 d2
zzz a3 b3 c3 d3
…..


Thank you for your help

max



Sponsored Link
Ads by Google

Response Number 1
Name: Mechanix2Go
Date: July 29, 2008 at 04:08:27 Pacific
Reply:

As to the first bit; you can nevermind making a tmp file. Just use DIR in FOR.

No need to popd.

::=================================
@echo off
setLocal EnableDelayedExpansion

pushd src

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


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

M2


0

Response Number 2
Name: maxbre
Date: July 29, 2008 at 06:28:33 Pacific
Reply:

m2

you are perfectly right, the core of the problem was sorted!

by the way I'm just wondering if I can conveniently make a slight variation to your code as to include a specific dir
(I already made something with pushd...)

now I'm testing:

Set Folder=originali

for /f "tokens=* delims= " %%a in ('dir "%Folder%\*.txt" /b/a-d') do (
....

but it seems not working so there must be still something I'm missing...

bye

m


0

Response Number 3
Name: maxbre
Date: July 29, 2008 at 06:35:04 Pacific
Reply:

m2
sorry this should have been intended as a follow up to my other post (your reply #3):
http://www.computing.net/answers/pr...

I definitely mismatched the post but in the end it seems quite consistent also in this one!

thanks again
max


0

Sponsored Link
Ads by Google
Reply to Message Icon

Related Posts

See More







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: batch rename files with a part of..

Batch Rename files in a folder www.computing.net/answers/programming/batch-rename-files-in-a-folder/15096.html

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

extracting a part of string in Batch File www.computing.net/answers/programming/extracting-a-part-of-string-in-batch-file/18991.html