Modify text file using batch commands

April 15, 2010 at 11:05:54
Specs: Windows 7
Hello all I have a program that uses a file config.ini for storing data locations , I carry it over in a pen drive , and i need to manually change the drive letter everytime i plug it in a new computer, I am not able to work out commands for editing the file :

The lines i wish to alter are as

text
--------
;;Specify location of Data files
Data=G:\Prog1\
---------
text

I wish to change G: with the path of the pen drive ..

Any help pls...........


See More: Modify text file using batch commands

Report •


#1
April 16, 2010 at 03:10:19
Do you have to change the driveletter ? Alternatives are :

- forcing a given hardware device to always use the same driveletter (if free), if you select a "high" letter (X, Y, R, ...) that would work
- add another letter, for example, Pen Drive is setup as either G or F, add one more (high letter) that actually is pointing to G or F or ...


Report •

#2
April 16, 2010 at 03:39:48
Setting up the drive letter high woulfd work , But then also for my information what should be the command if i wish to change the drive letter automatically when i plug it into a computer........

Report •

#3
April 16, 2010 at 05:40:34
I don't know how you'd force a drive letter. This script may be able to find it. If your USB is labeled "MYDATA":

============================
@echo off & setLocal EnableDELAYedeXpansion

set L=
for %%a in (f g h) do (
vol %%a: 2> nul | find "MYDATA" > nul
if not errorlevel 1 set L=%%a
)

if defined L (
echo drive letter is !L!
) else (
echo not found
)


=====================================
Helping others achieve escape felicity

M2


Report •

Related Solutions

#4
April 16, 2010 at 05:46:19
This may be a stupid question.....

Have you tried to just specify a relative path like:

data=\prog1\

It may not work but it's worth a try......


Report •

#5
April 16, 2010 at 08:41:03
Setting the driveletter to a given fixed number, is a Windows setting. Prolly cannot be done with command line (if it can be done already), but you should only do once.

Report •

#6
April 16, 2010 at 09:04:39
force drive letter from command line- not a good idea, and setting a relative path doesnt work , ..

It would be nice if we revert back to the main question...........


Report •

#7
April 16, 2010 at 10:13:26
So you need to modify this line in config.ini, right?

Data=G:\Prog1\

Post the config.ini at least up to a couple lines past the line to be changed.


=====================================
Helping others achieve escape felicity

M2


Report •

#8
April 17, 2010 at 02:59:06
Here's the full file :


;; LicensePath = Location of License file
License Path=C:\Prog1

;;Specify location of Data files
Data=C:\Prog1\Data

;;Specify list of companies to preload,
;; ** TO STOP DEFAULT LOADING set the following line to NO
Default Companies=Yes

;;Specify list of TDL files for user defined reports and changes,
;; ** TO STOP LOADING TDL FILES, set the following line to NO
User TDL=Yes

;;Specify location of TALLYSAV.tsf (saved configuration of Tally)
Config=C:\Prog1

;;Specify location of Language Files
LangPath=C:\Prog1

;;Specify location where files are created during export by default
Export Path=C:\Prog1

;;Specify Color details of local monitor
Color=Yes
Sound=Yes

Client Server=None
ServerPort=9000
Enable ODBC Server=Yes
Load Buffering=Yes
Ignore Tcp Timeout=Yes

Width=1366
Height=728

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

Here i wish to change every path to the current drive letter at the time of inserting the pen drive into a new computer. .


Report •

#9
April 17, 2010 at 08:31:45
@echo off > newfile & setLocal enableDELAYedeXpansion

set L=g:

for /f "tokens=1* delims=[]" %%a in ('find /v /n "" ^< config.ini') do (
set S=%%b
echo !S! | find "C:" > nul
  if not errorlevel 1 (
    set S=!S:C:=%L%!
  )
>> newfile echo.!S!
)


=====================================
Helping others achieve escape felicity

M2


Report •

#10
April 17, 2010 at 08:45:45
That will only work for C: , But in actual it will be varying every time.....

Report •

#11
April 17, 2010 at 08:54:03
I thing changing find to "=*:" , here * is to be used as a wildcard , not sure how to implement that.....

Report •

#12
April 17, 2010 at 10:58:28
@Judago: i guess the script is running from the host's hard drive and not from the usb drive. Although i don't see why the script couldn't be placed onto the usb and run from there.
I think M2 had the right idea way back there: look for the volume-name or serial number.
first find out the serial# of your usb drive:
(insert drive, figure out it's letter "x:", then VOL x)
then put the serial# into line two of this script:
(f/e: set usb=236A-12E4)
@echo off & setlocal
set usb=
for %%a in (d e f g h i j k l m n o p q r s t u v w x y z) do (
for /f "tokens=*" %%b in ('vol %%a: 2^>nul ^| find /i "volume serial number is %usb%" 2^>nul') do (
echo %%a: %%b
set usbdrive=%%a
goto :xit
)
)
:xit
echo usbdrive: %xx%
if "%usbdrive%" equ "" goto :eof
::------ end
then put the script at the top of M2's script,
(replacing the line: "set L=g:") then replace all
C: with %usbdrive%: everywhere else in the script.
EDIT oops, forgot this was working with config.ini and not a script. edited to specs

Report •

#13
April 18, 2010 at 05:29:14
Hey , Judago's idea of using realtive paths worked, Thanks man....

But Still for my info , how will i implement thye task if i am running it from the pen drive and NOT from the host drive...


Report •

#14
April 19, 2010 at 02:10:20
By means of test, I've put on of my USB drives as a higher letter (instead of the next free letter) once, and he keeps using that one. It works, but it's not the best solution.

OK, a decent solution.

You know that each drive gets a unique key given, on format or burning ?

Example (fixed drive, but non-fixed drives like USB drives and sticks, and CD's/DVD's/etc work the same):

D:\>dir
Volume in drive D is Program and Data
Volume Serial Number is C8BE-1EC5

Directory of D:\
(..)


You see this "Serial Number" ? It's a fixed number, and sort of unique (assume it is unique, but "only" 99.999999% certain). Well, you can hardcode that number, and you'll be able to recognize it. Remember it may change on FORMAT.


Report •

#15
April 19, 2010 at 02:13:48
> Hey , Judago's idea of using realtive paths worked,
> Thanks man....
>
> But Still for my info , how will i implement thye task if i
> am running it from the pen drive and NOT from the host
> drive...
>

You mean the other way round ? A relative path excluding the driveletter only works from the drive in question, not from any other drive


Report •

#16
April 19, 2010 at 04:16:43
Sorry , If any of you got it wrong,but the program and the script both resides in the pen drive and NOT at the host drive....

Report •

#17
April 19, 2010 at 09:31:46
tarung1793: the program and the script both resides in the pen drive
Well that makes finding the pen drive's letter easy:
@echo %~d0

Seeing as this could easily be solved with a regular expression, isn't there a one-liner GAWK he could use? Anyone? (The alternative is for me to write a VBScript, and that'd be a lot more than one line.)


Report •

#18
April 19, 2010 at 10:16:09
#3 shows how to get the letter and #9 shows how to alter the ini.

Which is where we started.

If the ini is not what OP posted, oh well.

tvc,

The reason I suggest using the label is it can easily be changed [with LABEL surprise] unlike the serial, which would probably involve editing absolute sectors.

OBW, unique means "unlike any other" so there are no degrees of unique. A thing is either unique or it isn't.


=====================================
Helping others achieve escape felicity

M2


Report •

#19
April 19, 2010 at 10:46:32
#9 shows how to alter the ini.
#9 shows him how to modify the ini file if it's C:\. On the second time he runs the script, it will no longer be C:\. It'll be the new (now possibly incorrect) drive letter.

Report •

#20
April 19, 2010 at 11:10:52
yeah, i went with the serial# on the off chance that the volume label might be duplicated, but there's no guarantee of the serial# either. you could use both for a pretty good bet.
for /f "tokens=2 delims=s" %%a in ('vol') do set xx=!xx!%%a

Report •

#21
April 19, 2010 at 11:28:25
Hi R2,

Yep. Smart money would start with:

copy config.sav config.ini

Cuts way down on the guesswork.

nbrane,

yep. But label will use 11 alpha/num chars plus some punctuation. Shouldn't be too tough to make a label which is very unlikely to be a dup.


=====================================
Helping others achieve escape felicity

M2


Report •

#22
April 19, 2010 at 11:40:49
copy config.sav config.ini
The problem with this is it assumes the configuration is static. What if the user resizes the window? (Apperently saved in the ini file.) It would revert whenever the user ran the script.

EDIT: Someone play with this:

findstr /i ".*=[a-z]:\\ " config.ini


Report •

#23
April 19, 2010 at 11:49:57
R2,

Well, if we're not trying to alter a 'given' ini, then I guess I lost the plot.

Happens all too often these days.


=====================================
Helping others achieve escape felicity

M2


Report •

#24
April 19, 2010 at 12:51:40
I imagine he has some program on his USB drive that loads a configuration file. In this file is the full paths to the important stuff. Since it's installed on a USB drive, the drive letter changes. I imagine this because I've run into the same issue.

Scripting the config changes is something I meant to do, but never really got around to it. (The annoyance factor just isn't high enough yet.) If I did, it might look something like this:

del c 2>NUL
for /f "usebackq tokens=1* delims==" %%A in ("config.ini") do @(
  if /i "%%~dB"=="%~d0" (echo %%A=%%B) else echo %%A=%~d0%%~pnxB) >> c
move c config.ini


Report •

#25
April 19, 2010 at 13:11:38
Yeah, you got yer square to-its and round to-its.

Always good if you can get around to it.

:(


=====================================
Helping others achieve escape felicity

M2


Report •

#26
April 19, 2010 at 23:28:09
Anyone on #11........

Report •

#27
April 20, 2010 at 05:30:24
I'm still waiting on a response from you about #24.

Report •

#28
April 21, 2010 at 02:11:50
@Razor2.3 : Thanks, It works brilliantly . though it trimmed the file to bare essentials, i mean after running the batch file form E: drive
the config file changed to:

License Path=E:\Prog1
Data=E:\Prog1\Data
Default Companies=Yes
User TDL=Yes
Config=E:\Prog1
LangPath=E:\Prog1
Export Path=E:\Prog1
Color=Yes
Sound=Yes
Client Server=None
ServerPort=9000
Enable ODBC Server=Yes
Load Buffering=Yes
Ignore Tcp Timeout=Yes
Width=1366
Height=728

----------------------------------------------
But it did worked.
----------------------------------------------------------------------

I know some C++, but i was used to TC++, which didnt worked on my new win 7 x64. that's why i decided to turn up to batch file but now i hav also worked up with C++, now i use code block with borland C++ 5.5 compiler. So i decided to write a C++ program that can do my job.And it worked. here is the code :
-------------------------------------------------------------
#include<iostream.h>
#include<conio.h>
#include<fstream.h>
#include<ctype.h>
char curr()
{
char a=3;
char b[13]="A:\\gh.test";
char b1[13]="A:\\gaf.tst";
ifstream fin,fin1;

for(char ch='A';ch<='Z';ch++)
{
b[0]=b1[0]=ch;
fin.open(b,ios::nocreate);
fin1.open(b1,ios::nocreate);
if(fin1 && fin)
return ch;
fin.close();
fin1.close();}

return a;
}

void main()
{char c=curr();
fstream f1("config.ini");
fstream f2("temp.ini");
char b[80];int i;

while(f1)
{ f1.getline(b,80);
for (i=0;b[i]!='\0';i++)

{
if (b[i]=='=' && isalpha(b[i+1]) && b[i+2]==':')
{b[i+1]=c;
cout<<b;
}}
f2<<"\n";};
f1.close();
f2.close();
remove("config.ini");
rename("temp.ini","config.ini");
}


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

It keeps the file intact , just changing what i require.

I know this code is a lot longer than razor's . I don't know the direct way of detecting the drive letter in C++ , So i end up in making a custom function curr(), that detects two user placed files in the drive...

any cut-short in the above program is welcome.......

Btw thank to all you guys for your replies.....


Report •


Ask Question