Using the @instr function

Dec 26, 2009
29
2
No where
@INSTR[start,[length],string]

I've been using the @instr function for a long time to extract part of a filename when I have a lot of files I want to rename. Works great, no problem. But I can't figure out how to work backwards from the end of the filename, which the documentation says is supported:

"If length is negative, the offset is measured leftward from the right end of the string, and its length is specified by the value of length without the minus sign."

The example given in the documentation is fine for a single string of a known length, but I have lots of files of various lengths with a certain string at the end I don't want. For example, I might have:

filename abcd.jpg
another filename abcd.jpg
a different filename abcd.jpg
the longer weird filename abcd.jpg
something else again abcd.jpg
some really different long weird name abcd.jpg
and another even longer unimportant filename abcd.jpg
....and so on...

And I want to rename them all without the abcd at the end. But I've never figured out how to make this work. Or is there a different function that would be better for this other than @instr.
 
Aug 9, 2009
229
1
@index[test.btm.watch,.,-1] < locates right most "." as numeric value you can use as a start point or a end point in @instr
 
May 20, 2008
11,400
99
Syracuse, NY, USA
I hope these help.
Code:
v:\play> d
2019-08-06  16:34               0  a abcd.txt
2019-08-06  16:34               0  a b abcd.txt

v:\play> do f in * (echo %@instr[7,-8,%f] )
abcd.txt
abcd.txt

v:\play> do f in * (echo %@instr[0,%@index[%f, abcd.txt],%f] )
a
a b

In the first, I want "abcd.txt", 8 characters. the '-' means count from the end; that makes the end 0 so I'll start 7 characters before.

In the second, I don't want " abcd.txt" . That includes the space before.

@LEFT and @RIGHT might be a little easier to use.

Code:
v:\play> d
2019-08-06  16:34               0  a abcd.txt
2019-08-06  16:34               0  a b abcd.txt

v:\play> do f in * (echo %@right[8,%f] )
abcd.txt
abcd.txt

v:\play> do f in * (echo %@left[%@index[%f, abcd.txt],%f] )
a
a b
 

Charles Dye

Super Moderator
Staff member
May 20, 2008
4,461
88
Albuquerque, NM
prospero.unm.edu
I think this would be easier with @REPLACE:
Code:
echo %@replace[ abcd.,.,"another filename abcd.jpg"]

Note that the first argument is not quoted, despite the leading space!
 
Dec 7, 2009
238
2
Left Coast, USA
I think this would be easier with @REPLACE:
Code:
echo %@replace[ abcd.,.,"another filename abcd.jpg"]
Note that the first argument is not quoted, despite the leading space!


Agreed. If there's a text string that appears in a predictable way, that seems far easier than trying to do it via "@instr". Below — one possible approach (tested with a small number of files). Uses "ren /n" to test what would happen. If it appears to work as needed, remove "/n" switch.

Code:
do _filename in *.jpg
    set newname=%@replace[ abcd.,.,%_filename]
    ::  Proceed with "ren" only if the replacement worked:
    if "%newname" ne "%_filename" ren /n "%_filename" "%newname"
enddo
 
Similar threads
Thread starter Title Forum Replies Date
Jesse Heines Using Regular Expressions with the REN commanc Support 8
R WAD Unusable state when using Chinese characters Support 3
Joe Caverly Using TYPE with non-English text Support 22
L Using TCC.exe through an SSH connection Support 3
Joe Caverly Using @PSHELL from @EVAL Support 8
Jay Sage Command Will Not Run Using Short Name of Path Support 5
Jay Sage Cannot Postion Cursor in Command Line Using Mouse Support 7
MikeBaas Using the debugger Support 2
Joe Caverly Multiple Text Searches at once using FFIND or TPIPE Support 4
Joe Caverly Using this CMD technique from TCC Support 17
Joe Caverly What version and Windows OS are you using? Support 3
Craig Fitzgerald Problems using tcstart.btm Support 3
R How to? Append files in multiple subfolders using copy? Support 8
M Ctrl-C when using command line history does nothing Support 2
Joe Caverly Using a Directory Alias with @iniwrite fails Support 14
C French accents using msgbox Support 4
C "Failed to update the system registry. Please try using REGEDIT" Support 3
x13 Problem listing repository files using DIR http(s)://... Support 8
R Regex using ^ Support 2
cxxl WAD Mouse movement sluggish when using TCC list Support 3
jfalch WAD internal "which" should check for FILEEXIST when using AppPaths entry Support 8
D How to? Scale font in TCC using wheel mouse or other means Support 2
fpefpe Documentation copy/move using /c or /u Support 2
gschizas Fixed Using codepage 65001 (UTF-8) breaks non-ASCII characters Support 8
D How to? Use typed envars using regex. Support 3
epement Using "everything" Support 10
U Installing the Compaq Visual Fortran 6.0 compiler using Take Command Support 8
Alexander WAD The number of files and dirs are multiplied when using multiple wildcards Support 6
T using bottom line for status bar Support 3
A How to? How do you launch TCC/LE using the already-open instance? Support 5
rps Fixed Using last argument variable ! Support 5
M Incorrect ARGV1 when using linux-style paths Support 4
D How to? Using a custom ini file with TCC from Take Command Support 7
thedave WAD Unable to type various characters using Windows 8's onscreen keyboard Support 7
D Documentation Using "send all input to other tabs" Support 11
Joe Caverly Word Count using TPIPE Support 10
Phileosophos Fixed Option Dialog Hangs when Using Help Support 7
MickeyF problem using COM object in VBScript from v16 x64 TCC but not from v15 32-bit TCC Support 4
S How to? How do I distinguish missing source file from download error when using HTTP? Support 6
Jay Sage Using EVERYTHING Command in TCMD15 Support 5
Charles Dye Odd behavior after using OPTION "Advanced" tab Support 2
T How to? Using FOLDERMONITOR 1 event causes multiple trigger(ing)s Support 8
samintz WAD FILEWRITE using SMOPEN Support 1
ehab aboudaya How to? 2 GUI questions and return HWND using TakeCommandIPC Support 3
R Fixed Using %~dp1 for paths with unavailable drives Support 6
C Using dif. sending addresses with sendmail Support 11
R TC 13 / Using TCDIALOG to exit commands Support 4
epement Video tutorials of using Take Command Support 2
electrotype Error using TCC: "Label not found" Support 6
Phileosophos unzip not using specified path Support 5

Similar threads