Searching for text in a file to discover the folder path
Moderator: DosItHelp
Searching for text in a file to discover the folder path
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
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.
Re: Newbie needs help
Use the FINDSTR command within a FOR /F command.
Re: Newbie needs help
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.
-
- Expert
- Posts: 1166
- Joined: 06 Sep 2013 21:28
- Location: Virginia, United States
Re: Newbie needs help
I thought I recognized this question - http://stackoverflow.com/questions/3239 ... -directory
We still need an example of the contents of the file, though.
We still need an example of the contents of the file, though.
Re: Newbie needs help
Yeah I'm not great explaining things lol I've updated the stack flow question and I'll post it here as well.
Re: Newbie needs help
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:
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
Re: Newbie needs help
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.
Re: Newbie needs help
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.
Re: Searching for text in a file to discover the folder path
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?
So for example, instead of reading 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.
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
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.
Re: Searching for text in a file to discover the folder path
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
Re: Searching for text in a file to discover the folder path
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:
I'm unsure of the use of /b, what does "Indicates a Binary File" mean exactly?
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?
Re: Searching for text in a file to discover the folder path
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.
Re: Searching for text in a file to discover the folder path
Sorry about the obscurity, sometimes I don't know what I want haha
Just wanted to check if this was valid
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:
Right now with echo on it shows
But as soon as I try to it's just blank.
So copy isn't finding the path.
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%"
So copy isn't finding the path.
Re: Searching for text in a file to discover the folder path
You need to use delayed expansion.
http://www.robvanderwoude.com/variableexpansion.php
http://www.robvanderwoude.com/variableexpansion.php
Re: Searching for text in a file to discover the folder path
Squashman wrote:You need to use delayed expansion.
http://www.robvanderwoude.com/variableexpansion.php
Just read through it, worked perfectly thanks Squashman