Computing.Net > Forums > Programming > help on creating DOS Batch file

help on creating DOS Batch file

Reply to Message Icon

Original Message
Name: batoushinden
Date: March 17, 2007 at 07:45:08 Pacific
Subject: help on creating DOS Batch file
OS: Win XP
CPU/Ram: P4/256
Model/Manufacturer: INTEL
Comment:

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 ;)


Report Offensive Message For Removal

Response Number 1
Name: cup
Date: March 17, 2007 at 10:21:17 Pacific
Subject: help on creating DOS Batch file
Reply: (edit)

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.


Report Offensive Follow Up For Removal

Response Number 2
Name: Mechanix2Go
Date: March 17, 2007 at 16:01:27 Pacific
Subject: help on creating DOS Batch file
Reply: (edit)

Not much hope in DOS, but in w2k/XP:

::== L4.bat
@echo off
setLocal EnableDelayedExpansion

for /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



Report Offensive Follow Up For Removal

Response Number 3
Name: batoushinden
Date: March 20, 2007 at 04:53:08 Pacific
Subject: help on creating DOS Batch file
Reply: (edit)

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 =)


Report Offensive Follow Up For Removal

Response Number 4
Name: Mechanix2Go
Date: March 20, 2007 at 05:14:21 Pacific
Subject: help on creating DOS Batch file
Reply: (edit)

"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



Report Offensive Follow Up For Removal

Response Number 5
Name: batoushinden
Date: March 21, 2007 at 05:48:41 Pacific
Subject: help on creating DOS Batch file
Reply: (edit)

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...


Report Offensive Follow Up For Removal


Response Number 6
Name: Mechanix2Go
Date: March 21, 2007 at 07:58:46 Pacific
Subject: help on creating DOS Batch file
Reply: (edit)

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



Report Offensive Follow Up For Removal

Response Number 7
Name: rick1978
Date: March 21, 2007 at 09:32:54 Pacific
Subject: help on creating DOS Batch file
Reply: (edit)

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%a

set 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.45

ren vv vv200703 01932.45
The syntax of the command is incorrect.
****************************

Any advice? I am using Windows XP SP2. Thanks.


Report Offensive Follow Up For Removal

Response Number 8
Name: Mechanix2Go
Date: March 21, 2007 at 10:16:04 Pacific
Subject: help on creating DOS Batch file
Reply: (edit)

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



Report Offensive Follow Up For Removal

Response Number 9
Name: rick1978
Date: March 21, 2007 at 20:18:28 Pacific
Subject: help on creating DOS Batch file
Reply: (edit)

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!


Report Offensive Follow Up For Removal

Response Number 10
Name: Mechanix2Go
Date: March 21, 2007 at 23:04:51 Pacific
Subject: help on creating DOS Batch file
Reply: (edit)

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



Report Offensive Follow Up For Removal

Response Number 11
Name: batoushinden
Date: March 22, 2007 at 05:01:10 Pacific
Subject: help on creating DOS Batch file
Reply: (edit)

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 =)



Report Offensive Follow Up For Removal

Response Number 12
Name: Mechanix2Go
Date: March 23, 2007 at 04:56:52 Pacific
Subject: help on creating DOS Batch file
Reply: (edit)

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



Report Offensive Follow Up For Removal

Response Number 13
Name: batoushinden
Date: March 23, 2007 at 22:50:28 Pacific
Subject: help on creating DOS Batch file
Reply: (edit)

ok sir, thans again after all the infos you have given me.. i have came up with this code..
*******************************************
::== L4.bat
@echo off
setLocal EnableDelayedExpansion

for /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 =)


Report Offensive Follow Up For Removal

Response Number 14
Name: Mechanix2Go
Date: March 24, 2007 at 03:09:02 Pacific
Subject: help on creating DOS Batch file
Reply: (edit)

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



Report Offensive Follow Up For Removal

Response Number 15
Name: batoushinden
Date: March 24, 2007 at 07:48:27 Pacific
Subject: help on creating DOS Batch file
Reply: (edit)

sir, actually this is an internal batch program for microstation...

masbatch cnvdg >--> command
inname:***.*** >--> filename to be converted
outname:***.*** >--> filename output

syntax -->
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.. =)


Report Offensive Follow Up For Removal

Response Number 16
Name: Mechanix2Go
Date: March 25, 2007 at 05:16:17 Pacific
Subject: help on creating DOS Batch file
Reply: (edit)

I'm lost. I think you need a new thread.


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

M2



Report Offensive Follow Up For Removal

Response Number 17
Name: akira29krj
Date: April 9, 2007 at 22:55:01 Pacific
Subject: help on creating DOS Batch file
Reply: (edit)

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....


Report Offensive Follow Up For Removal






Use following form to reply to current message:

   Name: From My Computing.Net Settings
 E-Mail: From My Computing.Net Settings

Subject: help on creating DOS Batch file

Comments:

 


  Homepage URL (*): 
Homepage Title (*): 
         Image URL: 
 
Data Recovery Software