Jump to content


 


Register a free account to unlock additional features at BleepingComputer.com
Welcome to BleepingComputer, a free community where people like yourself come together to discuss and learn how to use their computers. Using the site is easy and fun. As a guest, you can browse and view the various discussions in the forums, but can not create a new topic or reply to an existing one unless you are logged in. Other benefits of registering an account are subscribing to topics and forums, creating a blog, and having no ads shown anywhere on the site.


Click here to Register a free account now! or read our Welcome Guide to learn how to use this site.

Photo

Batch Script: to find files


  • Please log in to reply
8 replies to this topic

#1 Richard ken

Richard ken

  • Members
  • 247 posts
  • OFFLINE
  •  
  • Gender:Not Telling
  • Local time:01:19 AM

Posted 04 July 2017 - 03:54 PM

Hi, I have zero experience in batch scripting but I want to write a code that can accept a list of user input, that have partial filename, and search in the directory and sub-directory. If there are matches return where those files are located. I have been searching for anything that could give me some idea, but I couldn't find anything close. Thank you



BC AdBot (Login to Remove)

 


#2 egjk

egjk

  • Members
  • 23 posts
  • OFFLINE
  •  

Posted 04 July 2017 - 04:09 PM

Using a google search I found this:
https://stackoverflow.com/questions/1447703/search-for-files-in-a-batch-script-and-process-those-files

for /r %%x in (*.resx) do echo "%%x"

I think this is what you're looking for? The * is a wildcard which means it captures anything with the file extension .resx.
You could use this to find all files with "pic" in the name by doing something like pic_*.jpg. Or a wildcard for any file extension e.g recommendation_letter.*
You can combine wild cards too :) e.g. pic_001*.* would return pic_0012.jpg and pic_0014.png

Edited by egjk, 04 July 2017 - 04:11 PM.


#3 britechguy

britechguy

    Been there, done that, got the T-shirt


  • Moderator
  • 9,008 posts
  • OFFLINE
  •  
  • Gender:Male
  • Location:Staunton, VA
  • Local time:05:19 AM

Posted 04 July 2017 - 04:10 PM

What you're looking for already exists in the form of the DIR command.

 

See:  https://duckduckgo.com/?q=Windows+DIR+command&t=h_&atb=v33-7__&ia=web

 

and look at the tutorials regarding the switches you need to use.


Brian  AKA  Bri the Tech Guy (website in my user profile) - Windows 10 Home, 64-Bit, Version 1803, Build 17134 

     . . . the presumption of innocence, while essential in the legal realm, does not mean the elimination of common sense outside it.  The willing suspension of disbelief has its limits, or should.

    ~ Ruth Marcus,  November 10, 2017, in Washington Post article, Bannon is right: It’s no coincidence The Post broke the Moore story


 

 

 

              

 


#4 GoofProg

GoofProg

  • Banned
  • 224 posts
  • OFFLINE
  •  
  • Gender:Male
  • Local time:02:19 AM

Posted 07 July 2017 - 04:08 PM

Yeah yeah.  In Windows there is the find tool and the findstr tool.  You may want to look into using the findstr tool in combination with dir /s

That can be a batch script or a little c++ app.
the character > pipes to stdout 

the character < pipes to stdin

so getinput < inputlist.txt 

may also be up your route.



#5 britechguy

britechguy

    Been there, done that, got the T-shirt


  • Moderator
  • 9,008 posts
  • OFFLINE
  •  
  • Gender:Male
  • Location:Staunton, VA
  • Local time:05:19 AM

Posted 07 July 2017 - 05:08 PM

For the sake of clarity, the '>' (redirect stdout [standard output] to), and '<' (redirect to stdin [standard input] from) operators are redirects, not pipes.  Stdout is generally "to the monitor" by default and stdin is generally "from the keyboard" by default, regardless of the operating system.

 

Pipe, generally '|', takes the stdout from the command on the left side and directly passes it as the input [stdin] to the command on its right.  A pipe is, as its name implies, a data pass-through between programs/commands like a pipe is for liquid between its endpoints.

 

A great deal of how you'd do what you outlined via either a command prompt batch file or a powershell script will depend on how you get your input of the partial file names and how much post processing on the output of the commands used will be necessary to strip it down to only what you want to see.


Brian  AKA  Bri the Tech Guy (website in my user profile) - Windows 10 Home, 64-Bit, Version 1803, Build 17134 

     . . . the presumption of innocence, while essential in the legal realm, does not mean the elimination of common sense outside it.  The willing suspension of disbelief has its limits, or should.

    ~ Ruth Marcus,  November 10, 2017, in Washington Post article, Bannon is right: It’s no coincidence The Post broke the Moore story


 

 

 

              

 


#6 JohnnyJammer

JohnnyJammer

  • Members
  • 1,117 posts
  • OFFLINE
  •  
  • Gender:Male
  • Location:QLD Australia
  • Local time:07:19 PM

Posted 10 July 2017 - 08:40 PM

Ok i wrote ones years ago but i also utilize the right click on folder option.

Save this in notepad then save as SearchDirectory.reg and place that in C:\Windows, then right click and select merge to add the registry keys .

Windows Registry Editor Version 5.00

[HKEY_CLASSES_ROOT\Directory\shell\Search_Print DIR]
@="Search Directory"

[HKEY_CLASSES_ROOT\Directory\shell\Search_Print DIR\command]
@="SearchDirectory.bat \"%1\""

Now in notepad again paste this code below  and save as SearchDirectory.bat in C:\Windows .

@echo off
IF exist %temp%\Listing.txt del %temp%\Listing.txt
if exist %temp%\SearchListing.txt del %temp%\SearchListing.txt
set /p str_search=What is the file to search for:
dir %1 /b /on /s > %temp%\Listing.txt
type %temp%\Listing.txt | find /i "%str_search%" > %temp%\SearchListing.txt
start notepad %temp%\SearchListing.txt
exit

Now to search, right click a folder and select the Search Directory from the menu.

 

Simple, Enjoy!


Edited by JohnnyJammer, 10 July 2017 - 08:42 PM.


#7 jwoods301

jwoods301

  • Members
  • 1,489 posts
  • OFFLINE
  •  
  • Gender:Male
  • Local time:01:19 AM

Posted 10 July 2017 - 11:18 PM

Good tutorial on Windows Batch Scripting...

 

https://steve-jansen.github.io/guides/windows-batch-scripting/



#8 britechguy

britechguy

    Been there, done that, got the T-shirt


  • Moderator
  • 9,008 posts
  • OFFLINE
  •  
  • Gender:Male
  • Location:Staunton, VA
  • Local time:05:19 AM

Posted 11 July 2017 - 12:38 PM

And as much as I love batch files, and I do, I am slowly transitioning to PowerShell.

 

I have yet to find a single Command Prompt command that has not been aliased under PowerShell using precisely the same syntax.  It's clear that Microsoft is deprecating Command Prompt and, long term, that means the writing's on the wall.

 

PowerShell as a whole is still well out of my comfort zone, but if I were starting from scratch in learning scripting it would be PowerShell, not Command Prompt.


Brian  AKA  Bri the Tech Guy (website in my user profile) - Windows 10 Home, 64-Bit, Version 1803, Build 17134 

     . . . the presumption of innocence, while essential in the legal realm, does not mean the elimination of common sense outside it.  The willing suspension of disbelief has its limits, or should.

    ~ Ruth Marcus,  November 10, 2017, in Washington Post article, Bannon is right: It’s no coincidence The Post broke the Moore story


 

 

 

              

 


#9 JohnnyJammer

JohnnyJammer

  • Members
  • 1,117 posts
  • OFFLINE
  •  
  • Gender:Male
  • Location:QLD Australia
  • Local time:07:19 PM

Posted 11 July 2017 - 04:39 PM

And as much as I love batch files, and I do, I am slowly transitioning to PowerShell.

 

I have yet to find a single Command Prompt command that has not been aliased under PowerShell using precisely the same syntax.  It's clear that Microsoft is deprecating Command Prompt and, long term, that means the writing's on the wall.

 

PowerShell as a whole is still well out of my comfort zone, but if I were starting from scratch in learning scripting it would be PowerShell, not Command Prompt.

Yeah i feel a little the same but you can utilise .NET framework with powershell and also combine batch and powershell together when piping.

 

Only thing i find is different versions of powershell from Win7 to Win10 and server08 up.

With batch files most commands have been around for donkey's years.

 

In saying that, that right click menu option on that script above is very handy and i also have a few more for printing directory etc straight to printer.






2 user(s) are reading this topic

0 members, 2 guests, 0 anonymous users