n0cturne
Goto Top

Variablenveränderung mit Hilfe von Zählschleife

Hallo Zusammen,

ich möchte mit Hilfe einer Zählschleife die Outlet Variablen bei jedem Durchgang +1 setzen
um dann ab der Sprungmarke SyncStart den Wert von Outlet_1, Outlet_2...usw zu verwenden.
Beim Test mit echo %Outlet% erhalte ich aber immer nur die Ausgabe Outlet_1 und nicht den Wert.

Würde mich über eine Brechstange freuen, die das Brett vor meinem Kopf löst. ;)

@echo off
title HeadquarterSync

:: Allgemeine Pfadangaben
set MatrixServerPath=\\127.0.0.1\Matrix$
set MatrixOutlets=%MatrixServerPath%\MatrixFiliale
set MatrixScriptLog=%MatrixServerPath%\MatrixScrip\zzz-ScriptLog

:: Filialen
set Headquarter=%MatrixOutlets%\Headquarter

set Outlet_1=%MatrixOutlets%\Filiale_1
set Outlet_2=%MatrixOutlets%\Filiale_2
set Outlet_3=%MatrixOutlets%\Filiale_3
set Outlet_4=%MatrixOutlets%\Filiale_4
set Outlet_5=%MatrixOutlets%\Filiale_5
set Outlet_6=%MatrixOutlets%\Filiale_6
set Outlet_7=%MatrixOutlets%\Filiale_7
set Outlet_8=%MatrixOutlets%\Filiale_8
set Outlet_9=%MatrixOutlets%\Filiale_9
set Outlet_10=%MatrixOutlets%\Filiale_10

set includes=waiters.dat families.dat articles.dat famgroups.dat

set /a OutletID=0
:OutletCheck
if /I %OutletID% GEQ 10 goto :SyncEnd
set /a OutletID=%OutletID%+1
set Outlet=Outlet_%OutletID%
echo %Outlet%

if not exist "%MatrixOutlets%\%Outlet%" goto SyncEnd  
set RobocopyParam=/E /MT:4 /XD handhelds licences /XF *.old /COPY:DAT /R:2 /W:2 /LOG+:%MatrixScriptLog%\HeadquarterSync_robocopy.log /X /NS /NP /NC /V /NDL /NFL

:SyncStart
:: usr\data
%MatrixServerPath%\MatrixTools\robocopy.exe %Headquarter%\usr\data\ %includes% %Outlet%\usr\data %RobocopyParam%
:: Zwänge: usr\data\constr
%MatrixServerPath%\MatrixTools\robocopy.exe %Headquarter%\usr\data\constr %Outlet%\usr\constr %RobocopyParam%
:: Preise: usr\prices
%MatrixServerPath%\MatrixTools\robocopy.exe %Headquarter%\usr\data\prices %Outlet%\usr\prices %RobocopyParam%

goto OutletCheck

:SyncEnd
PAUSE
exit 0

Content-Key: 300015

Url: https://administrator.de/contentid/300015

Printed on: April 24, 2024 at 15:04 o'clock

Member: Gersen
Gersen Mar 24, 2016 updated at 13:48:56 (UTC)
Goto Top
Hallo,

weil ein String (mit dem Wert eines Variablennamens) noch lange nicht die Variable selbst ist...
Warum lässt Du das Konstrukt Zeilen 12-21 nicht weg und sagst in Zeile 29:
set Outlet=%MatrixOutlets%\Filiale_%OutletID%

Gruß,
Gersen
Member: Biber
Solution Biber Mar 24, 2016 updated at 14:05:53 (UTC)
Goto Top
Moin n0cturne,

ergänze in deinem Schnipsel die Zeile 01 auf @echo off & setlocal enableDelayedExpansion
Stichwort DelayedExpansion -> Forumssuche

Ändere die Zeile 30 zur Demo auf echo Outlet=%Outlet% Inhalt:!%outlet%!
(und füge erstmal in der Zeile darunter ein goto :eof ein.))

Ergebnis sollte sein.
Outlet=Outlet_1 Inhalt:\\127.0.0.1\Matrix$\MatrixFiliale\Filiale_1


Wenn das Ergebnis deinen Wünschen entspricht, dann ändere in den Zeilen unterhalb Zeile 30 auch das %outlet% in !%outlet%!.

Abgesehen davon hat Gersen recht - du machst dir das Leben zu schwer mit dem dutzend Variablen.
Die kannst du alle in der Pseudo-Hochzählschleife abfackeln.

Grüße
Biber
Member: n0cturne
n0cturne Mar 24, 2016 updated at 15:52:43 (UTC)
Goto Top
Danke fürs Brett wegnagen, werter Herr Biber ;)
Läuft! :D
Member: n0cturne
n0cturne May 20, 2016 updated at 15:13:20 (UTC)
Goto Top
Hallo Nochmal,

seit ich @echo off & setlocal enableDelayedExpansion nutze, habe ich ein Problem, wenn Variablen ein ! enthalten.

set password=pw123!

Ergibt:
echo pw123

Das ist unglücklich, da ich auf das Passwort keinen Einfluss habe.
Was kann ich tun, um das Problem zu umschiffen?
Member: Biber
Biber May 23, 2016 at 07:09:53 (UTC)
Goto Top
Moin n0cturne,

es gibt auch zu setlocal EnableDelayedExpansion das Gegenstück SetLocal DisableDelayedExpansion.

Es sollte reichen, wenn du nach den Zeilen, in denen du die "Verzögerte Variablenauflösung" definitiv brauchst.

Also sinngemäß
....
SetLocal EnableDelayedExpansion

FOR /F .... %%i in ....(..) DO (
   Set "password=%%i"  
   Set "password=!password=~7,8!"  & Rem als Beispiel  
)
SetLocal DisableDelayedExpansion

Echo Passwort=[%passwort%]
...

Grüße
Biber
Member: n0cturne
n0cturne May 24, 2016 at 10:28:23 (UTC)
Goto Top
Hi Biber,

ich fürchte, dass ich mir SqlCmd Zeile selbst im Weg stehe.
Hast du eine Idee, wie ich hier verfahren kann?

@echo off & SetLocal EnableDelayedExpansion
title SQLBackup
color 1e

set BackupDate=%date:~6,4%-%date:~3,2%-%date:~0,2%
set BackupTime=%time:~0,2%-%time:~3,2%-%time:~6,2%

set SQLServer=127.0.0.1
set SQLInstance=SQLExpress
set SQLUser=sa
set SQLPassword=Passwort!

set BackupLocation=C:\Matrix\42\SQLBackups\%BackupDate%
if not exist "%BackupLocation%" md %BackupLocation%  

set SQLDatabase_1=master
set SQLDatabase_2=posdata
set SQLDatabase_3=posAccess
set SQLDatabase_4=
set SQLDatabase_5=
set SQLDatabase_6=
set SQLDatabase_7=
set SQLDatabase_8=
set SQLDatabase_9=
set SQLDatabase_10=

set /a DB_ID=0
:DB_Check
if /I %DB_ID% GEQ 10 goto :BackupEnd
set /a DB_ID=%DB_ID%+1
set SQLDatabase=SQLDatabase_%DB_ID%
set SQLDatabase=!%SQLDatabase%!
:eof

if "%SQLDatabase%" EQU "" goto BackupEnd  

:DB_Backup
SqlCmd -S %SQLServer%\%SQLInstance% -U %SQLUser% -P %SQLPassword% -Q "BACKUP DATABASE %SQLDatabase% TO DISK='%BackupLocation%\%SQLDatabase%.bak'"  

goto DB_Check

:BackupEnd

exit 0
Member: Biber
Solution Biber May 24, 2016 updated at 10:43:46 (UTC)
Goto Top
Moin n0cturne,

auf den ersten flüchtigen Blick:
Lass einfach in Zeile 01 das EnableDelayedExpansion ersatzlos weg - du benötigst in dem Schnipsel doch keine verzögerte Variablenauflösung.

Und weiter: Ändere sicherheitshalber die Schreibweise bei den SETs so:
set "SQLPassword=Passwort!"

Nur um unerwünschte trailing blanks auszuschliessen.
Nicht dass wir an der ganz falschen Stelle einen Fehler suchen.

Grüße
Biber
Member: n0cturne
n0cturne May 24, 2016 updated at 13:47:28 (UTC)
Goto Top
Habe die Quotes gesetzt und EnableDelayedExpansion entfernt.

Nun passt das Passwort mit dem !
Allerdings bekomme ich so ein Problem mit der SQLDatabase Variable,
die ich in Zeile 31 und 32 benutze.

echo %SQLDatabase%
ergibt
!SQLDatabase_1!

Dabei möchte ich mit master, posdata oder posAccess weiterarbeiten.
Mitglied: 129413
129413 May 24, 2016 updated at 17:14:14 (UTC)
Goto Top
DelayedExpansion kann an jeder Stelle im Skript an und aus geschaltet werden
Setlocal Enabledelayedexpansion 
REM mach hier was mit delayed expansion !Variable!
Setlocal Disabledelayedexpansion
Dort wo man es eben braucht schaltet man sie vorher ein und danach wieder aus.

Gruß skybird
Member: Biber
Solution Biber May 24, 2016 at 14:10:30 (UTC)
Goto Top
Moin n0cturne,

um skybirds Antwort für dich noch eindeutiger zu präzisieren:

Zitat von @n0cturne:

Allerdings bekomme ich so ein Problem mit der SQLDatabase Variable,
die ich in Zeile 31 und 32 benutze.
Dann halt VOR der heutigen Zeile 31 ein Setlocal EnableDelayedExpansion einfügen.
Nach der heutigen Zeile 32 ein Setlocal DisableDelayedExpansion einfügen.

Grüße
Biber