?
Solved

Dos batch file reading parameters

Posted on 2009-04-27
4
Medium Priority
?
787 Views
Last Modified: 2012-08-13
i want to create a batch file, in which i can pass in a parameter which is a file name.
for eg filename like abc_20090427.txt
once this parameter is passed i would like to read it inside the batch file and check it if the file is abc_20090427.txt.gz i want to delete it else exit. the batch file by doing nothing.
Please note abc_*.txt.gz is constant but the date can change every time i execute it.

Thanks in advance
this is what i have now but i dont know what is the like operator in dos.
@echo off
SET sname= %1
echo This is passed %sname% 
 
if %sname% == abc*.txt.gz (
echo here i would like to exit this bactch file
)ELSE (
del %1
)

Open in new window

0
Comment
Question by:kenny577
  • 2
4 Comments
 
LVL 21

Expert Comment

by:AmazingTech
ID: 24245250
Use if exist.
this is what i have now but i dont know what is the like operator in dos.
@echo off
SET sname= %1
echo This is passed %sname% 
 
if exist %sname%.gz (
echo here i would like to exit this bactch file
)ELSE (
del %1
)

Open in new window

0
 

Author Comment

by:kenny577
ID: 24245400
But i need to compare the parameter passed with the constant part of the file name abc.????????.txt.gz

for eg. abc.20090427.txt.gz is passed as a parameter, i want to check if the passes file name is abc.????????.txt.gz if thats true i would like to exit the batch file else i would like to delete it.
in the above i dont see its comparing with the contant part fo the file name.

Thanks,
K
0
 
LVL 43

Accepted Solution

by:
Steve Knight earned 2000 total points
ID: 24245535
The easiest way here is probably to check the start and end of the parameter, e.g.

@echo off
Remove any "" characters and set name
set name=%~1
REM check if first four chars are abc.
if not "%name:~0,4%"=="abc." goto error
REM check if last seven char are .txt.gz"
if not "%name:~-7,7%==".txt.gz" goto error
echo The name is %name% and that is OK.
REM Do what you need on "%name%"

goto end
:error
 echo "%name%" is not a valid name

:end
0
 

Author Closing Comment

by:kenny577
ID: 31575135
Thanks
0

Featured Post

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Question has a verified solution.

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

TOMORROW TOMORROW.BAT is inspired by a question I get asked over and over again; that is, "How can I use batch file commands to obtain tomorrow's date?" The crux of this batch file revolves around the XCOPY command - a technique I discovered w…
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…
This Micro Tutorial will teach you how to add a cinematic look to any film or video out there. There are very few simple steps that you will follow to do so. This will be demonstrated using Adobe Premiere Pro CS6.
Screencast - Getting to Know the Pipeline

862 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