View previous topic :: View next topic |
Author |
Message |
Privateer Pocket GPS Moderator
Joined: 30/12/2002 17:36:20 Posts: 4918 Location: Oxfordshire, England, UK
|
Posted: Sat Jan 17, 2015 8:03 pm Post subject: Getting a Windows batch file to read a .ini file? |
|
|
I'm looking for help in getting a batch file to read a .ini file
I'm not that good at coding, although I can Cut & Paste and edit if I can understand the code.
I've found a website that shows how to get a batch file to read keys in a .ini file, it's the Almanac Hackers "Reading an ini config file from a batch file" website.
My problem is that I can get the example in the above URL to work but, I'm getting trouble with getting any of my code that I place after the subroutine to work.
Here's my .ini file:
Code: | ; anything after a semi-colon is a comment
; some implementations do not recognize comments, some start them with #
; start a section with a line beginning with a text string inside square brackets
; sections [general] are useful divisions, however always use unique keys throughout whole file.
[general]
; "Windows XP"
; "Windows Vista"
; "Windows 7"
; "Windows 8"
; "Windows 8.1"
pc_opsy="Windows 7"
driveprog=c
drivedata=c
driveflash=e
|
Here's my batch file:
Code: | @echo off
:: putting a @ at the start of a line prevents the command on that line from being shown (echoed) on the screen.
echo ### Backup Pro Version 1.00
echo.
:: Version 1.00
:: Last updated 14/01/2015
::
:: History (latest first)
::
:: 1.00 (14/01/2015)
:: Completely new backup file that uses .ini file as well as exclude file
:readini
:: Read the ini file for values such as: PC Operating System; Drive of progame files; Drive of data; Drive of flash USB; etc
echo Reading .ini file ... Please wait!
echo.
set INIFILE="%~dp0settings.ini"
:: %0 is the script file name (with path), %~0 removes the surrounding " " ("%~0" == %0)
:: Adding dp returns the drive and path to the file, instead of the file name itself
call:getvalue %INIFILE% "pc_opsy" "" PC_OPSY
call:getvalue %INIFILE% "driveprog" "" DRIVEPROG
call:getvalue %INIFILE% "drivedata" "" DRIVEDATA
call:getvalue %INIFILE% "driveflash" "" DRIVEFLASH
echo PC Operating System: %PC_OPSY%
echo Program Drive: %DRIVEPROG%
echo Data Drive: %DRIVEDATA%
echo USB Drive: %DRIVEFLASH%
echo.
goto:exit
:getvalue
::@echo off
rem This function reads a value from an INI file and stored it in a variable
rem %1 = name of ini file to search in.
rem %2 = search term to look for
rem %3 = group name (not currently used)
rem %4 = variable to place search result
FOR /F "eol=; eol=[ tokens=1,2* delims==" %%i in ('findstr /b /l /i %~2= %1') DO set %~4=%%~j
goto:eof
:backuptype
:: Here we ask the user whether it's an incremental or full backup
:: Incremental = A backup of all changed and new files since the last backup
:: Full = A backup of all files in a specified backup set or job
:setpaths
:: The path of Microsoft Outlook and others are different in each version of Windows
if %PC_OPSY% EQU "Windows XP" goto ver_xp
if %PC_OPSY% EQU "Windows Vista" goto ver_vista
if %PC_OPSY% EQU "Windows 7" goto ver_7
if %PC_OPSY% EQU "Windows 8" goto ver_8
if %PC_OPSY% EQU "Windows 8.1" goto ver_8
goto warnthenexit
:ver_xp
:Run Windows XP specific commands here.
echo Operating System is: Windows XP
::set pc_opsy=Windows XP
set syspath1=Local Settings\Application Data
set syspath2=Application Data
goto step1
:ver_vista
:Run Windows Vista specific commands here.
echo Operating System is: Windows Vista
::set pc_opsy=Windows Vista
set syspath1=AppData\Local
set syspath2=AppData\Roaming
goto step1
:ver_7
:Run Windows 7 specific commands here.
echo Operating System is: Windows 7
::set pc_opsy=Windows 7
set syspath1=AppData\Local
set syspath2=AppData\Roaming
goto step1
:ver_8
:Run Windows 8 specific commands here.
echo Operating System is: Windows 8
::set pc_opsy=Windows 8
set syspath1=AppData\Local
set syspath2=AppData\Roaming
goto step1
:warnthenexit
echo Operating System is: Machine undetermined.
goto exit
:step1
echo hello world
:exit
@pause |
The purpose of the batch file is to backup data on a PC and it also needs to work out where the Outlook files are.
If anybody can advise (in simple English) I would appreciate it.
Many thanks,
Update:
On my PC, I try to keep all of my data under a single folder (C:\Data\). A few years ago, I wrote (cobbled together) a batch file script that does the following:
Determine versions of Windows (as that determines where MS Outlook files are found)
Set the path of the Outlook 2007 files
Copy important Outlook files to C:\Data\z DR Backups
Copy C:\Data\ and sub-folders to USB stick
NB1 An external "exclude" file is used by the batch file to stop large folders i.e. photos being backed up to the USB stick.
NB2 Whilst the versions of Windows (from XP to 8.1) might be different, it's always up until now been just Outlook 2007 that I have to backup. Now I need to cope with different versions of Outlook as well as different versions of Windows.
When I want to make a full backup, I run the batch file and then copy all of the C:\Data folder and all of its sub-folders to an external hard drive.
I could use a piece of software but I like the challenge of doing this via a batch file.
My wife has just got a new PC (Windows 8.1 and Office 2013) so the Outlook files are in yet another location. This time I'd like a batch file that I can easily configure for any version of Office and any version of Windows by an external config/ini file. _________________ Robert.
iPhone 6s Plus, iOS 14.0.1: iOS CamerAlert v2.0.7
TomTom GO Mobile iOS 2.3.1; TomTom (UK & ROI and Europe) iOS apps v1.29
Garmin Camper 770 LMT-D
Last edited by Privateer on Sun Jan 18, 2015 8:10 pm; edited 3 times in total |
|
Back to top |
|
|
DennisN Tired Old Man
Joined: Feb 27, 2006 Posts: 14902 Location: Keynsham
|
Posted: Sat Jan 17, 2015 9:05 pm Post subject: Re: Getting a Windows batch file to read a .ini file? |
|
|
Privateer wrote: | If anybody can advise (in simple English) I would appreciate it. |
Just give up? _________________ Dennis
If it tastes good - it's fattening.
Two of them are obesiting!! |
|
Back to top |
|
|
sussamb Pocket GPS Verifier
Joined: Mar 18, 2011 Posts: 4462 Location: West Sussex
|
Posted: Sat Jan 17, 2015 9:19 pm Post subject: |
|
|
I just use SyncToy ... _________________ Where there's a will ... there's a way. |
|
Back to top |
|
|
Oldboy Pocket GPS Moderator
Joined: Dec 08, 2004 Posts: 10643 Location: Suffolk, UK
|
Posted: Sat Jan 17, 2015 10:23 pm Post subject: |
|
|
At first glance the only thing I can see is that you have a goto:eof but no Label :eof
Most batch files would stop with an error at this point. _________________ Richard
TT 910 V7.903: Europe Map v1045
TT Via 135 App 12.075: Europe Map v1140 |
|
Back to top |
|
|
Kremmen Pocket GPS Verifier
Joined: Mar 03, 2006 Posts: 7147 Location: Reading
|
Posted: Sun Jan 18, 2015 5:50 am Post subject: |
|
|
sussamb wrote: | I just use SyncToy ... |
Ditch Synctoy and get SyncBackFree ASAP from 2brightsparks. Synctoy has numerous bugs that we discovered that can delete source files instead of backing them up.
SyncBackFree works in a similar manner as it does a 1 2 1 copy. _________________ DashCam:
Viofo A119 V3 |
|
Back to top |
|
|
Kremmen Pocket GPS Verifier
Joined: Mar 03, 2006 Posts: 7147 Location: Reading
|
Posted: Sun Jan 18, 2015 5:59 am Post subject: |
|
|
As far as trying to debug the vbs script I agree with getting a backup tool. For Outlook files I use pstbackup from Microsoft but if you know where your .pst or .ost files are you can use SyncBackFree to ensure they are backed up on a schedule.
Also don't forget to have a second backup on a portable drive that is only connected during a non scheduled manual backup. That prevents these crypto viruses corrupting all backups. _________________ DashCam:
Viofo A119 V3 |
|
Back to top |
|
|
M8TJT The Other Tired Old Man
Joined: Apr 04, 2006 Posts: 10118 Location: Bexhill, South Sussex, UK
|
Posted: Sun Jan 18, 2015 10:13 am Post subject: |
|
|
I suspect that privateer actually wants a clue as to why his code does not work, rather than a bunch of suggestions on what software to use to do backups.
Is a double colon the same as a semi colon for a REM? |
|
Back to top |
|
|
Kremmen Pocket GPS Verifier
Joined: Mar 03, 2006 Posts: 7147 Location: Reading
|
Posted: Sun Jan 18, 2015 12:56 pm Post subject: |
|
|
Yep, a 'comment' statement.
I've not dabbled that much in .vbs as I used to prefer VB6. _________________ DashCam:
Viofo A119 V3 |
|
Back to top |
|
|
sussamb Pocket GPS Verifier
Joined: Mar 18, 2011 Posts: 4462 Location: West Sussex
|
Posted: Sun Jan 18, 2015 1:25 pm Post subject: |
|
|
Kremmen wrote: | sussamb wrote: | I just use SyncToy ... |
Ditch Synctoy and get SyncBackFree ASAP from 2brightsparks. Synctoy has numerous bugs that we discovered that can delete source files instead of backing them up.
SyncBackFree works in a similar manner as it does a 1 2 1 copy. |
Thanks, good tip _________________ Where there's a will ... there's a way. |
|
Back to top |
|
|
Privateer Pocket GPS Moderator
Joined: 30/12/2002 17:36:20 Posts: 4918 Location: Oxfordshire, England, UK
|
Posted: Sun Jan 18, 2015 7:42 pm Post subject: |
|
|
I've add an update to the original post to hopefully explain my requirements a little better.
Thanks for everybody's comments so far, please keep them coming.
M8TJT wrote: | I suspect that privateer actually wants a clue as to why his code does not work, rather than a bunch of suggestions on what software to use to do backups. |
That's right.
M8TJT wrote: | Is a double colon the same as a semi colon for a REM? |
I believe that in an ini file a semi colon is used for a REM, whilst in a windows batch file a double colon is used for a REM. That's what I've picked up from some web pages - but we all know how accurate and truthful the internet is!
Kremmen wrote: | Ditch Synctoy and get SyncBackFree ASAP from 2brightsparks. Synctoy has numerous bugs that we discovered that can delete source files instead of backing them up.
SyncBackFree works in a similar manner as it does a 1 2 1 copy. |
Thanks for that I may resort to using SyncBackFree if I can't get my batch file to work.
Kremmen wrote: | As far as trying to debug the vbs script I agree with getting a backup tool. For Outlook files I use pstbackup from Microsoft but if you know where your .pst or .ost files are you can use SyncBackFree to ensure they are backed up on a schedule. |
Thanks for that. I'll also have a look at pstbackup.
Kremmen wrote: | Also don't forget to have a second backup on a portable drive that is only connected during a non scheduled manual backup. That prevents these crypto viruses corrupting all backups. |
Excellent advice, I generally run a full virus scan first and then copy to an external portable hard drive that is only connected for the duration of the backup.
The section of the batch file that reads the ini file works well. However the batch file does not appear to run any of the code that follows the ini reading sub-routine. _________________ Robert.
iPhone 6s Plus, iOS 14.0.1: iOS CamerAlert v2.0.7
TomTom GO Mobile iOS 2.3.1; TomTom (UK & ROI and Europe) iOS apps v1.29
Garmin Camper 770 LMT-D |
|
Back to top |
|
|
|
Posted: Today Post subject: Pocket GPS Advertising |
|
|
We see you’re using an ad-blocker. We’re fine with that and won’t stop you visiting the site.
Have you considered making a donation towards website running costs?. Or you could disable your ad-blocker for this site. We think you’ll find our adverts are not overbearing!
|
|
Back to top |
|
|
|