Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

INSTR in Windows Shell Scrupt

Posted on 2009-05-14
13
Medium Priority
?
507 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 1000 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
Prepare for your VMware VCP6-DCV exam.

Josh Coen and Jason Langer have prepared the latest edition of VCP study guide. Both authors have been working in the IT field for more than a decade, and both hold VMware certifications. This 163-page guide covers all 10 of the exam blueprint sections.

 
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 1000 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 71

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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

This article helps those who get the 0xc004d307 error when trying to rearm (reset the license) Office 2013 in a Virtual Desktop Infrastructure (VDI) and/or those trying to prep the master image for Microsoft Key Management (KMS) activation. (i.e.- C…
Know the reasons and solutions to move/import EDB to New Exchange Server. Also, find out how to recover an Exchange .edb file and to restore the file back.
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…
In this video, viewers are given an introduction to using the Windows 10 Snipping Tool, how to quickly locate it when it's needed and also how make it always available with a single click of a mouse button, by pinning it to the Desktop Task Bar. Int…

618 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