Jump to content
  • Advertisement
Sign in to follow this  
cignox1

Batch on windows: string manipulation

This topic is 2990 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi all, as I never really worked with batch files, I'm a bit stuck with this task: I have a full path to a file and another path which is a substring of the previous. In addition I have another path i.e.

C:\temp\images\mypics\image.jpg
C:\temp\images
C:\mydocuments\data

What I need is building another path as follows:

C:\mydocuments\data\mypics\image.jpg

The key here is that I need to subtract the second path from the first, so that I have the relative part only, and then adding this to the third path.

Unlukily, I can't find a way to do this. Any idea?

Thank you!

Share this post


Link to post
Share on other sites
Advertisement
Use the SET statement to manipulate environment variables (type help set at the prompt to see a list of features).
Quote:
set src=C:\temp\images\mypics\image.jpg
set dest=%src:C:\temp\images=C:\mydocuments\data%
echo %src% -^> %dest%
I'd be more tempted to use the Windows Scripting Host instead of batch scripting, as that is available on nearly every installation of Windows these days and allows you to use more conventional programming languages (e.g. the preinstalled JScript or VBScript).

Share this post


Link to post
Share on other sites
Quote:
Original post by benryves
Use the SET statement to manipulate environment variables (type help set at the prompt to see a list of features).
Quote:
set src=C:\temp\images\mypics\image.jpg
set dest=%src:C:\temp\images=C:\mydocuments\data%
echo %src% -^> %dest%
I'd be more tempted to use the Windows Scripting Host instead of batch scripting, as that is available on nearly every installation of Windows these days and allows you to use more conventional programming languages (e.g. the preinstalled JScript or VBScript).


Thank you, perhaps I should have been more precise though: I know that there is the replacement operator, but I was not able to use it with variables (all the paths involved are passed as arguments to the script). Am I just using the wrong syntax or it is not even possible?

Thank you for suggesting WSH. I'll considering it but its not up to me to take that decision so I fear I'm still bound to batch for now.

Share this post


Link to post
Share on other sites
Quote:
Original post by cignox1
I know that there is the replacement operator, but I was not able to use it with variables (all the paths involved are passed as arguments to the script). Am I just using the wrong syntax or it is not even possible?
I don't know if there's a nicer way than this, but it works:
Quote:
set src=C:\temp\images\mypics\image.jpg
set a=C:\temp\images
set b=C:\mydocuments\data
cmd /c set dest=%%src:%a%=%b%%%
echo %src% -^> %dest%

Or, for something more like you'd presumably be using:
Quote:
@echo off
cmd /c set dest=%%src:%2=%3%%
echo %1 -^> %dest%

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!