help to create a batch-file

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
Pf@nne
Posts: 2
Joined: 27 Aug 2018 13:16

help to create a batch-file

#1 Post by Pf@nne » 27 Aug 2018 13:30

Hi,

I try to write a little batch that lists the folders of a given directory.
My current batch looks like this:

Code: Select all

@echo off
cls
REM -----------------------------------------
REM Verzeichnis auslesen und anzeigen
REM -----------------------------------------
setlocal EnableDelayedExpansion
cd _customDevices
SET /a i = 0
FOR /D %%G in ("*") DO (
  SET /a i += 1
  Echo [!i!] %%~nxG
)
Echo Es wurden %i% Verzeichnisse gefunden.
Echo Welches Device soll geladen werden?
endlocal
REM -----------------------------------------
REM Verzeichnis auswählen
REM -----------------------------------------
set /P dirCounter=
Echo Verzeichnis #%dirCounter%
Output:

Code: Select all

[1] DEMO_GPIO
[2] DEMO_I2C_OW
[3] lightSensor
[4] simpleSwitch
[5] SWA1
Es wurden 5 Verzeichnisse gefunden.
Welches Device soll geladen werden?
4
Verzeichnis #4
this works so far....

The next step is to put the selected directory into a variable.
What is the best way to do this?
Run the loop again and break at the selected number?

Can anybody help me to complete the batch?

Greetings from Hamburg
Pf@nne

jeb
Expert
Posts: 1055
Joined: 30 Aug 2007 08:05
Location: Germany, Bochum

Re: help to create a batch-file

#2 Post by jeb » 28 Aug 2018 01:57

Hi Pf@nne,

you could store the directory names in an array (not really an array, but it has the look and feel).

Code: Select all

@echo off
cls
REM -----------------------------------------
REM Verzeichnis auslesen und anzeigen
REM -----------------------------------------
setlocal EnableDelayedExpansion
cd _customDevices
SET /a i = 0
FOR /D %%G in ("*") DO (
  SET /a i+=1
  set "deviceDir[!i!]=%%~nxG"
  Echo [!i!] %%~nxG
)
Echo Es wurden %i% Verzeichnisse gefunden.
Echo Welches Device soll geladen werden?

REM -----------------------------------------
REM Verzeichnis auswählen
REM -----------------------------------------
set /P dirCounter=
Echo Verzeichnis #%dirCounter%
echo Device: !deviceDir[%dirCounter%]!

Pf@nne
Posts: 2
Joined: 27 Aug 2018 13:16

Re: help to create a batch-file

#3 Post by Pf@nne » 29 Aug 2018 03:14

Great, thanks!

Code: Select all

[/@echo off
CLS

REM ----------------------------------------------------------------------------
REM MENU load or archive
REM ----------------------------------------------------------------------------

:start
ECHO.
ECHO what to do?
ECHO -------------------------------------------
ECHO.
ECHO [1] load device from archive
ECHO [2] archive modified device
ECHO [3] copy new clean OmniESP.json
ECHO [4] EXIT
SET /P toDo=

IF %toDo% == 1 GOTO loadDevice
IF %toDo% == 2 GOTO archiveDevice
IF %toDo% == 3 GOTO copyOmniesp
IF %toDo% == 4 GOTO end

CLS
ECHO Invalid Selection! Try again
PAUSE
CLS
GOTO start


REM ----------------------------------------------------------------------------
:loadDevice
CLS
ECHO load device from framework
ECHO -------------------------------------------
ECHO.
REM -----------------------------------------
REM clean framework
REM -----------------------------------------
ECHO clean framework files....
DEL data\customDevice\*.json
DEL src\customDevice\customDevice*.*
ECHO done
ECHO.

REM -----------------------------------------
REM read directory and show
REM -----------------------------------------

setlocal EnableDelayedExpansion
CD _customDevices
SET /a i = 0
FOR /D %%G in ("*") DO (
  SET /a i+=1
  SET "deviceDir[!i!]=%%~nxG"
  ECHO [!i!] %%~nxG
)
CD ..
ECHO.
ECHO select Device.....

REM -----------------------------------------
REM choose device
REM -----------------------------------------

SET /P dirCounter=
CLS
ECHO load Device: !deviceDir[%dirCounter%]!
ECHO -------------------------------------------
ECHO.

REM -----------------------------------------
REM copy Files
REM -----------------------------------------

ECHO copy files...
copy %~d0%~p0_customDevices\!deviceDir[%dirCounter%]!\data\customDevice\*.json data\customDevice
copy %~d0%~p0_customDevices\!deviceDir[%dirCounter%]!\src\customDevice\customDevice*.* src\customDevice
ECHO done

GOTO end

REM ----------------------------------------------------------------------------
:archiveDevice
CLS
ECHO archive Device
ECHO -------------------------------------------
ECHO.

REM -----------------------------------------
REM read directory and show
REM -----------------------------------------

setlocal EnableDelayedExpansion
CD _customDevices
SET /a i = 0
FOR /D %%G in ("*") DO (
  SET /a i+=1
  SET "deviceDir[!i!]=%%~nxG"
  ECHO [!i!] %%~nxG
)
CD ..
ECHO.
ECHO select Device.....

REM -----------------------------------------
REM choose device
REM -----------------------------------------

SET /P dirCounter=
CLS
ECHO archive Device: !deviceDir[%dirCounter%]!
ECHO -------------------------------------------
ECHO.

REM -----------------------------------------
REM clean archive
REM -----------------------------------------
ECHO clean archive files !deviceDir[%dirCounter%]!....
DEL _customDevices\!deviceDir[%dirCounter%]!\data\customDevice\*.json
DEL _customDevices\!deviceDir[%dirCounter%]!\firmware\*.bin
DEL _customDevices\!deviceDir[%dirCounter%]!\firmware\*.tar
DEL _customDevices\!deviceDir[%dirCounter%]!\src\customDevice\customDevice*.*
ECHO done
ECHO.

REM -----------------------------------------
REM copy Files
REM -----------------------------------------

ECHO archive files to !deviceDir[%dirCounter%]!
copy %~d0%~p0data\customDevice\*.json _customDevices\!deviceDir[%dirCounter%]!\data\customDevice
copy %~d0%~p0update\*.* _customDevices\!deviceDir[%dirCounter%]!\firmware
copy %~d0%~p0src\customDevice\customDevice*.* _customDevices\!deviceDir[%dirCounter%]!\src\customDevice
ECHO done

GOTO end

REM ----------------------------------------------------------------------------
:copyOmniesp
CLS
ECHO copy OmniESP to data DIR
ECHO -------------------------------------------
ECHO.
ECHO copy files...
copy %~d0%~p0_customDevices\OmniESP.json data

:end

code]

Post Reply