PDA

View Full Version : Batch Case Statement



Clutch
06-26-2015, 08:52 PM
Okay, so this is what I have so far... My concern is if there is a way to shorten this. I would rather it just detect if x directory exists it replaces it - if more than x directories exists returns an error. Currently it'll go through all of them - and if multiple locations exists it'll replace the addin in both locations.

@ECHO OFF
:CASE_O12
xcopy /y "C:\Source" "C:\Program Files (x86)\Microsoft Office\Office12\Library"
GOTO END_CASE

:CASE_O14
xcopy /y "C:\Source" "C:\Program Files (x86)\Microsoft Office\Office14\Library"
pause

:CASE_O15
xcopy /y "C:\Source" "C:\Program Files (x86)\Microsoft Office\Office15\Library"
GOTO END_CASE

:END_CASE
ECHO Successfully replaced addin!
pause

Title Replace Addin
ECHO.
ECHO Replacing Addin
IF EXIST "C:\Program Files (x86)\Microsoft Office\Office12\Library" GOTO CASE_012
IF EXIST "C:\Program Files (x86)\Microsoft Office\Office14\Library" GOTO CASE_014
IF EXIST "C:\Program Files (x86)\Microsoft Office\Office15\Library" GOTO CASE_015


Edit: Also if anyone could give me some insight on how to make this into an executable program with a GUI that'd be awesome.

Le Jingle
06-26-2015, 10:44 PM
This is a rewrite of the initial code you posted;


@echo off
set "destDir=C:\Source"
call :checkExists "C:\Program Files (x86)\Microsoft Office\Office12\Library" "%destDir%"
call :checkExists "C:\Program Files (x86)\Microsoft Office\Office14\Library" "%destDir%"
call :checkExists "C:\Program Files (x86)\Microsoft Office\Office15\Library" "%destDir%"
EXIT /B
:checkExists
if exist "%1" (
xcopy %1 %2
)
goto:EOF


Adding this to an executable program with a GUI would be much more work that this simple program is worth in my opinion.

However if it's really important, you could simply make a java swing GUI, then wrap the executable with Launch4j.

Your post lacks many details and contents as how to proceed with a more in-depth approach.
Questions I would ask include:
1. What layout would the GUI be?
2. What would go on the GUI (what functionality/components/etc)?
3. Is this windows dependent? (could make into cross-platform)
4. Is this a simple script, or are you trying to use this on a large scale basis (lots and lots of files being used)
5. etc.

-lj

m3gaman3g3nd
06-26-2015, 11:24 PM
Well there arent any really CASE implementation in Batch really at all but you are probablly looking for a directory walker...
and if directory exists



@echo off
for /r "C:\Program Files (x86)\Microsoft Office" %%a in (.) do echo %%~fa
pause

this will list folder tree...
if you know the name of the folder

@echo OFF
setlocal enabledelayedexpansion
for /r "C:\Program Files (x86)\Microsoft Office" %%a in (.) do (
IF "%%~fa" == "C:\Program Files (x86)\Microsoft Office\Office12\Library" (
MKDIR "C:\FOLDER"
XCOPY "C:\Program Files (x86)\Microsoft Office\Office12\Library" "C:\FOLDER" )
)
pause

Clutch
06-28-2015, 12:58 AM
This is a rewrite of the initial code you posted;


@echo off
set "destDir=C:\Source"
call :checkExists "C:\Program Files (x86)\Microsoft Office\Office12\Library" "%destDir%"
call :checkExists "C:\Program Files (x86)\Microsoft Office\Office14\Library" "%destDir%"
call :checkExists "C:\Program Files (x86)\Microsoft Office\Office15\Library" "%destDir%"
EXIT /B
:checkExists
if exist "%1" (
xcopy %1 %2
)
goto:EOF


Adding this to an executable program with a GUI would be much more work that this simple program is worth in my opinion.

However if it's really important, you could simply make a java swing GUI, then wrap the executable with Launch4j.

Your post lacks many details and contents as how to proceed with a more in-depth approach.
Questions I would ask include:
1. What layout would the GUI be?
2. What would go on the GUI (what functionality/components/etc)?
3. Is this windows dependent? (could make into cross-platform)
4. Is this a simple script, or are you trying to use this on a large scale basis (lots and lots of files being used)
5. etc.

-lj

Thanks, I'll look over this later when I get time!