Re: DOS Batch File Question

  • From: "Donald Marang" <donald.marang@xxxxxxxxx>
  • To: <programmingblind@xxxxxxxxxxxxx>
  • Date: Sat, 3 Jul 2010 01:34:53 -0400

I finally found the main hurdle with this batch file. It was not the syntax of the findstr command getting flagged. It was the rename command! I had not realized that it would not allow a fulll path for both the source and destination, even if they are the same! I am not sure there are not still lingering compatibility issues, but there was a more straight forward answer. Here is the working solution.


@echo off
:: Make a backup
copy %AppData%\VMware\preferences.ini %AppData%\VMware\preferences.bak > nul
findstr /V "hotkey" %AppData%\VMware\preferences.ini > %AppData%\VMware\temp.txt
erase %AppData%\VMware\preferences.ini
ren %AppData%\VMware\temp.txt preferences.ini
:: Append desired hotkey definition
echo pref.hotkey.shift="true" >> %AppData%\VMware\preferences.ini


Don Marang

--------------------------------------------------
From: "Martin Slack" <m.g.slack@xxxxxxxxxxxx>
Sent: Thursday, July 01, 2010 2:59 PM
To: <programmingblind@xxxxxxxxxxxxx>
Subject: Re: DOS Batch File Question

This is the batch file I use (called remove.bat) to remove a line which matches a string command line parameter:

@echo off
findstr /I /L /V "%1" phones.txt > temp
erase phones.txt
ren temp phones.txt

So I call remove testString from a command line which can see the batch file. I usually don't include spaces in the testString, but surrounding the parameter with double quotes might get that to work.

phones.txt is my target file. You might want that filename to be a parameter too.

There is absolutely no error checking here, so I usually run a non-inverted findstr command first that just prints out the matches. My goal is always to fiddle the testString to give only one match, since I only want to remove one line at a time. If I get more than one match, I try again.

It would definitely be safer to include the checking stage in the same batch file, so something like this might be called for:

@echo off
findstr /I /L "%1" phones.txt
set reply=""
set /p reply=Continue? (y/n):
if "%reply%"=="n" (exit /b)
findstr /I /L /V "%1" phones.txt > temp
erase phones.txt
ren temp phones.txt

This seems to work (with about five minutes of checking. Let me know how you get on.

 Martin



----- Original Message ----- From: "Donald Marang" <donald.marang@xxxxxxxxx>
To: <programmingblind@xxxxxxxxxxxxx>
Sent: Thursday, July 01, 2010 2:01 PM
Subject: Re: DOS Batch File Question


I would appreciate looking at your code. I started putting that exact approach together late last night and could not quite get it to work.

I also have a ugly solution using a for /f statement combined with a find statement and the ancient edlin commands. The edlin command only works with short filenames!

Don Marang

--------------------------------------------------
From: "Martin Slack" <m.g.slack@xxxxxxxxxxxx>
Sent: Thursday, July 01, 2010 2:23 AM
To: <programmingblind@xxxxxxxxxxxxx>
Subject: Re: DOS Batch File Question

Hi Don,

To do what you want in a DOS batch file, you need to use find or findstr to pull out the lines you want to remove. Those functions have an option to invert the selection so you can redirect the output, minus the chosen lines, into a temporary file. Then append the new line to that file, and finally rename it to overwrite the original file (or not) as you prefer.

I'm doing something like this myself. Let me know if you need code samples and I'll dig them out.

 Martin


----- Original Message ----- From: "Donald Marang" <donald.marang@xxxxxxxxx>
To: <programmingblind@xxxxxxxxxxxxx>
Sent: Thursday, July 01, 2010 4:05 AM
Subject: DOS Batch File Question


I was attempting to help someone write a batch file to help configure VMware Player to use a preconfigured Vinux Virtual Machine. We want to modify the %AppData%\VMware\preferences.ini file to set the hotkey combination to Alt + Control + Shift. To do this, I need the batch file to do two things.

1. Delete any lines that contain the word "hotkey" that are currently in the file. Two definitions are not permitted in this file. There are other similar lines that appear in this file, but only this line seems to be processed in the newest versions of the software.

2. Append a line at the bottom of the file to define the desired hotkey with the command:
echo 'pref.hotkey.shift="true"' >> %AppData%\VMware\preferences.ini

Obviously, I know how to get the second task done. Can anybody help me with the first? We would need it to work in XP, Vista and Windows 7. In Linux, I think a simple sed statement would do the trick, like
sed 'hotkey/d' ${AppData}/VMware/preferences.ini

I am just guessing at the above hypothetical syntax as well. I have not attempted anything like that in years.Does anybody know how to modify text files in DOS? I ran across 'munge', which could have accomplished this as long as an additional definitions file was used. I use the past tense since it has not been around for a long time. There must be some such capability to search and destroy! The 'FIND' command can locate the lines. I want them deleted as well.

Don Marang
__________
View the list's information and change your settings at //www.freelists.org/list/programmingblind

__________
View the list's information and change your settings at //www.freelists.org/list/programmingblind


__________
View the list's information and change your settings at //www.freelists.org/list/programmingblind

__________
View the list's information and change your settings at //www.freelists.org/list/programmingblind

Other related posts: