Open a file with unknown parent folder

Microsoft Windows xp professional w/serv...
December 21, 2009 at 11:54:34
Specs: Windows XP
Hi,

I need help with a script opening a .CFG file with notepad even if notepad is not the default program. I also need this to open without knowing the parent folder. This is what I have

@echo off
echo.

start C:\"Program Files"\needhelp\1234\aero.cfg

I need a script that will open the .CFG with notepad no matter where the .BAT file is started from. Example:


@echo off
echo.

start C:\????????\needhelp\1234\aero.cfg

Basically this program is installed to Program Files in 2000/XP but to the root of C:\ in Vista/7 and I need to open that file in either environment and with notepad. I don't want to be prompted to select a program to open with.

Thanks!!!!!


See More: Open a file with unknown parent folder

Report •


#1
December 21, 2009 at 12:24:05
maybe:

set pth=C:\Program Files\needhelp\1234
if exist c:\aero.cfg set pth=c:
start notepad %pth%\aero.cfg

(edited to reflect the problem with the quotes)


Report •

#2
December 21, 2009 at 13:01:16
I tried that but it didn't open. Got a message "The filename, directory name, or volume label syntax is incorrect" and it opens a blank notepad.

I tried opening the .bat from the root of C:\, the desktop, and within the directory where the file exists.

My biggest boundary is making that .CFG file open no matter where you have the .BAT located. My script from the original post will open that file but only if the .BAT is in the same directory. I really don't mind if this is a .BAT or a .VBS script, I just need it to do what I said above. Thanks!!


Report •

#3
December 21, 2009 at 13:58:38
some dithering around discovered that notepad does not like quotes in cmdline! how idiotic...

if you can eliminate the quotes around "program files" it might open the file:
notepad c:\program files\needhelp\1234

it worked over here...


Report •

Related Solutions

#4
December 21, 2009 at 14:47:07
I am going to try it on a few different PCs cause this one only has XP. It worked great though on this one!!!

The only thing is on PC's with Vista/7 the program needs to be installed to the root of the C:\. This bypasses the protection on program files so the CFG can be modified on its own. How would I make this script open it without looking at program files? That way I don't have to make two batch files, the end user can just run this one and it will work no matter what O/S they have.


Report •

#5
December 21, 2009 at 15:12:46
I think the generally accepted "correct" place to put a config file
is in "%APPDATA%\CompanyName\filename". That way you
can use the same batch file for all operating systems. It also
means it becomes a per-user setting.

Report •

#6
December 21, 2009 at 15:32:15
I agree but this program puts it in XP at

c:\ program files\Company name\program title\folder called local data\File.cfg

In Vista/7 it is installed to

c:\Company name\program title\folder called local data\File.cfg

Instead of writing the developers of that software I was hoping to compile a script that would open this without directing it to look in Program Files and just look at the sub folders. I thought this would be possible with .bat or .vbs script. Seemed easier and quicker than asking for a new release of the program, although it wouldn't be a bad suggestion for the future. I need this to work now though not in a year when they update :)

Thanks!!


Report •

#7
December 22, 2009 at 00:39:42
some dithering around discovered that notepad does not
like quotes in cmdline!

Notepad does take quotes it's just that is technically an invalid path.

C:\"Program Files"\needhelp\1234\aero.cfg

The correct path would be:

"C:\Program Files\needhelp\1234\aero.cfg"

Notepad accepts this fine on my end.

Start also rejects such paths and has a little quirk with
arguments. If the first arhument is double quoted it assumes
it is a title, the work around is like so:

start "" "q:\uoted\path\to\fil.e"


********************************************************

What about something like this:

if exist "%programfiles%\Company name\program title\folder called local data\File.cfg" (
    start notepad "%programfiles%\Company name\program title\folder called local data\File.cfg"
) else (
    if exist "%systemdrive%\Company name\program title\folder called local data\File.cfg" (
        start notepad "%systemdrive%\Company name\program title\folder called local data\File.cfg"
    ) else (
        echo file.cfg could not be found!
        pause
    )
)


Batch Variable how to


Report •

#8
December 22, 2009 at 10:10:13
Thanks!! Yeah, I think that will work fine for me! I tried it and it worked no matter where it was launched in XP so I'll test Vista/7 to see if it picks up the CFG file when its at the root drive, but it should be the same. I added some VBS to the end to have a message box with inital instructions for the end user as well. All tested and working flawless!! Thanks to everyone that replied and this awesome forum!

Report •

#9
December 22, 2009 at 11:47:56
Buick, for as far as this is still an issue, if you want to open a file, example C:\dir1\dir2\myfile.txt with notepad, do this:

dir C:\notepad.* /s /b

This may result in some lines, take out the executable one (EXE, COM, or even BAT or CMD), and use its full path in a script, example:

=====================
@echo off

start "C:\WINDOWS\notepad.exe" "C:\dir1\dir2\myfile.txt"
=====================

Only use double quotes (") when at least 1 adjacent character is a whitespace. In the above, you don't need to have them actually, but this solution works for all paths, the ones with whitespaces, and the ones without.


Report •

#10
December 22, 2009 at 12:51:52
Well I tried this script:

if exist "%programfiles%\Company name\program title\folder called local data\File.cfg" (
start notepad "%programfiles%\Company name\program title\folder called local data\File.cfg"
) else (
if exist "%systemdrive%\Company name\program title\folder called local data\File.cfg" (
start notepad "%systemdrive%\Company name\program title\folder called local data\File.cfg"
) else (
echo file.cfg could not be found!
pause
)
)

----------------------------------------------------------------------------------

It doesn't appear to find the file if it is not located in program files. I tried launching the batch from the desktop and from my flash drive.

It works great with the program installed to program files but any other directory it won't find the file.
------------------------------------------------------------------------------------

I also would like for it to make a backup of the CFG before it opens. The backup can go to the same folder with a different filename. Would I use xcopy for that??


Report •

#11
December 22, 2009 at 16:21:32
So there is no fixed location? Can it be any file with that
name on any drive? If so this will search every drive an
every folder for file.cfg.

It will take a little while to search the drives, you can
remove letters or move them to the front of the alphabet
to give them higher priority.

As for the backup since it's just one file plain old copy
should do. I have put a set file name into the script,
you could use "set /p" if you want just be aware that
validating the input will be a pain in the neck.

@ECHO OFF
set fn=backup.cfg
for %%a in (a b c d e f g h i j k l m o p q r s t u v w x y z) do (
    for /f "delims=" %%b in ('2^>nul dir /s/b %%a:\file.cfg') do (
        copy /-y "%%b" "%%~dpb%fn%"
        start notepad "%%b"
        goto :eof
    )
)
echo file.cfg not found!
pause


Batch Variable how to


Report •

#12
December 22, 2009 at 16:26:28
Ok everyone I got my script to backup and pull the CFG file from ANY location. All the help here made it possible. Thanks and I have posted a new thread on another question related to this script but thought I'd start fresh for that question. :)


Thanks!!


Report •

#13
December 23, 2009 at 04:30:04
Good you got it solved,

Just a side note, but when I see this code, I'm getting headache:

if exist "%systemdrive%\Company name\program title\folder called local data\File.cfg" (
start notepad "%systemdrive%\Company name\program title\folder called local data\File.cfg"
)

The length of the filename is making the code very unreadable. It may be down to personal preference, but look at this code, easier to read, easier to modify:

set cfgfile=%systemdrive%\Company name\program title\folder called local data\File.cfg

if exist "%cfgfile%" (
start notepad "%cfgfile%"
)


Report •

#14
December 23, 2009 at 12:59:19
Yeah I tried your last script there TVC and it worked good too. It looks cleaner. Well thanks everyone as this is SO helpful!

Report •

#15
December 23, 2009 at 22:56:35
@tvc, thanks man. i was wondering when someone would get around to suggesting that. The "whitespaces" (which i deplore under any circumstances) seem to be the problem about half the time (or more, not quantified). someones posts "i have directory A and file B", you try to fix, then you find out directory A is mapped to server: \\nigit whitespace\are you here?\no whitespace i'm not

and file B is: this file will really f_you_up<watchout for those special characters!? " gotcha!!!

sorry, I ALWAYS use dbl quotes (if i'm operating unknowns and i'm at least semi-conscious) around paths, but you still run into wierdisms like:
set /p xx=enter comnent:
user puts "I liked it! see myspace post --> file.a"
then your script innocently says:
echo %xx% user are you ready to send comment?
ya know what happens? if you have a file "user", it will get wiped just from echoing %xx%.
(setlocal will prevent this).

merry ChristMass or whatever works (PC insert) this season.


Report •

#16
December 25, 2009 at 06:06:23
But if would use that ECHO command with the bad variable content in the same script, how is SETLOCAL going to prevent that ? The variable stays defined until you undefine it, and because you do an ECHO with it, surely you are still having the content, thus also the problem ?!

Special characters indeed are a pain .. but, that's why we have a job, I guess.


Report •

#17
December 26, 2009 at 01:07:50
i was thinking about the same problem: what status of expansion, and wrote a simple test:
call :testexp

:testexp
set OFF=ON
echo [!OFF!]
:: if enabled, output: ON if not enabled, output: !OFF!
echo enabled is !OFF!
goto :eof
i might have goofed betwixt, but that worked last time i looked. it could be made into a loop to "decommission" all levels of "sets" by comparing and looping, doing "endlocal"
each loop, or just do: for /L %%a in (1 1 32) do endlocal
thus clearing the whole arena, instead of testing.
see other post response, my experience, pls comment
if you see i'm wrong! i want to learn about this stuff...

http://www.computing.net/answers/pr...

although I'm increasingly favored to using QBASIC or
VisBasic, SO MUCH simpler. but i know every user has
his own comfort level. Thank God for diversity!


Report •

#18
December 26, 2009 at 08:05:51
I have no clue what you are saying

Report •

#19
December 26, 2009 at 12:12:38
"(setlocal will prevent this)."

sorry, that was very UNclear, what i meant was
setlocal
WITHOUT enabledelayedexpansion, or:
SETLOCAL DISABLEDELAYEDEXPANSION
if you will. the delayedexpansion enabled is what causes echo to do things like removal of strings between exclms,
for example. sometimes you want en.delexp but sometimes
you very much don't want it, i've learned.


Report •

#20
December 27, 2009 at 05:08:11
I can't seem to reproduce that issue, in all of the below cases, the redirection occurs. I don't see how setting any setting prevents it ...


C:\>type test.cmd
@echo off
setlocal enabledelayedexpansion

set /p p_var1=

echo %p_var1% user

C:\>test
this is a test > yes

C:\>dir yes
Volume in drive C is OS
Volume Serial Number is 6C06-72AF

Directory of C:\

27/12/2009 14:05 23 yes
1 File(s) 23 bytes
0 Dir(s) 58.520.989.696 bytes free

C:\>type yes
this is a test user

C:\>type test.cmd
@echo off

set /p p_var1=

echo %p_var1% user

C:\>test
this is a test > yes

C:\>dir yes
Volume in drive C is OS
Volume Serial Number is 6C06-72AF

Directory of C:\

27/12/2009 14:06 23 yes
1 File(s) 23 bytes
0 Dir(s) 58.520.989.696 bytes free

C:\>type yes
this is a test user

C:\>type test.cmd
@echo off

SETLOCAL DISABLEDELAYEDEXPANSION

set /p p_var1=

echo %p_var1% user

C:\>test
this is a test > yes

C:\>dir yes
Volume in drive C is OS
Volume Serial Number is 6C06-72AF

Directory of C:\

27/12/2009 14:07 23 yes
1 File(s) 23 bytes
0 Dir(s) 58.520.989.696 bytes free

C:\>type yes
this is a test user

C:\>


Report •

#21
December 27, 2009 at 11:26:58
you're right, enable/disable won't affect user input at the "set /p". i'm still learning all these behaviors of batch.
in fact, mainly what "enabled" affects is only exclamations
in an inputfile stream. The other char.s (<, >, | etc) are handled ok, but the exclms will not be handled with enabled set, so to correctly handle an inputfile that has a possibility of an exclm., it should be off. to see, make a file:
testing
<data>!this will disappear when enabledelayed is on!</data>
test#2!even just one exclm
then run the file through a simple batch reader with enab on
and then with enab off and you will see.
for /f "tokens=* delims=" %%a in (test) do (
echo %%a)
I've decided, since any file could contain exclms, it is best, when in doubt, to assume the worst and code for it
(a pita, i must say!)
That's what i meant the other day when i said i was leaning more toward my previous means of handling certain processes using QBasic or VisuaBasic. Batch is made to do certain things well, and it can be stretched to do a lot of things with a lot of effort and patience, but it depends how much time you want to spend/waste getting batch to jump through a hoop that QB, visbasic, gawk etc. doesn't even hesitate at! Right now, I'm trying to learn the batch environment & script techniques, so it's still "fun" to attempt to do certain things.

But then, when you suggest another platform,
(QB, VB, VBscript, Jscript, gnu utilities...)
you get into the "third party" debate which i don't EVEN want to open up... I use debug and QBasic to write executables, and debug is more or less native to MSoft, but my executables i guess would be considered "third party" regardless.
anyway, have a good newyears


Report •

#22
December 27, 2009 at 13:24:32
As you are finding out, Windows batch scripting is very quirky. It may be worth reading Judago's How-To, which covers variable expansion in great detail and points out many of the pitfalls:
http://www.computing.net/howtos/sho...

Report •

#23
December 27, 2009 at 13:45:56
Thanks for the info, interesting reading all of this

Report •


Ask Question