Solved

INSTR in Windows Shell Scrupt

Posted on 2009-05-14
13
464 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
13 Comments
 
LVL 21

Accepted Solution

by:
AmazingTech earned 250 total points
Comment Utility
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
Comment Utility
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
Comment Utility
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
 
LVL 16

Expert Comment

by:t0t0
Comment Utility
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
Comment Utility
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
Comment Utility
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
Do email signature updates give you a headache?

Constantly trying to correctly format email signatures? Spending all of your time at every user’s desk to make updates? Want high-quality HTML signatures on all devices, including on mobiles and Macs? Then, let Exclaimer solve all your email signature problems today!

 
LVL 16

Expert Comment

by:t0t0
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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 68

Expert Comment

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

Expert Comment

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

Featured Post

Do email signature updates give you a headache?

Constantly trying to correctly format email signatures? Spending all of your time at every user’s desk to make updates? Want high-quality HTML signatures on all devices, including on mobiles and Macs? Then, let Exclaimer solve all your email signature problems today!

Join & Write a Comment

VALIDATING DATES One method of validating dates is to jam the date into the DATE command and see if it accepts it by examining the system's errorlevel value. A non-zero result indicates failure. A typical example might look something like the fol…
YESTERDAY YESTERDAY.BAT is inspired by a previous article I wrote entitled: TOMORROW.BAT (http://www.experts-exchange.com/OS/Microsoft_Operating_Systems/MS_DOS/A_4196-Advanced-Batch-File-Programming-TOMORROW-BAT.html). The crux of this batch f…
Windows 8 comes with a dramatically different user interface known as Metro. Notably missing from the new interface is a Start button and Start Menu. Many users do not like it, much preferring the interface of earlier versions — Windows 7, Windows X…
The viewer will learn how to successfully create a multiboot device using the SARDU utility on Windows 7. Start the SARDU utility: Change the image directory to wherever you store your ISOs, this will prevent you from having 2 copies of an ISO wit…

744 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now