-1

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

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 .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

Nohj
  • 1
  • 4
  • If it's not the last line specifically then a recursive `findstr /m /s /r /c:"word1 word2$" *.txt` can be used to build the list of directories, otherwise you'd have to manually get the last line, then get the last two tokens (there are solutions on stackoverflow for that). – wOxxOm Sep 04 '15 at 07:58

2 Answers2

0

First, if the application is installed with an installer which adds something to Windows registry for knowing itself on next run (update/upgrade/uninstall) which version of the application is already installed and into which directory if installed at all, it is better to evaluate this information in registry instead of searching around in file system. See my answer on Locate if the program exists and set a custom variable in batch windows.

Second, with assuming text file origin.txt can be found anywhere within application data directory of current user containing only once (or last) the string :\ as part of the directory with the program files of the application, the following batch code could be used to get the directory path and make it the current directory.

@echo off
set "ApplicationDirectory="
for /F "delims=" %%I in ('dir /A-D /B /S "%APPDATA%\origin.txt" 2^>nul') do (
    for /F "delims=" %%D in ('%SystemRoot%\System32\findstr.exe /C:":\\" "%%~I" 2^>nul' ) do (
        if exist "%%~D" (
            set "ApplicationDirectory=%%~D"
            goto ChangeDirectory
        )
    )
)
echo Could not determine directory of application.
goto :EOF

:ChangeDirectory
cd /D "%ApplicationDirectory%"
echo Directory is: %ApplicationDirectory%
set "ApplicationDirectory="

The first FOR loop uses command DIR to search in all subdirectories of user related application data directory for the file origin.txt. The inner FOR loop is processed if there is at least one such file found.

The inner FOR loop calls FINDSTR to search in found origin.txt for all lines containing the literal string :\ and processes all found lines.

For each line returned by FINDSTR the inner FOR loop checks if there is a directory (or file) using the entire string of the line containing :\.

Both loops are exited immediately on a positive check on existence of directory (or file) with a jump to label ChangeDirectory with the commands to switch the current directory independent on current drive and echoing the found application directory.

For understanding the used commands and how they work, open a command prompt window, execute there the following commands, and read entirely all help pages displayed for each command very carefully.

  • cd /?
  • dir /?
  • findstr /?
  • for /?
  • goto /?
  • if /?
  • set /?

Note: I strongly recommend to run FINDSTR with a better search string or perhaps even a regular expression than just :\ if file origin.txt could contain multiple paths.

Community
  • 1
  • 1
Mofi
  • 46,139
  • 17
  • 80
  • 143
  • Thanks Mofi! You really helped a lot, especially with your explanation and I'm looking through the help for each command now trying to grasp what's going on better. – Nohj Sep 06 '15 at 18:55
  • I did encounter a problem where it's not finding the directory, I've changed the findstr to a few things with the same result, and I tried tweaking the dir %APPDATA%\Roaming\Etc\origin.txt to see if I can place it in a closer directory but I don't know if that's valid. Either way with it changed its the same outcome – Nohj Sep 06 '15 at 19:12
  • @Nohj Edit your question and show us the entire content of `origin.txt` if not too large and which line should be found and we might suggest the right string or regular expression for finding the line of interest. – Mofi Sep 07 '15 at 05:28
0

I ended up using

set "ApplicationDirectory="
for /r "%appdata%" %%a in (origin.txt*) do find "thewordsiwaslookingfor" <"%%a" >nul && set "ApplicationDirectory=%%~dpa"

Then I used ApplicationDirectory to be able to move some files into that directory.

Which worked perfectly, for what I was trying to do.

Nohj
  • 1
  • 4