Discussion forum for all Windows batch related topics.
Moderator: DosItHelp
-
doscode
- Posts: 175
- Joined: 15 Feb 2012 14:02
#1
Post
by doscode » 19 Feb 2012 10:20
I have a variable !digit! which is "0", and variable !tempStr! which is specification for filename that I look for.
Code: Select all
SET tempStr="GEN !digit! *.html"
echo T:!tempStr!
:: Prints T:"GEN 0 *.html"
Then I have variable %%H which is "GEN 0 GENERAL.html".
Code: Select all
echo %%H
:: prints "GEN 0 GENERAL.html" (quotes are displayed)
I need to compare if the variable %%H includes filename matching !tempStr! or "GEN 0*.html".
I tried to find this is manuals but did not find any help. I hope somebody could help me with it. I need it for IF condition.
Last edited by
doscode on 19 Feb 2012 10:50, edited 1 time in total.
-
foxidrive
- Expert
- Posts: 6031
- Joined: 10 Feb 2012 02:20
#2
Post
by foxidrive » 19 Feb 2012 10:33
:: Prints T:"GEN 0*.html"
:: prints "GEN 0 GENERAL.html"
I need to compare if the variable %%H includes filename matching !tempStr! or "GEN 0*.html".
Can you give an example of !tempSTR! where it DOES match in %%H? I ask because in one you have GEN 0 followed by a space and in the other there is no space.
What we also need to know is does
*.html
match
GENERAL.html
or do you need to do simpler string comparison?
-
!k
- Expert
- Posts: 378
- Joined: 17 Oct 2009 08:30
- Location: Russia
#3
Post
by !k » 19 Feb 2012 10:53
SET tempStr="GEN !digit! .*.html"
...
echo %%H |findstr /r /c:!tempStr! >nul &&echo YES ||echo NO
-
doscode
- Posts: 175
- Joined: 15 Feb 2012 14:02
#4
Post
by doscode » 19 Feb 2012 10:56
There should be space after digit. I repaired the code.
I don't understand your second question. There is not *.html and there is not GENERAL.html. There are only files html files beginning with GEN , 4th letter is space, 5th is digit and 6th is space. I ask if there is "GEN "+digit+space+*.html .... in the filename.
I would like better to have it as part of IF <conditon>() ELSE () command. If possible.
-
Squashman
- Expert
- Posts: 4488
- Joined: 23 Dec 2011 13:59
#5
Post
by Squashman » 19 Feb 2012 11:03
So all you really want to do is compare the first 5 characters of each string?
Sure would like to see your entire code. It really helps people understand what the intended goal of the batch file is. Are you attempting to see if a file exists?
-
doscode
- Posts: 175
- Joined: 15 Feb 2012 14:02
#6
Post
by doscode » 19 Feb 2012 11:10
!k wrote:SET tempStr="GEN !digit! .*.html"
...
echo %%H |findstr /r /c:!tempStr! >nul &&echo YES ||echo NO
I think there are still quotes because if I type
Code: Select all
echo tempStr:!tempStr!
echo %%H |findstr /r /c:!tempStr! >nul &&echo YES ||echo NO
It prints
tempStr:
"GEN 0 *.html
"NO
Last edited by
doscode on 19 Feb 2012 11:26, edited 1 time in total.
-
aGerman
- Expert
- Posts: 4740
- Joined: 22 Jan 2010 18:01
- Location: Germany
#7
Post
by aGerman » 19 Feb 2012 11:12
You can't use wildcards in an if condition. !k is almost right, except that the dot before html has to be escaped for regular expressions.
Code: Select all
SET tempStr="GEN !digit! .*\.html"
or in case there must be min. one character before .html
Code: Select all
SET tempStr="GEN !digit! ..*\.html"
Use !k's code or Errorlevel logic in an if statement instead:
Code: Select all
echo %%H |findstr /rc:!tempStr! >nul
if errorlevel 1 (
echo NOT found
) else (
echo found
)
Regards
aGerman
-
doscode
- Posts: 175
- Joined: 15 Feb 2012 14:02
#8
Post
by doscode » 19 Feb 2012 11:17
Squashman wrote:So all you really want to do is compare the first 5 characters of each string?
I want to compare the "GEN 0 *.html" or "^GEN 0 .*\.html" if it would be regular expression, with the file name
-
doscode
- Posts: 175
- Joined: 15 Feb 2012 14:02
#9
Post
by doscode » 19 Feb 2012 11:20
aGerman wrote:You can't use wildcards in an if condition. !k is almost right, except that the dot before html has to be escaped for regular expressions.
I tried, but I think problem is that !tempStr! contains quotes. So no match.
-
aGerman
- Expert
- Posts: 4740
- Joined: 22 Jan 2010 18:01
- Location: Germany
#10
Post
by aGerman » 19 Feb 2012 11:25
doscode wrote:I think problem is that !tempStr! contains quotes. So no match.
The expression after
/rc: has to be enclosed in quotes. For that reason it should work.
Regards
aGerman
-
doscode
- Posts: 175
- Joined: 15 Feb 2012 14:02
#11
Post
by doscode » 19 Feb 2012 11:30
aGerman wrote:The expression after /rc: has to be enclosed in quotes. For that reason it should work.
Oh, I understand. I had mistake in regex. It works. I originally thought it should be possible to compare without need of call external program. I tried to use ~ .
-
doscode
- Posts: 175
- Joined: 15 Feb 2012 14:02
#12
Post
by doscode » 19 Feb 2012 11:47
My code:
Code: Select all
@echo off
SETLOCAL EnableDelayedExpansion
for /f "delims=" %%P in ('dir /b *.pdf') do (
SET "sPDFName=%%~nxP"
IF "!sPDFName:~0,1!"=="0" (SET "sPDFName=!sPDFName:~0,1!")
IF "!sPDFName:~0,1!"=="1" (SET "sPDFName=!sPDFName:~0,1!")
IF "!sPDFName:~0,1!"=="2" (SET "sPDFName=!sPDFName:~0,1!")
IF "!sPDFName:~0,1!"=="3" (SET "sPDFName=!sPDFName:~0,1!")
IF "!sPDFName:~0,1!"=="4" (SET "sPDFName=!sPDFName:~0,1!")
IF "!sPDFName:~0,1!"=="5" (SET "sPDFName=!sPDFName:~0,1!")
IF "!sPDFName:~0,1!"=="6" (SET "sPDFName=!sPDFName:~0,1!")
IF "!sPDFName:~0,1!"=="7" (SET "sPDFName=!sPDFName:~0,1!")
IF "!sPDFName:~0,1!"=="8" (SET "sPDFName=!sPDFName:~0,1!")
IF "!sPDFName:~0,1!"=="9" (SET "sPDFName=!sPDFName:~0,1!")
SET tempStr="^GEN !sPDFName! .*\.html"
for /f "delims=" %%H in ('dir /b *.html') do (
echo %%H |findstr /r /c:!tempStr! >nul
if errorlevel 1 ( echo NOT found ) else ( echo found )
pause
)
)
Is it possible to break loop #2 when the errorlevel is 1?
-
Squashman
- Expert
- Posts: 4488
- Joined: 23 Dec 2011 13:59
#13
Post
by Squashman » 19 Feb 2012 11:57
Use a GOTO. But that will break both loops. Need to call the 2nd loop from the first
Last edited by
Squashman on 19 Feb 2012 12:01, edited 1 time in total.
-
doscode
- Posts: 175
- Joined: 15 Feb 2012 14:02
#14
Post
by doscode » 19 Feb 2012 11:58
Squashman wrote:Use a GOTO.
I thought about it but I didn't know if this will break the loop. I thought that if I would use goto, so the loop 2 would run still.
-
Squashman
- Expert
- Posts: 4488
- Joined: 23 Dec 2011 13:59
#15
Post
by Squashman » 19 Feb 2012 12:10
You realize all those set statements are unnecessary. You could change your DIR command in your first for loop to only display PdF files that begin with a number by listing each ('dir /a-d /b 0*.pdf 1*.pdf etc...) or piping your existing dir command to the findstr command and define a regular expression that finds a number at the beginning of the filename.