# Batch on windows: string manipulation

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

## 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 on other sites
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.jpgset 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 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.jpgset 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 on other sites
Quote:
 Original post by cignox1I 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.jpgset a=C:\temp\imagesset b=C:\mydocuments\datacmd /c set dest=%%src:%a%=%b%%%echo %src% -^> %dest%

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

##### Share on other sites
Thank you very much :-)

1. 1
2. 2
Rutin
21
3. 3
A4L
15
4. 4
5. 5

• 13
• 26
• 10
• 11
• 44
• ### Forum Statistics

• Total Topics
633740
• Total Posts
3013621
×