Page 1 of 1
which wildcard can be used in this case??
Posted: 20 Jun 2018 06:17
by goodywp
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
Re: which wildcard can be used in this case??
Posted: 20 Jun 2018 07:08
by Squashman
A question mark for a single character.
Here is a good read for you.
viewtopic.php?t=6207
Re: which wildcard can be used in this case??
Posted: 20 Jun 2018 08:15
by sst
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
Re: which wildcard can be used in this case??
Posted: 20 Jun 2018 08:40
by goodywp
I tried it and not working. probably as sst said that I need to access the file...
Thanks anyway..
Re: which wildcard can be used in this case??
Posted: 20 Jun 2018 08:44
by goodywp
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...
Re: which wildcard can be used in this case??
Posted: 20 Jun 2018 08:54
by Squashman
goodywp wrote: ↑20 Jun 2018 08:40
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.
Re: which wildcard can be used in this case??
Posted: 20 Jun 2018 09:33
by goodywp
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
Re: which wildcard can be used in this case??
Posted: 20 Jun 2018 11:23
by Squashman
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?
Re: which wildcard can be used in this case??
Posted: 20 Jun 2018 12:50
by goodywp
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...
Re: which wildcard can be used in this case??
Posted: 20 Jun 2018 13:56
by pieh-ejdsch
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
Re: which wildcard can be used in this case??
Posted: 20 Jun 2018 16:05
by Squashman
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
)
Re: which wildcard can be used in this case??
Posted: 21 Jun 2018 07:39
by goodywp
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"
Re: which wildcard can be used in this case??
Posted: 21 Jun 2018 07:43
by goodywp
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
Re: which wildcard can be used in this case??
Posted: 21 Jun 2018 09:00
by pieh-ejdsch
You do not have to add the file extension additionally. This is done in the variable with the for extension "%%~nxi".