Search for files that contain the $ symbol

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Message
Author
balubeto
Posts: 136
Joined: 08 Dec 2011 12:14

Re: Search for files that contain the $ symbol

#16 Post by balubeto » 19 Jun 2017 01:42

penpen wrote:
balubeto wrote:There is something that does not work because your script analyzes the files content and not their names. Why?
Have you tested it, or do you assume that the script analyzes the files content?
Note that the above is a "for /R" loop and no "for /F" loop.


penpen


I tested the command

for /R "C:\" %a in (.) do if "%%~nxa" == "$Windows.~BT" echo(%%~fa


and noticed that it looks for the specified string in each file and displays the full path of the files where the string exists. Why?

Thanks

Bye

penpen
Expert
Posts: 1992
Joined: 23 Jun 2013 06:15
Location: Germany

Re: Search for files that contain the $ symbol

#17 Post by penpen » 19 Jun 2017 03:00

It shouldn't looks for the specified string in each file!

Could it be that you mean "it looks for the specified string in each filename"? That would be correct.
The "for /R" loop is searching for files and directories. The only way to let it search for directories only is to search for the filename ".", but this will find all directories.

But now that i am more thinking about it, against my note above note it might be faster to also search for filenames, and check if the found item is a directory:

Code: Select all

@echo off
setlocal enableExtensions disableDelayedExpansion
for /R "C:\" %%a in ($Windows.~BT) do if exist "%%~fa" if "%%~nxa" == "$Windows.~BT" (
   set "attributes=%%~aa"
   setlocal enableExtensions enableDelayedExpansion
   if NOT "!attributes!" == "!attributes:*d=%%!" echo(%%~fa
   endlocal
)
goto :eof


penpen

Edit: Corrected the base directory of the "for /R" loop from "C:\test" to "C:\".

balubeto
Posts: 136
Joined: 08 Dec 2011 12:14

Re: Search for files that contain the $ symbol

#18 Post by balubeto » 19 Jun 2017 09:14

penpen wrote:It shouldn't looks for the specified string in each file!

Could it be that you mean "it looks for the specified string in each filename"? That would be correct.


Yes

The "for /R" loop is searching for files and directories. The only way to let it search for directories only is to search for the filename ".", but this will find all directories.

But now that i am more thinking about it, against my note above note it might be faster to also search for filenames, and check if the found item is a directory:

Code: Select all

@echo off
setlocal enableExtensions disableDelayedExpansion
for /R "C:\test" %%a in ($Windows.~BT) do if exist "%%~fa" if "%%~nxa" == "$Windows.~BT" (
   set "attributes=%%~aa"
   setlocal enableExtensions enableDelayedExpansion
   if NOT "!attributes!" == "!attributes:*d=%%!" echo(%%~fa
   endlocal
)
goto :eof


penpen


This script produces no output.

In practice, your script does not find the hidden files or directories. Why?

Thanks

Bye

penpen
Expert
Posts: 1992
Joined: 23 Jun 2013 06:15
Location: Germany

Re: Search for files that contain the $ symbol

#19 Post by penpen » 19 Jun 2017 10:24

Oh, :oops: ... sorry for that, i haven't those files, so i created som,e in a test directory "C:\test".
You should just change this string to "C:\", i have corrected the above code.

penpen

balubeto
Posts: 136
Joined: 08 Dec 2011 12:14

Re: Search for files that contain the $ symbol

#20 Post by balubeto » 19 Jun 2017 10:53

penpen wrote:Oh, :oops: ... sorry for that, i haven't those files, so i created som,e in a test directory "C:\test".
You should just change this string to "C:\", i have corrected the above code.

penpen


Code: Select all

@echo off
setlocal enableExtensions disableDelayedExpansion
for /R "C:\" %%a in ($Windows.~BT) do if exist "%%~fa" if "%%~nxa" == "$Windows.~BT" (
   set "attributes=%%~aa"
   setlocal enableExtensions enableDelayedExpansion
   if NOT "!attributes!" == "!attributes:*d=%%!" echo(%%~fa
   endlocal
)
goto :eof


Even so, this script produces no output because your script does not find the hidden files or directories. Why?

Thanks

Bye

penpen
Expert
Posts: 1992
Joined: 23 Jun 2013 06:15
Location: Germany

Re: Search for files that contain the $ symbol

#21 Post by penpen » 19 Jun 2017 13:48

It doesn't search for files - it seemed, that you didn't search for files:
I also mentioned it, and you didn't contradict.
If you also need files just remove anything in round brackets and replace it with "echo(%%~fa".

But this code should definitely find hidden directories; maybe the command takes longer than you expect (searches the whole directory structure - again as your initial dir command(s)). You may want to test it:
Add a ">&2 echo finished" line between the closing bracket and the "goto :eof".


penpen

balubeto
Posts: 136
Joined: 08 Dec 2011 12:14

Re: Search for files that contain the $ symbol

#22 Post by balubeto » 20 Jun 2017 04:09

penpen wrote:It doesn't search for files - it seemed, that you didn't search for files:
I also mentioned it, and you didn't contradict.
If you also need files just remove anything in round brackets and replace it with "echo(%%~fa".

But this code should definitely find hidden directories; maybe the command takes longer than you expect (searches the whole directory structure - again as your initial dir command(s)). You may want to test it:
Add a ">&2 echo finished" line between the closing bracket and the "goto :eof".


penpen


Now, you might explain a particular:

If I run this script

Code: Select all

@echo off
for /R "C:\" %%a in (%1) do if exist "%%~fa" if "%%~nxa" == "%1" echo(%%~fa
goto :eof


it works perfectly.

While I run the for /R "C:\" %a in ($Windows.~BT) do if exist "%%~fa" if "%%~nxa" == "$Windows.~BT" echo(%%~fa command from the command line, the screen is filled with code and I do not get the same result as the script?

Thanks

Bye

penpen
Expert
Posts: 1992
Joined: 23 Jun 2013 06:15
Location: Germany

Re: Search for files that contain the $ symbol

#23 Post by penpen » 20 Jun 2017 08:01

I assume you are calling the script using the parameter:

Code: Select all

script.bat $Windows.~BT
So don't use doublequotes around $Windows.~BT (or use %~1 within the script instead of %1)
(If running from inside another batch you should add a call in front.)


If you execute for commands from command line, then you should ensure that you use a single % to access for variables (%a, %~a, %~fa, %~nxa, ...). Also if you only want to have data output only, you must ensure to not echo the commands; actually you get tons of these ("the screen is filled with code"), but that doesn't mean that it finds anything.

So the command displaying only the result of the search for the shell is:

Code: Select all

@(for /R "C:\" %a in ($Windows.~BT) do @(if exist "%~fa" if "%~nxa" == "$Windows.~BT" echo(%~fa))


penpen

balubeto
Posts: 136
Joined: 08 Dec 2011 12:14

Re: Search for files that contain the $ symbol

#24 Post by balubeto » 21 Jun 2017 11:09

penpen wrote:I assume you are calling the script using the parameter:

Code: Select all

script.bat $Windows.~BT
So don't use doublequotes around $Windows.~BT (or use %~1 within the script instead of %1)
(If running from inside another batch you should add a call in front.)


If you execute for commands from command line, then you should ensure that you use a single % to access for variables (%a, %~a, %~fa, %~nxa, ...). Also if you only want to have data output only, you must ensure to not echo the commands; actually you get tons of these ("the screen is filled with code"), but that doesn't mean that it finds anything.

So the command displaying only the result of the search for the shell is:

Code: Select all

@(for /R "C:\" %a in ($Windows.~BT) do @(if exist "%~fa" if "%~nxa" == "$Windows.~BT" echo(%~fa))


penpen


It works fine.

Thanks

Bye

Post Reply