Searching for text in a file to discover the folder path

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Message
Author
nohj
Posts: 7
Joined: 04 Sep 2015 17:30

Searching for text in a file to discover the folder path

#1 Post by nohj » 04 Sep 2015 17:42

EDIT: To clarify what I'm looking to do is move a few files into a data folder of an application. The application is installed into Program Files but it keeps data inside app data. The folder name looks something like this

76053ADAJSQDUC4975

Problem is that it's unique to every instance of the application installed and for every computer that will be using this batch. So I'm in the directory and it has folders:

1AKDHCI4985HF55GHJKB
G5586HJFRUK56885KOQQ

The only way to identify the folders is by a .txt file inside each one called origin.txt

that shows the file path to the applications installation directory (C:/Program Files(x86)/********) on one line.

I figured I can use a for to loop through, find the the file, and read it. What I don't know how to do, is find the right file, and cd to its directory. The second problem is since this batch file will be used by multiple users, not all of their installation paths are the same. So inside origin.txt it could be C:/, D:/, Program Files or Program Files(x86) so the only thing useful to me is the last several words. How would I go about being selective like that.

I'm currently traveling so can't answer right away but would appreciate if you guys help me out or point me in the right directions. Thanks
Last edited by nohj on 06 Sep 2015 00:38, edited 1 time in total.

Squashman
Expert
Posts: 4486
Joined: 23 Dec 2011 13:59

Re: Newbie needs help

#2 Post by Squashman » 04 Sep 2015 19:04

Use the FINDSTR command within a FOR /F command.

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Newbie needs help

#3 Post by foxidrive » 04 Sep 2015 20:31

nohj wrote:Sorry for the crazy question, I don't necessarily need code but if you could point me in a direction that'd be awesome.


While reading your question I lost the plot.

Examples of what you are trying to find and locations you are searching in, would be useful to understand just what you want to do.

ShadowThief
Expert
Posts: 1166
Joined: 06 Sep 2013 21:28
Location: Virginia, United States

Re: Newbie needs help

#4 Post by ShadowThief » 04 Sep 2015 21:08

I thought I recognized this question - http://stackoverflow.com/questions/3239 ... -directory

We still need an example of the contents of the file, though.

nohj
Posts: 7
Joined: 04 Sep 2015 17:30

Re: Newbie needs help

#5 Post by nohj » 06 Sep 2015 00:34

Yeah I'm not great explaining things lol I've updated the stack flow question and I'll post it here as well.

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Newbie needs help

#6 Post by foxidrive » 06 Sep 2015 07:10

The app keeps a data folder in appdata\roaming with random foldernames - and origin.txt is inside them.

If that's right then this can help you:

Code: Select all

@echo off
set "folder="
for /r "%appdata%" %%a in (origin.txt*) do find "last words" <"%%a" >nul && set "folder=%%~dpa"
if defined folder echo keywords were found in at least once spot = "%folder%"
if not defined folder echo tough!
pause

nohj
Posts: 7
Joined: 04 Sep 2015 17:30

Re: Newbie needs help

#7 Post by nohj » 06 Sep 2015 11:37

foxidrive wrote:

Code: Select all

@echo off
set "folder="
for /r "%appdata%" %%a in (origin.txt*) do find "last words" <"%%a" >nul && set "folder=%%~dpa"
if defined folder echo keywords were found in at least once spot = "%folder%"
if not defined folder echo tough!
pause



Oh wow, I was looking into findstr but this looks perfect.
If I know more of the actual directory can I place them inside the quotations like so: "%appdata/Roaming/appFolder/Terminal/"?
Will the for loop also check sub-directories?
and the "last words" would be the string I'm looking for, and when it's found it places it makes the variable folder equal the filepath?
Sorry if it's extra newbie, I just glanced over batch terminology before I posted the question and this is the only other chance I've had to really be on the internet, so I'm still trying to grasp the everything. Thanks I'm going to be playing around with this for loop and researching find.

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Newbie needs help

#8 Post by foxidrive » 06 Sep 2015 20:39

nohj wrote:If I know more of the actual directory can I place them inside the quotations like so: "%appdata/Roaming/appFolder/Terminal/"?

Yes, but with the missing percent sign also, and backslashes in Windows.

"%appdata%\Roaming\appFolder\Terminal"
Will the for loop also check sub-directories?

Yes.
and the "last words" would be the string I'm looking for, and when it's found it places it makes the variable folder equal the filepath?

Yep. It finds the last match, if there is more than one.

It's case sensitive and adding the find switch /i makes the search ignore the case.

findstr can also be used with appropriate switches.

nohj
Posts: 7
Joined: 04 Sep 2015 17:30

Re: Searching for text in a file to discover the folder path

#9 Post by nohj » 10 Sep 2015 14:15

Wow that's awesome, it's exactly what I was looking for, definitely appreciate the help.

I have a new question in regards to this, can I use findstr to find every instance of the file

Code: Select all

origin.txt
?

So for example, instead of reading

Code: Select all

origin.txt
at all to find the correct directory, can it just for example: Find origin.txt, get the directory, move some files to directory, find the next origin.txt, get the second directory, move some files to directory, and repeat this until it doesn't find any more files named origin.txt?

That would literally be a smarter way to go about it.
Currently I'm letting the user select what version of the application they have installed, then the batch finds origin.txt, reads it to find what version it is, gets the directory, moves files to the directory.

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Searching for text in a file to discover the folder path

#10 Post by foxidrive » 10 Sep 2015 17:57

This should do what you are asking - merely copy files to every folder with origin.txt inside.

Code: Select all

@echo off
for /r "%appdata%" %%a in (origin.txt?) do (
   copy /b "c:\folder1\*.txt"  "%%~dpa"
   copy /b "c:\folder2\file2.ext"  "%%~dpa"
   copy /b "c:\folder3\file3.ext"  "%%~dpa"
)
pause

nohj
Posts: 7
Joined: 04 Sep 2015 17:30

Re: Searching for text in a file to discover the folder path

#11 Post by nohj » 11 Sep 2015 13:11

Appreciate the help, thats exactly what I'm looking for.

can I indicate a specific folder within the directory that origin.txt is inside like so:

Code: Select all

copy /y "c:\folder1\*.txt"  "%%~dpa\exampleFolder\subFolder\"


I'm unsure of the use of /b, what does "Indicates a Binary File" mean exactly?

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Searching for text in a file to discover the folder path

#12 Post by foxidrive » 11 Sep 2015 17:06

nohj wrote:can I indicate a specific folder within the directory that origin.txt is inside like so:


LOL! Tell us the exact task to begin with, please! See here: viewtopic.php?f=3&t=6108

I'm unsure of the use of /b, what does "Indicates a Binary File" mean exactly?


It means it copies the file exactly as it is.
In the old days a text file required an End Of File marker added to the end so there was a text copy/ascii mode.

nohj
Posts: 7
Joined: 04 Sep 2015 17:30

Re: Searching for text in a file to discover the folder path

#13 Post by nohj » 15 Sep 2015 11:12

Sorry about the obscurity, sometimes I don't know what I want haha

Just wanted to check if this was valid

Code: Select all

copy /y "c:\folder1\*.txt"  "%%~dpa\exampleFolder\subFolder\"


Because I didn't have access to my computer to test it myself, as I've been travelling.

This is what I'm working with so far:

Code: Select all

set "batchisin=%~dp0"
for /r "%appdata%" %%a in (origin.txt*) do (
   set "folder=%%~dpa"
   if defined folder echo = "%folder%"
   cd %batchisin%
   copy /y /b "%batchisin%\Test.ex4" "%folder%\FolderN\folder"
)
pause


Right now with echo on it shows

Code: Select all

set "folder=C:\The\Correct\File\Path\Here"

But as soon as I try to

Code: Select all

echo = "%folder%"
it's just blank.
So copy isn't finding the path.


nohj
Posts: 7
Joined: 04 Sep 2015 17:30

Re: Searching for text in a file to discover the folder path

#15 Post by nohj » 15 Sep 2015 12:43

Squashman wrote:You need to use delayed expansion.
http://www.robvanderwoude.com/variableexpansion.php



Just read through it, worked perfectly thanks Squashman

Post Reply