which wildcard can be used in this case??

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
goodywp
Posts: 250
Joined: 31 Jul 2017 09:57

which wildcard can be used in this case??

#1 Post by goodywp » 20 Jun 2018 06:17

Hi all,

I have a if statement as below:

Code: Select all

if exist "C:\auto_pkg_build\Sources\Source_pkg\%opkg%\%optn%\Factory Download\TETRA\TELIUM3\%VAR14%\Schemes to Sign_for_%VAR001%.doc" (
.......
)
Now the problem is that sometimes the format of this "Schemes to Sign_for_%VAR001%.doc" vary depend upon the developer and could be
Schemes_to_Sign_for_%VAR001%.doc
Schemes to_Sign_for_%VAR001%.doc
Which wildcard I can use in this case to accommodate all these possible variation of the file name??

Thanks

goodywp

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

Re: which wildcard can be used in this case??

#2 Post by Squashman » 20 Jun 2018 07:08

A question mark for a single character.
Here is a good read for you.
viewtopic.php?t=6207

sst
Posts: 93
Joined: 12 Apr 2018 23:45

Re: which wildcard can be used in this case??

#3 Post by sst » 20 Jun 2018 08:15

If you don't need to access the matched file or know the exact file name, then using wildcard in combination with `IF` statement would suffice, but if there is a need to access the file afterwards (read contents of, know the exact file name, pass the file name to another program,...) then `FOR` statement can do the job of `IF EXIST` and at the same time gives access to the exact match.

Code: Select all

for %%f in ("PathToFile\a?b?c.ext") do (
    REM being here means that there is at least one existing match and %%f refers to the exact match
)
But you should then watch for multiple matches as the loop will be executed for every one of them

goodywp
Posts: 250
Joined: 31 Jul 2017 09:57

Re: which wildcard can be used in this case??

#4 Post by goodywp » 20 Jun 2018 08:40

Squashman wrote:
20 Jun 2018 07:08
A question mark for a single character.
Here is a good read for you.
viewtopic.php?t=6207
I tried it and not working. probably as sst said that I need to access the file...

Thanks anyway..

goodywp
Posts: 250
Joined: 31 Jul 2017 09:57

Re: which wildcard can be used in this case??

#5 Post by goodywp » 20 Jun 2018 08:44

sst wrote:
20 Jun 2018 08:15
If you don't need to access the matched file or know the exact file name, then using wildcard in combination with `IF` statement would suffice, but if there is a need to access the file afterwards (read contents of, know the exact file name, pass the file name to another program,...) then `FOR` statement can do the job of `IF EXIST` and at the same time gives access to the exact match.

Code: Select all

for %%f in ("PathToFile\a?b?c.ext") do (
    REM being here means that there is at least one existing match and %%f refers to the exact match
)
But you should then watch for multiple matches as the loop will be executed for every one of them
Thanks a lot sst!

I tried it and not working due to I need to access the file afterwards. I shall try the FOR command as you suggested.
However, I also need to use this file in a tiny utility (convert word to txt ) therefore give me the limitation to use this...

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

Re: which wildcard can be used in this case??

#6 Post by Squashman » 20 Jun 2018 08:54

goodywp wrote:
20 Jun 2018 08:40
Squashman wrote:
20 Jun 2018 07:08
A question mark for a single character.
Here is a good read for you.
viewtopic.php?t=6207
I tried it and not working. probably as sst said that I need to access the file...

Thanks anyway..
Really hard to help you when you don't show us the exact code you attempted to use. Unfortunately none of us are omniscient.

goodywp
Posts: 250
Joined: 31 Jul 2017 09:57

Re: which wildcard can be used in this case??

#7 Post by goodywp » 20 Jun 2018 09:33

sst wrote:
20 Jun 2018 08:15
If you don't need to access the matched file or know the exact file name, then using wildcard in combination with `IF` statement would suffice, but if there is a need to access the file afterwards (read contents of, know the exact file name, pass the file name to another program,...) then `FOR` statement can do the job of `IF EXIST` and at the same time gives access to the exact match.

Code: Select all

for %%f in ("PathToFile\a?b?c.ext") do (
    REM being here means that there is at least one existing match and %%f refers to the exact match
)
But you should then watch for multiple matches as the loop will be executed for every one of them
I tried to rename it (without access the file) before it can be used for the afterwards code as below code not go through ??

Code: Select all

for %%f in ("C:\auto_pkg_build\Sources\Source_pkg\%opkg%\%optn%\Factory Download\TETRA\TELIUM3\%VAR14%\Schemes?to?Sign_for_%VAR001%.doc") do (
rename %%f Schemes_to_Sign_for_%VAR001%.doc
Does this above command valid?
rename %%f Schemes_to_Sign_for_%VAR001%.doc

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

Re: which wildcard can be used in this case??

#8 Post by Squashman » 20 Jun 2018 11:23

goodywp wrote:
20 Jun 2018 09:33
Does this above command valid?
rename %%f Schemes_to_Sign_for_%VAR001%.doc
Well I will give you a hint. What do you need to do with your commands when your file paths or file names contain spaces?

goodywp
Posts: 250
Joined: 31 Jul 2017 09:57

Re: which wildcard can be used in this case??

#9 Post by goodywp » 20 Jun 2018 12:50

OK, I think I had several try and get what I need for. Here is the one:

Code: Select all

cd "C:\auto_pkg_build\Sources\Source_pkg\%opkg%\%optn%\Factory Download\TETRA\TELIUM3\%VAR14%\"
if exist *.doc (
rename *.doc Schemes_to_Sign_for_%VAR001%.doc
So in this way, I can make sure the file format is always has underscore between words, like Schemes_to_Sign_for_%VAR001%.doc
No matter what the format of the source file as these as mightbe
Schemes to_Sign_for_%VAR001%.doc
Schemes to Sign_for_%VAR001%.doc
Schemes_to_Sign_for_%VAR001%.doc
I always can handle the file to be Schemes_to_Sign_for_%VAR001%.doc as the input to the next code....
Thanks anyway...

pieh-ejdsch
Posts: 239
Joined: 04 Mar 2014 11:14
Location: germany

Re: which wildcard can be used in this case??

#10 Post by pieh-ejdsch » 20 Jun 2018 13:56

You can use this to rename:

Code: Select all

Setlocal enable delayedexpansion
For %%i in (*.doc)  do ( set "filename=%%~nxi"
  Set "filename=!filename: =_!"
  Ren "%%i" "!filename!"
   rem do something
)
I can't see what content your variable is.
Phil

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

Re: which wildcard can be used in this case??

#11 Post by Squashman » 20 Jun 2018 16:05

goodywp wrote:
20 Jun 2018 09:33

Code: Select all

for %%f in ("C:\auto_pkg_build\Sources\Source_pkg\%opkg%\%optn%\Factory Download\TETRA\TELIUM3\%VAR14%\Schemes?to?Sign_for_%VAR001%.doc") do (
rename %%f Schemes_to_Sign_for_%VAR001%.doc
Does this above command valid?
rename %%f Schemes_to_Sign_for_%VAR001%.doc
As I alluded to in my previous comment, you seem to know that you need to use quotes to protect spaces with your full file path within the FOR command so why wouldn't you use quotes with the rename command. Using quotes is a best practice regardless of needing them or not. Pretty sure we have mentioned this to you a couple of times in your previous questions.

Code: Select all

for %%f in ("C:\auto_pkg_build\Sources\Source_pkg\%opkg%\%optn%\Factory Download\TETRA\TELIUM3\%VAR14%\Schemes?to?Sign_for_%VAR001%.doc") do (
     rename "%%f" "Schemes_to_Sign_for_%VAR001%.doc" 2>nul
 )

goodywp
Posts: 250
Joined: 31 Jul 2017 09:57

Re: which wildcard can be used in this case??

#12 Post by goodywp » 21 Jun 2018 07:39

Squashman wrote:
20 Jun 2018 16:05
goodywp wrote:
20 Jun 2018 09:33

Code: Select all

for %%f in ("C:\auto_pkg_build\Sources\Source_pkg\%opkg%\%optn%\Factory Download\TETRA\TELIUM3\%VAR14%\Schemes?to?Sign_for_%VAR001%.doc") do (
rename %%f Schemes_to_Sign_for_%VAR001%.doc
Does this above command valid?
rename %%f Schemes_to_Sign_for_%VAR001%.doc
As I alluded to in my previous comment, you seem to know that you need to use quotes to protect spaces with your full file path within the FOR command so why wouldn't you use quotes with the rename command. Using quotes is a best practice regardless of needing them or not. Pretty sure we have mentioned this to you a couple of times in your previous questions.

Code: Select all

for %%f in ("C:\auto_pkg_build\Sources\Source_pkg\%opkg%\%optn%\Factory Download\TETRA\TELIUM3\%VAR14%\Schemes?to?Sign_for_%VAR001%.doc") do (
     rename "%%f" "Schemes_to_Sign_for_%VAR001%.doc" 2>nul
 )
Thanks Squashman for your comment and make this works by using "%%f" :)

goodywp
Posts: 250
Joined: 31 Jul 2017 09:57

Re: which wildcard can be used in this case??

#13 Post by goodywp » 21 Jun 2018 07:43

pieh-ejdsch wrote:
20 Jun 2018 13:56
You can use this to rename:

Code: Select all

Setlocal enable delayedexpansion
For %%i in (*.doc)  do ( set "filename=%%~nxi"
  Set "filename=!filename: =_!"
  Ren "%%i" "!filename!"
   rem do something
)
I can't see what content your variable is.
Phil
Thanks Phil! for making less hard coding. However we still need define the filename as well as the doc extension

Code: Select all

....
 Ren "%%i" "!filename!.doc"
....
Thanks

pieh-ejdsch
Posts: 239
Joined: 04 Mar 2014 11:14
Location: germany

Re: which wildcard can be used in this case??

#14 Post by pieh-ejdsch » 21 Jun 2018 09:00

You do not have to add the file extension additionally. This is done in the variable with the for extension "%%~nxi".

Post Reply