Reputation: 6007
I have this windows batch
file to make easier my frontend developers work:
@echo off
echo.
set NEW_COMPONENT=%1
set NEW_COMPONENT=%NEW_COMPONENT:/=\%
set NEW_COMPONENT_TO_SPLIT=%NEW_COMPONENT:/= %
REM get the component name (last item form '/' spearated array)
for %%a in ("%NEW_COMPONENT_TO_SPLIT%") do set "COMPONENT_NAME=%%~nxa"
set HTML=src\%NEW_COMPONENT%\%COMPONENT_NAME%.html
set SCSS=src\%NEW_COMPONENT%\_%COMPONENT_NAME%.scss
set JS=src\%NEW_COMPONENT%\_%COMPONENT_NAME%.js
set IMG=src\%NEW_COMPONENT%\img
mkdir src\%NEW_COMPONENT%
echo | set /p x=created folder: src\%NEW_COMPONENT%\
echo. 2>%HTML%
echo | set /p x=created file: %HTML%
echo. 2>%SCSS%
echo | set /p x=created file: %SCSS%
echo. 2>%JS%
echo created file: %JS%
mkdir %IMG%
echo | set /p x=created folder: %IMG%\
echo.
Now they ask me put import %COMPONENT_NAME% from '%PARENT_COMPONENT';
string into first line of parent component .js
files to link automatically the new component and the parent.
The user can use this like this:
create-component.bat components/header/nav
In this case the %COMPONENT_NAME%
will be nav
but how can I catch the item before last item? In this case it will be header
Upvotes: 0
Views: 491
Reputation:
Change your for
loop section to this (ammended first loop and added line):
for %%a in ("%NEW_COMPONENT_TO_SPLIT%") do set "COMPONENT_TMP=%%~pa"
for %%i in ("%COMPONENT_TMP:~0,-1%") do set "COMPONENT_NAME=%%~nxi"
So, your ammended script:
@echo off
echo.
set NEW_COMPONENT=%1
set NEW_COMPONENT=%NEW_COMPONENT:/=\%
set NEW_COMPONENT_TO_SPLIT=%NEW_COMPONENT:/= %
REM get the component name (last item form '/' spearated array)
for %%a in ("%NEW_COMPONENT_TO_SPLIT%") do set "COMPONENT_TMP=%%~pa"
for %%i in ("%COMPONENT_TMP:~0,-1%") do set "COMPONENT_NAME=%%~nxi"
set HTML=src\%NEW_COMPONENT%\%COMPONENT_NAME%.html
set SCSS=src\%NEW_COMPONENT%\_%COMPONENT_NAME%.scss
set JS=src\%NEW_COMPONENT%\_%COMPONENT_NAME%.js
set IMG=src\%NEW_COMPONENT%\img
mkdir src\%NEW_COMPONENT%
echo | set /p x=created folder: src\%NEW_COMPONENT%\
echo. 2>%HTML%
echo | set /p x=created file: %HTML%
echo. 2>%SCSS%
echo | set /p x=created file: %SCSS%
echo. 2>%JS%
echo created file: %JS%
mkdir %IMG%
echo | set /p x=created folder: %IMG%\
echo.
Upvotes: 0
Reputation: 56180
for
can work with relative paths. So components\header\nav\..
is the same as components\header
. From that path you need the last element %%~nxA
:
set "NEW_COMPONENT_TO_SPLIT=components\header\nav"
for %%A in ("%NEW_COMPONENT_TO_SPLIT%\..") do set "COMPONENT_NAME=%%~nxA"
echo %COMPONENT_NAME"
Upvotes: 3