logo

askAsk

  
a-2 Home > Computers & Internet >

Programming & Design

 >
 

Question

How do you manipulate a string stored in a batch file variable?


I currently have a batch file that stores a file name in a variable called %xmlvariable% (i.e. filename.xml). For this XML file, there is a corresponding zip file by the same name (i.e. filename.zip). I need to assign the zip file name to another variable called %zipvariable%.
How would I search %xmlvariable%, pull off the "xml" extension, replace it with a "zip" extension, and assign the new string to %zipvariable%?
Unfortunately, I can only use a windows batch script for this operation. While I know how to do this in other languages and scripting environments, this is my limitation.
Thanks in advance for your help!

Answer

I think this will do what you want it to do, the command you want to use if the FOR command in a batch file, you can type "for /?" to get the info for it, but here's what I got. It sets the xmlvariable and the for command extracts the main part of the filename using the %%~ni If this isn't exactly what you want then you can email me and I can try to understand your question better.
set xmlvariable=filename.xml
FOR /F %%i in ("%xmlvariable%") do set nameonly=%%~ni
set zipvariable=%nameonly%.zip
echo %xmlvariable%
echo %zipvariable%

2010-3-9 4:02:41

Other Answer

set zipvariable=%xmlvariable:~0,-3%.zip
this set zipvariable to xmlvariable minus the .xml and replaces it with .zip

2010-3-9 7:25:43

As far as I know, you would have to create/use a separate program for this, or maybe a script (like Perl, Python, PHP etc.) that can be run, and then call it from the batch file using the input as an argument and save the output to the variable.
Batch files are not a scripting language, you know, and isn't intended to do advanced scripting stuff.

2010-3-9 11:11:48

Do these answers help you?


Yesgood!     NOpoor!    Copy URL   Add to Favorites

How do you manipulate a string stored in a batch file _ Programming & Design

answer I can answer

Make sure your answer follows the community guidelines.    

more

A5  ©2010  www.answer5.com  All Rights Reserved   email