Solved

INSTR in Windows Shell Scrupt

Posted on 2009-05-14
13
495 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
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
Industry Leaders: 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 69

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 eBook: Backup on AWS

Everything you need to know about backup and disaster recovery with AWS, for FREE!

Question has a verified solution.

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

Issue: One Windows 2008 R2 64bit server on the network unable to connect to a buffalo Device (Linkstation) with firmware version 1.56. There are a total of four servers on the network this being one of them. Troubleshooting Steps: Connect via h…
Learn how to PXE Boot both BIOS & UEFI machines with DHCP Policies and Custom Vendor Classes
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
In this video, we discuss why the need for additional vertical screen space has become more important in recent years, namely, due to the transition in the marketplace of 4x3 computer screens to 16x9 and 16x10 screens (so-called widescreen format). …

685 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