Win a copy of Java Database Connections & Transactions (e-book only) this week in the JDBC forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Devaka Cooray
  • Knute Snortum
  • Paul Clapham
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Bear Bibeault
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Ron McLeod
  • Piet Souris
  • Frits Walraven
Bartenders:
  • Ganesh Patekar
  • Tim Holloway
  • salvin francis

Running Windows cmd findstr within java app  RSS feed

 
David Huynh
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi everybody,

I'd like to use findstr to see if a string exists or not within one or multiple directories. I found that for example findstr /c:\"word\" /d:C:\test;C:\test1 *.txt works fine within command prompt (given folders are made and whatnot), but I was wondering how I could implement this within a java method in order to run this command within an app, then do whatever depending on the output returned. From my google search, I tried the following:



What I was given back in the console was "C::" I'm not exactly sure what this means, but like I said the command does work in command prompt so I was expecting the same return value. I have also tried using process builder in order to do this, but as expected it gave me the same output.

Perhaps I did something wrong for the param of the exec method? Really unsure. Any help would be appreciated.

Thanks!
 
Joanne Neal
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
\ is a special character in Java strings which is used to escape other characaters - one of the characters it can be used to escape is a 't', \t in a Java string actually represenst a tab
" is also a special character (it marks the beginning and end of of a string literal - if you want an actual " in your string you need to escape it with a \.
So the actual command you are running is
findstr /c"word" /d:C:<tab>est;C:<tab>est1 *.txt
which is probably why you are getting strange results.
Try
 
Wouter Oet
Bartender
Posts: 2700
IntelliJ IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I would advise you to read this article. Is a bit old but still very relevant.
 
David Huynh
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ah, I see what I did wrong now. Working nicely now, thanks a lot. Will also take a look at the article.
 
Campbell Ritchie
Marshal
Posts: 64496
225
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
. . . and welcome to the Ranch
 
Don't touch me. And dont' touch this tiny ad:
how do I do my own kindle-like thing - without amazon
https://coderanch.com/t/711421/engineering/kindle-amazon
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!