Solved

INSTR in Windows Shell Scrupt

Posted on 2009-05-14
13
499 Views
Last Modified: 2012-05-07
I need to write a windows shell script that takes an input file like "20090508.txt" and return a string  "20080508" i.e return all characters before ".txt"

The file name is not a constant, the input could also be "HP_20090508.txt".

I need a code ot a function that truncates everything after ".txt"
0
Comment
Question by:Nathan_nyc
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
13 Comments
 
LVL 21

Accepted Solution

by:
AmazingTech earned 250 total points
ID: 24390782
Something like this?
for /f "tokens=1 delims=." %%a in ('echo HP_20090508.txt') do echo %%a

Open in new window

0
 
LVL 21

Expert Comment

by:AmazingTech
ID: 24390794
If it is an actual file name and it is in C:\Folder.
for /f "tokens=*" %%a in ('dir /a-d /b "C:\Folder\*.*"') do echo %%~na
 

Open in new window

0
 

Author Comment

by:Nathan_nyc
ID: 24391118
Thanks. That was the perfect answer. How do I convert the value of %%aa in your code to a variable. The below code doesnt seem to work

for /f "tokens=1 delims=." %%a in ('echo HP_20090508.txt') do set /a newfilename =  %%a
echo %newfilename %


 
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 16

Expert Comment

by:t0t0
ID: 24391406
Try this simple single-line batch file. Copy and paste the the single line into notepad and save as INSTR.BAT.

    @echo "%~n1"


Then, at a DOS prompt command line, enter the following command:

   INSTR "HP_20090508.txt"
0
 
LVL 16

Assisted Solution

by:t0t0
t0t0 earned 250 total points
ID: 24391423
If you want to convert input to a variable then do the following:

    @echo off
    set STRING="%~n1%
    echo %STRING%


To use, enter the following command:

    INSTR "HP_20090508.txt"
0
 
LVL 16

Expert Comment

by:t0t0
ID: 24391455
Oops! I forgot the closing quotes

If you want to convert input to a variable then do the following:

    @echo off
    set STRING="%~n1%"
    echo %STRING%


To use, enter the following command:

    INSTR "HP_20090508.txt"
0
 
LVL 16

Expert Comment

by:t0t0
ID: 24391504
Finally.... If you're generating the input string from within a batch file then this might help you too:

    @echo off
    call :StripExtension "HP_20090508.txt"
    exit /b

    :StripExtension
    set STRING="%~n1%"
    echo %STRING%
    exit /b


Or you  could set your variable first as in:

    @echo off
    set STRING="HP_20090508.txt"
    call :StripExtension %STRING%
    exit /b

    :StripExtension
    set STRING="%~n1%"
    echo %STRING%
    exit /b

0
 
LVL 16

Expert Comment

by:t0t0
ID: 24391529
Oh, and VERY finally, you could do this:

    @echo off
    set STRING="HP_20090508.txt"
    for /f "tokens=*" %%a in ("%STRING%") do set STRING="%%~na"
    echo %STRING%
0
 
LVL 16

Expert Comment

by:t0t0
ID: 24391554
Oh, and in true INSTR fashion, you can do this:

    @echo off
    set STRING=HP_20090508.txt
    set STRING=%STRING:~0,-4%
    echo %STRING%


Or if you're bent on using quotes then do this:

    @echo off
    set STRING="HP_20090508.txt"
    set STRING=%STRING:~0,-5%"
    echo %STRING%


0
 
LVL 16

Expert Comment

by:t0t0
ID: 24391565
AND LASTLY (VERY lastly)... You can do this:

    @echo off
    set STRING="HP_20090508.txt"
    set STRING=%STRING:.txt=%
    echo %STRING%
0
 
LVL 21

Expert Comment

by:AmazingTech
ID: 24393188
Just set without the /a. /a if for arithmetic operations.

for /f "tokens=1 delims=." %%a in ('echo HP_20090508.txt') do set newfilename=%%a
echo %newfilename %

Look at t0t0's suggestions as well. Some very good different samples on how to get achieve your goal.
0
 
LVL 70

Expert Comment

by:Qlemo
ID: 24395275
echo %newfilename%
of course, the space does not belong there.
0
 
LVL 16

Expert Comment

by:t0t0
ID: 24399096
Well done AmazingTech.... Please forgive me for posting all those variations.... It's an honour to share points with you.
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Learn how to PXE Boot both BIOS & UEFI machines with DHCP Policies and Custom Vendor Classes
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

738 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question