Discussion forum for all Windows batch related topics.
Moderator: DosItHelp
-
miskox
- Posts: 609
- Joined: 28 Jun 2010 03:46
#1
Post
by miskox » 13 Jun 2024 02:50
I have a strange situation:
User runs a .cmd from command prompt and receives an error (findstr command to search within %0):
batch_filename.cmd:
Code: Select all
FINDSTR: Cannot open batch_filename <----- note there is no extension displayed
I checked my computer and in the system settings I have 'File name extensions' checked (open File Explorer -> View -> File name extensions). I played with this and if I run a .cmd (@echo %0&pause) it displays extension regardless of this settings. From command prompt it does not work after I changed settings (and even if I enable it again) (I cannot reboot the computer at the moment to see the result).
I use this FINDSTR within a FOR statement so I thought that this might have something to do with this (because new CMD.EXE is executed and would default to another working direcotory) - if this would be the case @echo %0 (not used within FOR) would work always.
Ideas?
Update: I created another .cmd in a way that FINDSTR command writes to a temporary file which is then read by FOR. When users makes a test I will write an update to this post.
Thanks.
Saso
-
aGerman
- Expert
- Posts: 4674
- Joined: 22 Jan 2010 18:01
- Location: Germany
#2
Post
by aGerman » 13 Jun 2024 13:04
Why do you expect that %0 contains the file extension? It only contains the own call. That is, if the script was called without file extension, %0 would never expand to something with file extension as long as you don't use the typical modifiers.
test.cmd on my desktop:
Code: Select all
@echo off
echo %0
echo "%~f0"
echo ~~~~~~~~~~~~~~
CMD prompt with the desktop being the current workdir:
Code: Select all
Microsoft Windows [Version 10.0.22631.3737]
(c) Microsoft Corporation. Alle Rechte vorbehalten.
C:\Users\steffen\Desktop>test
test
"C:\Users\steffen\Desktop\test.cmd"
~~~~~~~~~~~~~~
C:\Users\steffen\Desktop>test.cmd
test.cmd
"C:\Users\steffen\Desktop\test.cmd"
~~~~~~~~~~~~~~
C:\Users\steffen\Desktop>%userprofile%\desktop\test.cmd
C:\Users\steffen\desktop\test.cmd
"C:\Users\steffen\Desktop\test.cmd"
~~~~~~~~~~~~~~
C:\Users\steffen\Desktop>.\test.cmd
.\test.cmd
"C:\Users\steffen\Desktop\test.cmd"
~~~~~~~~~~~~~~
C:\Users\steffen\Desktop>
Steffen
-
miskox
- Posts: 609
- Joined: 28 Jun 2010 03:46
#3
Post
by miskox » 13 Jun 2024 13:33
Thanks. You know there has always been something like "%0 is a batch file itself."
I never encountered this 'problem' because I always use full filename (I use TAB key).
test.cmd
When run as 'test' only:
and when run as 'test.cmd':
So my FINDSTR command should be something like:
instead of
Thanks.
Saso