Adding Wildcards to variables

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
haishi
Posts: 4
Joined: 23 May 2018 12:58

Adding Wildcards to variables

#1 Post by haishi » 23 May 2018 13:17

Hello there! This is my first post here, so please apologize if I do not act strictly on forum rules.
I hope you can help!

What I'm trying to achieve:
I am creating a script that allows users tu enter three different variables (number="job number"; customer="Customer Name" and job="job description", that would later create a user path within a specific folder (%number%_%customer%_%job%). That part is easy.

However, what i can not achieve is cross checking if a folder already exists, based solely on the jobnumber.
What the script should do is check if an entered jobnumer is already in use within the destination folder. If so, it should open the existing folder(s) in explorer. There might be more than one with the same number (e.g. our dummy jobs all start with "000000").

Here is the simplified part of the script that should check whether there are already folders starting with the same number:

______________________________________________________

@echo off
set /p number="job number: "

set "folderExist="
for %%a in ("U:\destination\work in progress\%number%*") do set "folderExist=1" & goto continue

:continue
IF DEFINED folderExist (
start "" "U:\destination\work in progress\\%number%*"
) ELSE (
.... do something else ....

)

______________________________________________________

The asterisk as a wildcard is not recognised.

I hope you understand what I am trying to achieve here and that someone can assist me. Any help yould be greatly appreciated.

Best regards,
Andreas

P.S. Please note, that the destination folder is on a mapped network drive. I'm not sure if that makes any difference.

Squashman
Expert
Posts: 4107
Joined: 23 Dec 2011 13:59

Re: Adding Wildcards to variables

#2 Post by Squashman » 23 May 2018 13:58

I suggest you reread the help file for the IF command.

IF DEFINED checks if an environmental variable has any value assigned to it.
IF EXIST checks if a file or folder exists.

haishi
Posts: 4
Joined: 23 May 2018 12:58

Re: Adding Wildcards to variables

#3 Post by haishi » 24 May 2018 00:25

The outcome is the same if I use "if exist", i tried that previously. Problem is, that after the variable %number% I am unable to use a wildcard to have the rest of the foldername ignored and open every folder that stards with a value that is equal to the entered %number%.

MarioZac
Posts: 38
Joined: 16 May 2018 00:12

Re: Adding Wildcards to variables

#4 Post by MarioZac » 24 May 2018 13:00

Instead of FOR-FILES command, try directly
IF EXIST "U:\destination\work in progress\%number%*" do (....)

Squashman
Expert
Posts: 4107
Joined: 23 Dec 2011 13:59

Re: Adding Wildcards to variables

#5 Post by Squashman » 24 May 2018 13:08

Use the FOR command to your advantage.

Code: Select all

@echo off
set /p "number=job number: "

set "folderExist="
for /D %%G in ("U:\destination\work in progress\%number%*") do (
	start "" "%%~G"
	set "folderExist=Y"
)

IF NOT DEFINED folderExist (
	.... do something else ....
)

haishi
Posts: 4
Joined: 23 May 2018 12:58

Re: Adding Wildcards to variables

#6 Post by haishi » 25 May 2018 01:14

WOW Squashman, that does exactly what I need! Thank you very very much!!! :D

Post Reply