Surendra Sharma

Surendra Sharma

Search This Blog

Showing posts with label DOS. Show all posts
Showing posts with label DOS. Show all posts

Friday, January 11, 2019

Batch file to create empty app from Sitecore JSS boilerplate app


If you are new to Sitecore JSS app and want to develop any website from it, then best place to start working on this is to restore sample Sitecore JSS app.

Default Sitecore JSS app comes as boilerplate.

Understand how it is functioning and how to write the code. Once you understand sample app, then remove all the default sample content (the Styleguide and GraphQL files) from below paths:

1.            Delete /src/components/Styleguide* and /src/components/GraphQL*
2.            Delete /sitecore/definitions/components/Styleguide*, /sitecore/definitions/templates/Styleguide*, and /sitecore/definitions/components/GraphQL*
3.            Delete /data/component-content/Styleguide
4.            Delete /data/content/Styleguide
5.            Delete /data/routes/styleguide and /data/routes/graphql
6.            Delete /data/dictionary/*.yml

If you like you can remove these files and folders manually. To make your life easy, I have created a batch file which remove all these contents and give you perfect empty app in just two click. 

Just copy the batch file in your JSS application root directory as "CleanupApp.bat" and run it.

Sitecore JSS App clean up batch file
Sitecore JSS App clean up batch file

Batch file looks like

REM Batch file to remove all of the default sample content (the Styleguide and GraphQL routes)

FOR /D %%p IN (".\src\components\Styleguide*") DO RMDIR "%%p" /s /q
FOR /D %%p IN (".\src\components\GraphQL*") DO RMDIR "%%p" /s /q

DEL /q ".\src\components\Styleguide*"
DEL /q ".\sitecore\definitions\components\Styleguide*"
DEL /q ".\sitecore\definitions\components\GraphQL*"
DEL /q ".\sitecore\definitions\templates\Styleguide*"

FOR /D %%p IN (".\data\component-content\Styleguide") DO RMDIR "%%p" /s /q

FOR /D %%p IN (".\data\content\Styleguide") DO RMDIR "%%p" /s /q

FOR /D %%p IN (".\data\routes\styleguide") DO RMDIR "%%p" /s /q
FOR /D %%p IN (".\data\routes\graphql") DO RMDIR "%%p" /s /q

DEL /q ".\data\dictionary\*.yml"

That’s it.


Friday, November 4, 2016

Run SQL scripts from DOS batch file



This post is regarding executing SQL scripts from DOS batch file by using sqlcmd.

sqlcmd with batch script
sqlcmd with batch script


When you want to setup new environment, you might need to run lots of database scripts manually. But fortunately we can avoid this manual step by using sqlcmd command line utility with the help of DOS batch file. 

Create folders and keep SQL scripts inside those folders and sub folders as

Scripts in Folder
Scripts in Folder


Tip: - You should keep your script name start with number so that they will run in same order.

Below is a sample script which execute all scripts inside folders and its sub folders

@ECHO OFF
SET SQLLOG=%~dp0sql_log.txt
SET SQLSERVER=.\SQLEXPRESS
ECHO. Starting Script, Server: %SQLSERVER%, logging to : %SQLLOG%
ECHO.

CALL:runsql "1 Create Databases"
CALL:runsql "2 Create Schema"
CALL:runsql "3 Data\1 Insert"
CALL:runsql "3 Data\2 Insert"

EXIT /B %ERRORLEVEL%

::Function to run SQL script files in a Directory
:runsql
echo.
echo. Executing Scripts in Directory:
echo. %~1
echo.
PUSHD %~1
for %%G in (*.sql) do echo. sqlcmd -S"%SQLSERVER%" -E -e -i"%%G"
for %%G in (*.sql) do echo. sqlcmd -S"%SQLSERVER%" -E -e -i"%%G"
"%SQLLOG%"
for %%G in (*.sql) do sqlcmd -S"%SQLSERVER%" -E -e -i"%%G" >> "%SQLLOG%"
POPD


In current script, I am using below options
-S [protocol:]server[instance_name][,port]
-e (echo input)
-E (use trusted connection)
-i input_file

Pushd command and popd command in a batch program is used to change the current directory from the one in which the batch program was run and then change it back.

Note: - Your SQL server should be in mixed mode.

You can refer link to know more about sqlcmd command and its various available options at https://msdn.microsoft.com/en-us/library/ms162773.aspx

This batch file also log all SQL command that it run in “sql_log.txt” file.

After running this script, I am able create database, tables and insert some records in those tables as

Final DB
Final DB


I hope you like this SQL batch trick. Stay tuned for more articles.

Please leave your comments or share this article if it’s useful for you.