Showing posts with label bat. Show all posts
Showing posts with label bat. Show all posts

Thursday, August 8, 2013

Svn file full history

If you’d need to search in the full history of a file stored in SVN, the script below can make an export of all changes ever applied into a file, starting for the first revision (full file) and adding each time an overview of all changes applied.

If you remember you ever applied some changes in a file, but removed it afterwards, this can help to search in the full history. Only tested on text based files.

For each revision, the revision number, author, timestamp and svn comments will be listed, followed by an overview of the removed lines, added lines, update lines etc. The initial revision will be a full extract of the original file.

Usage: save the SvnFileFullHistory.bat script in an SVN folder next to the file for which you’d need a full history extract. Drop the svn file onto the bat script to start the script and retrieve the full history of the dropped svn file. The full svn file history will be saved in a txt file in the same folder and with the same base name as the file to extract, but adding “-FullSvnHistory.txt” behind the file name. The extract can take a while, depending on the number of revisions and the size of the svn file. But while the extract is ongoing, the “…-FullSvnHistory.txt” can be read already, reloading it to get future updates.

A similar linux shell script has been created as well (not fully tested).

Based on information found on stackoverflow by user ladenedge.

Full script content:

@echo off   
TITLE SVN - Full file history
REM Original source: http://stackoverflow.com/questions/282802/how-can-i-view-all-historical-changes-to-a-file-in-svn and http://stackoverflow.com/questions/5622367/generate-history-of-changes-on-a-file-in-svn/5721533#5721533
echo Copy this bat script next to the checked out svn file on which to get full svn history. Drag and drop the svn file onto the bat script to start fetching the info (or a open command window and provide the name of the svn file as first parameter to the bat script execution)
if "%1%"=="" pause
set file=%1
set report=%file%-FullSvnHistory.txt
if [%file%] == [] (
  echo Usage: "%0 <file>"
  exit /b
)
echo Retrieving svn history of file, please wait...
echo The report will be saved in the file: %report%.
echo To stop the process press Ctrl+c.
rem first revision as full text
for /F "tokens=1 delims=-r " %%R in ('"svn log -q %file%"') do (
  svn log -r %%R %file% > %report%
  svn cat -r %%R %file% >> %report%
  goto :diffs
)
:diffs
rem remaining revisions as differences to previous revision
for /F "skip=2 tokens=1 delims=-r " %%R in ('"svn log -q %file%"') do (
  echo.
  svn log -r %%R %file% >> %report%
  svn diff -c %%R %file% >> %report%
)

Monday, August 22, 2011

Batch parameter modifier

In batch script, one can use the following modifier to expand to full path etc.

%1 first parameter provided to the script.
%~1 Expands %1 and removes any surrounding quotation marks ("").
%~f1 Expands %1 to a fully qualified path name.
%~d1 Expands %1 to a drive letter.
%~p1 Expands %1 to a path.
%~n1 Expands %1 to a file name.
%~x1 Expands %1 to a file extension.
%~s1 Expanded path contains short names only.
%~a1 Expands %1 to file attributes.
%~t1 Expands %1 to date and time of file.
%~z1 Expands %1 to size of file.
%~$PATH:1 Searches the directories listed in the PATH environment variable and expands %1 to the fully qualified name of the first one found. If the environment variable name is not defined or the file is not found, this modifier expands to the empty string.
%~dp1 Expands %1 to a drive letter and path.
%~nx1 Expands %1 to a file name and extension.
%~dp$PATH:1 Searches the directories listed in the PATH environment variable for %1 and expands to the drive letter and path of the first one found.
%~ftza1 Expands %1 to a dir-like output line.
%cd% current working direcotry.
%~dp0 script directory.
%~dp0..\ parent of script direcotry.

SET Today=%Date: =0%
SET TodayYYYYMMDD=%Today:~-4%%Date:~-7,2%%Date:~-10,2%
SET Now=%Time: =0%

In the previous examples, you can replace %1 and PATH with other batch parameter values.

The %* modifier is a unique modifier that represents all arguments passed in a batch file. You cannot use this modifier in combination with the %~ modifier. The %~ syntax must be terminated by a valid argument value.

You cannot manipulate batch parameters in the same manner that you can manipulate environment variables. You cannot search and replace values or examine substrings. However, you can assign the parameter to an environment variable, and then manipulate the environment variable.

String replace in variables: SET VAR=%VAR:12345=Hello% (replace 12345 with Hello in variable VAR)

Monday, November 16, 2009

Recovery tools boot USB stick


Based on Hiren's Boot CD 12 I created my personal Boot USB stick to be as complete as possible (containing more than 500 portable tools, 2,30GB). The original Hiren recovery CD contains many very useful tools to recover, tweak or patch pc's, divided into the following categories: Partition Tools, Backup Tools, Recovery Tools, Testing tools, RAM testing tools, Hard disk tools, System information tools, Master Boot Recovery tools, BIOS CMOS tools, Multimedia tools, Password tools, NTFS tools, Browser File manager tools, Other tools, Dos tools, Optimizers, Network tools, Process tools, Registry tools, Startup tools, Tweakers and Antivirus tools. A portable 'mini Windows XP' that can be run from the stick is available as well at boot time.


Many of the tools are available by booting up from the USB stick, but others need to be run into a Windows environment. These windows tools can be easily accessed by using the 'HBCDMenu.exe' tool which will be started when the cd or USB stick is started within a running Windows environment. Since the tools available within the 'HBCDMenu.exe' can be configured very easily using a 'HBCDMenu.csv' file, I created an Excel file to change the configuration an export to the csv file easier. Using this Excel it is much easier to move and rearrange the tools. Next I added all the tools I was still missing to make them available through the 'HBCDMenu.exe' tool. All tools are started using a DOS bat script and an UHARC archive. The archive is extracted in the PC's %temp% folder and started. All tools should be completely portable so no tool settings in the registry are kept after running them. I created a generic batch script to be able to run all tools in different modes: normal, just open a command window, just open an explorer windows, run the tool in Sandboxie, show online info on the tool, convert the tool into a zip file, force extraction of the uharc file. The mode is set by creating a specific file in the %temp% folder.


I keep all my personal files in a secured FreeOTFE file to make sure if I ever lose the stick no personal information can be discovered.


Besides the Hiren tools I also converted the latest BackTrack 4 bootable ISO to make it boot from a USB stick and added this into the Hiren boot screen menu. This live cd linux distribution is focused on penetration testing and perfect for quick and easy WEP cracking.


On the website of Hiren, a good explanation is provided by Hiren on how to easily convert the BootCD into a bootable USB stick using Grub4Dos. I used this 'menu.lst' as boot menu so it includes the launch of the BackTrack live environment and portable Mini Windows XP. Within an Windows environment, this 'autorun.inf' file is used to make it easier to start the 'autorun.exe' tool and other commonly used tools. To keep a backup of all my configuration, I configured a specific portable Dropbox so I can access all my tools online and keep them in sync on different locations.


The USB stick with all the extra tools requires now at least 2,29GB (I use it on a 8GB stick). I also added many of my extra tools into the CD iso file, but to keep it burnable onto a 80minute CD, some of the large tools didn't fit (office portable, tor browser, skype, toad, oracle client).


Compared to the original Hiren 10 boot cd, I've added different Windows tools. In the HBCDMenu cvs creator Excel, the complete list of all the tools are ordered in comprehensive categories. Many of these tools come from Sysinternals and Nirsoft since they provide some very useful portable little tools.


Update 25/11/2009: removed long list of personally added tools
Update 3/12/2010: update for Hiren Boot CD 12

Sunday, May 24, 2009

Eclipse project

eclipse Each project (workspace) in Eclipse has it's own .project file. To easily open the correct workspace with Eclipse, I wrote a little batch script that can be associated in Windows with the .project files. Now, I only have to double click the .project file and Eclipse will be loaded in the correct workspace.

  • Save the batch script bellow and make sure the correct Eclipse.exe file is used. You might want to change the Eclipse options if required.
  • Double click on a .project file, the first time Windows will ask how to open this file. Choose the .bat batch file script to open your .project file and make sure to make Windows remembers this option.
  • Eclipse will now open with the correct workspace loaded.
@echo off
set ECLIPSE_BIN=R:\tools\eclipse\eclipse.exe
set ECLIPSE_OPTIONS=-refresh -showlocation -Xmx512M -XX:MaxPermSize=512m
set PROJECT_FULL_PATH=%1%
set PROJECT_FOLDER=%PROJECT_FULL_PATH:.project=%
cd %PROJECT_FOLDER%
cd ..
set PROJECT_WORKSPACE="%CD%"
start "eclipse" "%ECLIPSE_BIN%" %ECLIPSE_OPTIONS% -data %PROJECT_WORKSPACE%
@echo on


Download batch script.

Sunday, April 26, 2009

End (kill) some application from command line

Different methods exist in Windows to end or kill some application from command line.

The normal way to do this is by using the TASKKILL command, for example to end FireFox one can use:

TASKKILL /F /IM “firefox.exe”


When you need to stop some command window it can be dangerous to just kill any cmd.exe, so one could use the window title to kill a specific batch window, for example to kill any batch window with name starting with “SIM-” I used:



TASKKILL /FI "WINDOWTITLE eq SIM-*"


But sometimes you’ll need to be more specific, I wanted an automated way to kill some Java application but only when it was started with some specific parameter. The only way I found to do so on Windows was by using the WMIC commands. This example show how I killed our Java monitor application which started as follows:



java -Dapp.prop.file.monitor="monitor_system.properties" -classpath SOME_JARS Monitor


Kill this specific Java instance:



wmic process where "name like '%%java.exe%%' AND commandline like '%%monitor%%'" DELETE


Before killing your application, you want to make sure your query will result in the correct process, one can easily check the query by executing the similar wmic command without yet killing the process:



wmic process where "name like '%%java.exe%%' AND commandline like '%%monitor%%'" get processid, name, commandline /VALUE


More examples and combinations that can be used with wmic are available on this page.



The Linux equivalent we used was pkill, for example to kill our simulators we used:



pkill -f "simulator.jar"

Sunday, April 19, 2009

SVN Automated repeated backup / dump – Windows / Linux

Every project using SVN should have an automated backup system in place for their SVN repository. We decided we wanted a weekly incremental backup. But we also wanted to easily create a new initial backup. I scripted it on Windows and Linux with the Shell/Batch scripts shown below.

For both scripts, the same methods is used: we keep track of the last backed up revision in a file called svn_backup_delta.txt. Every time the script is started, an incremental dump is exported and archived, starting from the last dumped revision till the latest available revision. If the latest revision is the same as the last dumped revision, nothing will happen.

If the svn_backup_delta.txt doesn’t exist or the revision could not be read correctly, a complete initial dump from revision 0 till the latest revision will be exported and archived. The scripts are called daily or weekly with a standard OS scheduler. When we want to have a new initial complete dump, we simply remove the svn_backup_delta.txt file.

The scripts can be a good starting point to create your own automated SVN dumping mechanism.

Windows backup_svn.bat Batch script. This script now expects the svn.exe and svnrdump.exe to be in the same folder as the script. It also requires a repos.txt and backup.properties files in the same folder. The output of the script will be stored in the same folder within the file backup_svn.log.

@echo off 
  TITLE %~n0 - see log %~n0.log 
  echo All details will be logged into %~n0.log 
  CALL :STARTBATCH >> %~dp0%~n0.log 2>&1 
  GOTO :EOF

:STARTBATCH     REM All properties are set in backup.properties     REM Variables that change in a for loop, should be marked with !VARIABLE! instead of %VARIABLE%     REM Necessary to use this, otherwise SET !VARIABLE! in for loop fails     setlocal enabledelayedexpansion     ::CONFIGURATION     ECHO %Date:~-4%%Date:~-7,2%%Date:~-10,2%-%Time% CHECKING FOR CHANGES AT %DATE%: %TIME%     REM Get properties from backup.properties and set as variables     FOR /F "tokens=1,2 delims==" %%A IN (%~dp0backup.properties) DO (     echo %Date:~-4%%Date:~-7,2%%Date:~-10,2%-%Time% %%A: %%B     set %%A=%%B     ) 
  :END_CONFIGURATION

REM Loop through all repos 
  for /f "delims=," %%r in (%~dp0repos.txt) DO ( 
  echo. 
  ECHO %Date:~-4%%Date:~-7,2%%Date:~-10,2%-%Time% Starting SVN backup 
  ECHO %Date:~-4%%Date:~-7,2%%Date:~-10,2%-%Time% Using following configuration: 
  SET REPO_URL=%REPO_BASE%/%%r 
  ECHO %Date:~-4%%Date:~-7,2%%Date:~-10,2%-%Time% -Repository URL: "%REPO_BASE%/%%r" 
  SET LAST_DELTA_FILE=%DUMP_BASE%\%%r\svn_backup_delta_%%r.txt 
  ECHO %Date:~-4%%Date:~-7,2%%Date:~-10,2%-%Time% -File to keep track of last revision backed up: !LAST_DELTA_FILE! 
  SET DUMP_FILE=%DUMP_BASE%\%%r 
  SET Today=%Date: =0% 
  SET Today=!Today:~-4!%Date:~-7,2%%Date:~-10,2% 
  SET Now=%Time: =0% 
  FOR /F "tokens=1,2 delims=:.," %%A IN ("!Now!") DO SET Now=%%A%%B

IF NOT EXIST "!DUMP_FILE!" mkdir "!DUMP_FILE!" 
  %~dp0svn --username %SVN_USERNAME% --password %SVN_PASS% --non-interactive --trust-server-cert --no-auth-cache info !REPO_URL! > %TEMP%\repoinfo.tmp

SET LAST_REV= 
  for /f "usebackq tokens=1,2 delims=: " %%i in (`find "Revision: " "%TEMP%\repoinfo.tmp"`) do (     SET LAST_REV=%%j     )     REM Check if !LAST_REV! retrieved of SVN is a number. If not, exit script.     echo !LAST_REV!| findstr /r "^[0-9]*$">nul || ( ECHO %Date:~-4%%Date:~-7,2%%Date:~-10,2%-%Time% ERROR: Last revision number is !LAST_REV! of %%r doesn't seem to be valid, aborting.     GOTO :END )     ECHO %Date:~-4%%Date:~-7,2%%Date:~-10,2%-%Time% Last revision: !LAST_REV!     ECHO %Date:~-4%%Date:~-7,2%%Date:~-10,2%-%Time% Last delta file: !LAST_DELTA_FILE!     REM Check if full backup exists, if true, do an incremental backup     IF NOT EXIST !LAST_DELTA_FILE! (         ECHO %Date:~-4%%Date:~-7,2%%Date:~-10,2%-%Time% Not delta file found, processing full export         call :FULL     ) ELSE (         ECHO %Date:~-4%%Date:~-7,2%%Date:~-10,2%-%Time% Delta found, processing delta         call :DELTA     ) 
  ) 
  ECHO %Date:~-4%%Date:~-7,2%%Date:~-10,2%-%Time% All repo's done, backup finalized 
  GOTO :END

REM Do an incremental backup 
  :DELTA     ECHO %Date:~-4%%Date:~-7,2%%Date:~-10,2%-%Time% DELTA     SET LAST_DELTA=     for /f "usebackq tokens=1,2 delims=: " %%i in (`find "Revision: " "!LAST_DELTA_FILE!"`) do (         SET LAST_DELTA=%%j     )     ECHO %Date:~-4%%Date:~-7,2%%Date:~-10,2%-%Time% Last delta:!LAST_DELTA!     ECHO %Date:~-4%%Date:~-7,2%%Date:~-10,2%-%Time% Last revision:!LAST_REV!     IF "!LAST_DELTA!"=="" GOTO FULL     IF "!LAST_REV!"=="" GOTO :END     IF "!LAST_DELTA!"=="!LAST_REV!" GOTO :END     REM Check if !LAST_DELTA! is a number. If not, exit script.         echo !LAST_DELTA!| findstr /r "^[1-9][0-9]*$">nul || ( ECHO ERROR: DELTA REVISION NUMBER IN !LAST_DELTA_FILE! OF %REPO_URL% DOESN'T SEEM TO BE A NUMBER... ABORTING BACKUP...     GOTO :END )     SET /A INCRE_LAST_DELTA=LAST_DELTA+1     SET DUMP_FILE_NAME=!Today!-!Now!-INCREMENTAL-R!INCRE_LAST_DELTA!-TO-R!LAST_REV!.dump     SET DUMP_FILE=!DUMP_FILE!\!DUMP_FILE_NAME!     ECHO %Date:~-4%%Date:~-7,2%%Date:~-10,2%-%Time% Creating dump file name: !DUMP_FILE_NAME!, !DUMP_FILE!     %~dp0svnrdump --username %SVN_USERNAME% --password %SVN_PASS% --non-interactive --trust-server-cert --no-auth-cache dump !REPO_URL! -r !INCRE_LAST_DELTA!:!LAST_REV! --incremental > !DUMP_FILE!     echo %Date:~-4%%Date:~-7,2%%Date:~-10,2%-%Time% %~dp0%ZIP% a -tzip  !DUMP_FILE!.zip !DUMP_FILE! -v%MAX_SIZE%     call %~dp0%ZIP% a -tzip  !DUMP_FILE!.zip !DUMP_FILE! -v%MAX_SIZE%     IF EXIST !DUMP_FILE!.zip DEL /F !DUMP_FILE!     IF EXIST !DUMP_FILE!.zip.* DEL /F !DUMP_FILE!     ECHO Revision: !LAST_REV! > !LAST_DELTA_FILE!     IF EXIST "!LAST_DELTA_FILE!" GOTO DELTA     GOTO :END

REM Do a full backup 
  :FULL     ECHO %Date:~-4%%Date:~-7,2%%Date:~-10,2%-%Time% FULL     ECHO FULL > !LAST_DELTA_FILE!     SET DUMP_FILE_NAME=!Today!-!Now!-INITIAL-R!LAST_REV!.dump     SET DUMP_FILE=!DUMP_FILE!\!DUMP_FILE_NAME!     ECHO %Date:~-4%%Date:~-7,2%%Date:~-10,2%-%Time% Dump file name: !DUMP_FILE_NAME!     ECHO %Date:~-4%%Date:~-7,2%%Date:~-10,2%-%Time% Dump file: !DUMP_FILE!     %~dp0svnrdump --username %SVN_USERNAME% --password %SVN_PASS% --non-interactive --trust-server-cert --no-auth-cache dump !REPO_URL! -r 0:!LAST_REV! > !DUMP_FILE!     echo %Date:~-4%%Date:~-7,2%%Date:~-10,2%-%Time% %~dp0%ZIP% a -tzip !DUMP_FILE!.zip !DUMP_FILE! -v%MAX_SIZE%     call %~dp0%ZIP% a -tzip !DUMP_FILE!.zip !DUMP_FILE! -v%MAX_SIZE%     IF EXIST !DUMP_FILE!.zip DEL /F !DUMP_FILE!     IF EXIST !DUMP_FILE!.zip.* DEL /F !DUMP_FILE!     ECHO Revision: !LAST_REV! > !LAST_DELTA_FILE!     IF EXIST "!LAST_DELTA_FILE!" GOTO DELTA     GOTO :END

:END     ECHO %Date:~-4%%Date:~-7,2%%Date:~-10,2%-%Time% END     GOTO :EOF


Linux svnbackup.sh Shell script:



#!/bin/sh
#SETUP
REPO_URL=https://url:8443/base/project
REPO_PATH=c:/repository/project
DUMP_DIR=/home/user/dumps
LAST_DELTA_FILE=svn_backup_delta.txt
#BEGINING OF THE SCRIPT ITSELF
date=$(date +%Y%m%d)
LOG=dump-$date.log
LAST_REV=$(svn info $REPO_URL |grep Revision |gawk ' { print $2}')
#Both files MUST exist, otherwise, wer'e going to init new "delting"
if [ -e $LAST_DELTA_FILE ] ; then
LAST_DELTA=$(cat $LAST_DELTA_FILE)
if [ $LAST_DELTA -ne $LAST_REV ] ; then
DUMP_FILE_NAME=$date-INCREMENTAL-R$((LAST_DELTA+1))-TO-R$LAST_REV.dump.gz
DUMP_FILE=$DUMP_DIR/$DUMP_FILE_NAME
svnadmin dump $REPO_PATH -r $((LAST_DELTA +1)):$LAST_REV --incremental --deltas 2>>$LOG |gzip -c > $DUMP_FILE
else
echo nothing to do : last revision number is same as last delta number, run it again later !
exit 0
fi
else
DUMP_FILE_NAME=$date-INITIAL-R$LAST_REV.dump.gz
DUMP_FILE=$DUMP_DIR/$DUMP_FILE_NAME
svnadmin dump $REPO_PATH 2>>$LOG | gzip -c > $DUMP_FILE
fi
#Is all ok ?
if [ $? -eq 0 ] ; then
echo -n $LAST_REV > $LAST_DELTA_FILE
else
echo "ERROR :" $date "Problem occured while saving dump" >>$LOG
fi

Update 02/07/2010: Script tested and working in Windows 7 (in combination with Visual SVN server). Make sure to add the bin folder of Visual SVN server to your 'path' system variable to make the 'svn' command available system wide. Run the script with right-click 'Run as administrator'.

 

Update 27/06/2016 on Windows script to use a backup_properties.txt, repo.txt files and validate the retrieved revision and delta numbers before executing the windows backup

Friday, March 27, 2009

Windows File Association fixes

On Windows it can happen that some (system) files can not be opened anymore with their default build in application. Such can happen for example after a failed installation of some software application. To easily fix the default file associations, one could use the registry files provided on the site of Doug Knox (for Windows XP).

He provides registry files to fix following file types:

If your EXE file associations are corrupted, it can be difficult to open REGEDIT, or to even import REG files.  To work around this, press CTRL-ALT-DEL and open Task Manager.  Once there, click File, then hold down the CTRL key and click New Task (Run).  This will open a Command Prompt window.  Enter REGEDIT.EXE and press Enter.

Thursday, February 19, 2009

BareGrep + BareTail + UltraEdit


Monitoring very large (log)files can be done very easily with BareGrep and BareTail. Those very little applications can make a big difference when it comes to productivity and speed during monitoring of production critical systems.

Although the applications are running fine, I was often missing some small extra features. So I made some small AutoHotKey script to get what I wanted:
  • Within BareGrep: press Ctrl+Shift+U: open the file in UltraEdit and scroll to the line that was selected within BareGrep
  • Within BareGrep: press Ctrl+Shift+F: open the selected file in BareTail and find the selected line.
Besides these extra keyboard shortcuts, I also made some registry changes to add extra options in the context menu when right-clicking on a file or folder: 'Grep this folder' option will be added, which will launch BareGrep with the correct folder selected.

My complete automated setup of BareTail+ can be downloaded here. All source files are included.

Tested on Windows XP, no uninstaller available.
Update 14/09/2010: new version of AutoHotKeyScript with properties for paths of BareGrep, BareTail and editor. New version of the 'grep this folder' explorer integration.

Update 23/11/2010 v1.1: new version with support for Notepad++ editor linenumber syntax ( -n)
Update 10/03/2016 v2.0: new version with support for Ctrl+Shift+e to view selected file in Explorer, Ctrl+Shift+N (same as Ctrl+Shift+U) open selected file in text editor, integrate admin elevation when enabling or disabling explorer integration to write to registry, store user config in %userprofile% dir instead of program files dir so no admin elevation is required for preference update and config storage.