Tom's Guide | Tom's Hardware | Tom's Games
![]() |
![]() |
![]() |
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 helpmax

As to the first bit; you can nevermind making a tmp file. Just use DIR in FOR.
No need to popd.
::=================================
@echo off
setLocal EnableDelayedExpansionpushd 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

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

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

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

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