Tom's Guide | Tom's Hardware | Tom's Games
![]() |
![]() |
![]() |
please i need help on making a batchfile for renaming files. On these case i need the 4-6 last characters of the original filename and using it prior to the characters that i need to use for the new filenames... hmmm... actually i am going to use the set of commands and add it to a bacth software that supports DOS commands... specifically MSBatch by Intergraph Microstation.... thanks for hearing me out ;)

Have a look at the set command - especially how you do character substitution and extraction from environment variables. That may help you. Alternatively code the lot in vbscript/javascript. There is more programmability in there than in dos batch.

Not much hope in DOS, but in w2k/XP:
::== L4.bat
@echo off
setLocal EnableDelayedExpansionfor /f "tokens=* delims= " %%a in ('dir /b/a-d') do (
set name=%%~na
set ext=%%~xa
set last4=!name:~-4!
echo ren %%a !last4!!ext!
)
=====================================
If at first you don't succeed, you're about average.M2

whew... :) thanks fo rthe tip guys... at least i have something to start with. I do not have that much knowledge on programming that is why i turned into DOS. But as you said it has its limits... im really confused on what to do since i have very limited time and knowledge... but i would really try,,, thanks guys... =)
mechanix2go... would this code run in win2k cmd window??? is win2k scripting different from dos batching??? thanks =)

"mechanix2go... would this code run in win2k cmd window??? is win2k scripting different from dos batching?"
Yes, it runs in w2k. And yes, w2k CMD has more capability than DOS. w2k will run most any DOS batch, but not vice-versa.
=====================================
If at first you don't succeed, you're about average.M2

thanks again for the experts advise :) now here are follow questions :)
in your given code the "setLocal EnableDelayedExpansion" expression, what is this for??? is it some kind of a header?? and where could i get codes like those that i could make use of on other tasks...
i am also confused with tokens and delims??? and whyu equate it with the "*"??? sorry for the lots of questions :) hope youll have the time to answer those... and also please help me understand how does your code extract the 4characters from the filename and insert it to the new filename.... thanks so mush i hope im not that annying yet i really want to understand i have read some books but books often expect the reader to have backgroud on programming but unfortunately im not...

You can get the built-in help with:
setlocal /?
set /?
for /?But it's anything but obvious. Best is do what you're doing; ask.
In the code I posted, this:
set name=%%~na
gets the name ONLY, no extension
this:
set ext=%%~xa
gets the extension
this:
set last4=!name:~-4!
gets the last 4 chars of the name
this:
"tokens=* delims= "
is standard layout. In this case I could have left it out; but I throw it in by habit.
=============================
It's never easy to figure out sombody else's code. I can't even understand my own two weeks after I wrote it.Two helpers here that I'm always learning from are 'dtech10' and 'IVO'.
=====================================
If at first you don't succeed, you're about average.M2

I need to rename some files to "prefix_yyyymmddhhmmss.dat". Based on some of the suggestions given, I created the batch file named chgdt.bat below:
****************************
for /f "tokens=2-4* delims=/ " %%a in ('DATE /T') do set THEDATE=%%c%%b%%a
For /F "tokens=2-4 delims=:" %%a in ('Command /C Echo. ^| Time ^| Find "current"') Do Set THETIME=%%a%%b%%c
Set THETIME=%THETIME:~1%
ren %1 %~n1%THEDATE%%THETIME%%~x1
****************************However, when I run the batch file "chgdt vv" where vv is the name of the file which I wanted to change, it gives me this error:
****************************
for /f "tokens=2-4* delims=/ " %a in ('DATE /T') do set THEDATE=%c%b%aset THEDATE=200703
For /F "tokens=2-4 delims=:" %a in ('Command /C Echo. ^| Time ^| Find "current"') Do Set THETIME=%a%b%c
Set THETIME= 01932.45
Set THETIME= 01932.45ren vv vv200703 01932.45
The syntax of the command is incorrect.
****************************Any advice? I am using Windows XP SP2. Thanks.

This probably needs it's own thread, but as long as we're here...
Looks like you put in some effort and got it pretty well hammered out. The usual GOTCHA applies: I can't test it because every version, date layout, region is different. But going by the error msg, it may be as simple as:
ren this that theother
has THREE parameters and a ren takes only TWO. So try modifying this:
ren %1 %~n1%THEDATE%%THETIME%%~x1
to this:
ren %1 "%~n1%THEDATE%%THETIME%%~x1"
=====================================
If at first you don't succeed, you're about average.M2

Thanks. I changed the line according to your suggestion and the error is gone. However, this batch file still does not append the day inside. When I run it, it changes the filename from vv to vv200703111223.25. The yyyy and mm are there but the dd is not. My regional settings are English (Australia), Time Format h:mm:ss tt and Short Date Format dd/MM/yyyy. By the way, how can I remove the ".25" from the string?
Thanks a lot!

You can either tinker with the substring or look here:
http://computing.net/programming/ww...
thanks to dtech10
=====================================
If at first you don't succeed, you're about average.M2

elow mechanic, thanks so much i think im getting a hang of this =) another thing is i try to follow the code but this line
<ren %%a !last4!!ext!>
doesnt seem to work... but one things is for sure... i might have a mistake... can you break down the steps please... how can this command rename all the files within a directory. or even batter make a copy of the file only with the new filename with it in the same directory perhaps... thanks =)

The code in #2 should produce an output like this:
================================
ren B6.BAT B6.BAT
ren B7.BAT B7.BAT
ren FTPTG.BAT TPTG.BAT
ren FTPFILES.BAT ILES.BAT
ren ftp2dir.bat 2dir.bat
======================Notice that the short named files are not effected. So if you did a COPY, you could have name collisions.
=====================================
If at first you don't succeed, you're about average.M2

ok sir, thans again after all the infos you have given me.. i have came up with this code..
*******************************************
::== L4.bat
@echo off
setLocal EnableDelayedExpansionfor /f "tokens=1 delims=" %%a in ('dir *.dgn /b/a-d') do (
set name=%%~na
set ext=%%~xa
set last4=!name:~-4!
msbatch cnvdgn inname:C:\pdf\test\dgn\test\!name!.dgn outname:c:\d_r340_!last4!.dgn
)
******************************************
now here is the problem... in the execution of the bat file the first loop is a success... conversion and renaming has been done but the proceeding loops have been denied... what i noticed is at the first loop the file name has been stored on the variable %%a and "last4" but on the second loop the preceeding filenames that should be processed has not been registered on the !name! variable... instead the code literally looks for the filename !name!.dgn that should only be a variable...thanks so much for the help again =)

I don't know what this is:
msbatch cnvdgn inname:C:\pdf\test\dgn\test\!name!.dgn outname:c:\d_r340_!last4!.dgn
=====================================
If at first you don't succeed, you're about average.M2

sir, actually this is an internal batch program for microstation...
masbatch cnvdg >--> command
inname:***.*** >--> filename to be converted
outname:***.*** >--> filename outputsyntax -->
msbatch cnvdgn inname:*.* outname:*.* <option>so this is where i got it...
on the intial loop it is successful but on the following run it cannot recognize the variable...the input name should be what is inside the variable !name! for the batch file to identify the filenames, but since it looks for the !name!.dgn it really wont find it...
i really dont know why in the second and preceeding loops the filename doesnt register the stored one since it already did on the first run...thanks.. =)

I'm lost. I think you need a new thread.
=====================================
If at first you don't succeed, you're about average.M2

I'm looking for something like this.
I want my batch to copy a directory and all its contents to another directory that the batch creates based on date and time.
basically I want to copy something like this
C:\Documents and Settings\user\somewhere\somehow\ to D:\backups\thedateandtime\somehow\
can someone help me out by writing something simple for me or pointing me in the right direction. I'm going to be using this file along with sched tasks to automatically backup the directory and all its contents to create an archive....

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

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