Create an account

Very important

  • To access the important data of the forums, you must be active in each forum and especially in the leaks and database leaks section, send data and after sending the data and activity, data and important content will be opened and visible for you.
  • You will only see chat messages from people who are at or below your level.
  • More than 500,000 database leaks and millions of account leaks are waiting for you, so access and view with more activity.
  • Many important data are inactive and inaccessible for you, so open them with activity. (This will be done automatically)


Thread Rating:
  • 608 Vote(s) - 3.45 Average
  • 1
  • 2
  • 3
  • 4
  • 5
substring using bat command

#1
I have the following thing in my bat file. say

set path=c:\temp\test

so basically i want to have an output which would give me the result as c:\temp\

i didnt find any indexof equivalent in bat command.

Thanks.
Reply

#2
A question that really makes me wish [4DOS](

[To see links please register here]

) still existed. However, I found something that might help in [`alt.msdos.batch.nt`](

[To see links please register here]

). The manual page for `set` seems to contain most of the same information. (command `help set`)

set test=123456789

rem extract chars 0-5 from the variable test
set test=%test:~0,5%

echo %test%

(Note: tested on Windows XP SP3)
Reply

#3
Naïve substrings have the problem that you have to adjust them every time your paths change and it isn't a generic solution to the problem.

The following batch file gives a proof of concept how you might do the truncation part of the path:

@echo off
set foo=C:\Temp\Test
call :strip
echo %foo%
goto :eof

:strip
if not "%foo:~-1%"=="\" (
set foo=%foo:~0,-1%
goto :strip
)
goto :eof

It's hard-coded to a single variable but that is easily fixed if needed.

The core part here is the `strip` subroutine which loops and cuts off the last character of the string until a backslash is found. This effectively removes the last part of the path.
Reply

#4
Why do you want that?

[Johannes'][1] answer is a possible solution, but maybe the path you refer to is being (or could be) passed as an argument to the script, in which case you can use the following syntax:

REM Extracts the drive and path from argument %1
SET p=%~dp1

Alternatively you may combine `..` and the script path (`%0`):

REM Sets p to a sibling of the script directory
SET p=%~dp0..\test

[1]:

[To see links please register here]

Reply

#5
Background:

>set fullname=c:\mypath\oldfile
>set changedname=%fullname:oldfile=newfile%
>echo %changedname%
c:\mypath\newfile

Applied to problem:

> set fullname=c:\mypath\oldfile
> set pathonly=%fullname:oldfile=%
> echo %pathonly%
c:\mypath\
Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

©0Day  2016 - 2023 | All Rights Reserved.  Made with    for the community. Connected through