Solved

INSTR in Windows Shell Scrupt

Posted on 2009-05-14
13
490 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
Portable, direct connect server access

The ATEN CV211 connects a laptop directly to any server allowing you instant access to perform data maintenance and local operations, for quick troubleshooting, updating, service and repair.

 
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

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

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

Know what services you can and cannot, should and should not combine on your server.
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
This Micro Tutorial will give you a basic overview of Windows DVD Burner through its features and interface. This will be demonstrated using Windows 7 operating system.
This is used to tweak the memory usage for your computer, it is used for servers more so than workstations but just be careful editing registry settings as it may cause irreversible results. I hold no responsibility for anything you do to the regist…

856 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