Page 1 of 1

Left String in a For Loop

Posted: 14 Jul 2008 11:13
by Obi-Wahn
Hi!

I've at least one problem with my Batch file. But first a few words about myself:

My name is Andrew, I'm 23 Jears old, and I'm living in Vienna (Austria). I'm educated in furniture building and restauration as well as in designing buildings.
But the other side on me is, I like coding and doing everything on the computer.

Well, and so on, we're right on my problem. I'm trying to create a new, more comfortable version of a RunOnceEx.cmd file, which is used in Unattended Windows Installations.
My plan is, to write the Title of the Application to install and the Path as well as the Silent-Installation-Switches into a Text-File, so it's easier to modify this file.

For demonstrative purposes, I've created a small file with a few Datastrings.

Code: Select all

Title1;Folder1\Setup.exe
Title2;Folder23\Setup.exe /S /D="C:\Program Files\Programfolder\"
Title3;\\Server2\Folder4\Setup.exe /S

Save this Code as "programs.txt"

Code: Select all

@echo off
setlocal
If %1 LEQ "" Exit
set KEY=HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\RunOnceEx
set PFile=%1
set IFile=%2
FOR %%i 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 (
   IF EXIST %%i:\%IFile% (
      SET SYSPATH=%%i:
   ) ELSE (
      SET SYSPATH=%SystemDrive%\Install
   )
)
echo Reg Add %KEY% /V title /D "Installiere folgende Anwendungen" /f > test.txt
echo Reg Add %KEY% /V flags /t REG_DWORD /D 0x00000030 /f >> test.txt
for /f "tokens=1,2 delims=;" %%a in (%PFile%) do (
   echo Merke Programm "%%a" zur Installation vor... >> test.txt
   echo. >> test.txt
   set NCheck=%%b
   :: Here's the Problem
   set NCheck=%NCheck:~0,2%
   If "%NCheck%"=="" (
      echo Fehler!
      goto EOF
   )
   If "%NCheck%" EQU "\\" (
      Set IPath=%%b
   ) ELSE (
      Set IPath=%SYSPATH%\%%b
   )
   echo Reg Add %KEY% /VE /D "%%a" /f >> test.txt
   echo Reg Add %KEY% /V 1 /D "%IPath%" /f >> test.txt
   set /a c=c + 1
)
echo. >> test.txt
echo Installationsreihenfolge gespeichert. >> test.txt
echo %c% Programme zur Installation vorgemerkt... >> test.txt
echo. >> test.txt

:EOF
del test.txt
endlocal

save this code as "RunOnceEx.cmd"

The echo's are written in German, but I think, this shouldn't cause troubles.
The aim of the Code is, that I can pass one or two Parameters (both filenames) to the script. The first file contains the data as shown above, and the second file is a indicator. If a second parameter is passed, the Batch will search for the drive, which contains the file an set an variable to the driveletter. This is used for installation from the CD or DVD, instead of copying the entire content of the Disc to the harddisc.

While Reading the Contents of the first file, the 2nd for-loop shall write the data to the registry for installation after a reboot (the reboot will be performed by the setup-routine, not by this script).

I've wrote a marker into the code, to show you where the script lags.
It's the Network-Path-check. I take the first two characters of the path, and if it's a double-backslash (\\) it's taken as a entire network-path.

I've redirected the output to a file (test.txt), so your computer won't be damaged if you execute the batch.

I'd be glad, if someone could help me.

Thanks in Advance
Obi-Wahn