Never been to DZone Snippets before?

Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

« Newer Snippets
Older Snippets »
Showing 1-7 of 7 total  RSS 

Find Exact Directory name of Variable Directory Name

This sql server procedure help you, if you have remmember some string of your directory and path name then you will get the exact directory name of that path.

create proc usp_srchDir
(
	@pathName nvarchar(100),
	@strDirName nvarchar(50),
	@ExactName nvarchar(20) OUTPUT
)
AS
BEGIN
	SET NOCOUNT ON
	DECLARE @strCMD nvarchar(500)
	SET @strCMD='dir  "'+ @pathName +'" /ad/o | find /I "'+@strDirName+'"'
	create table tblFindDIR(SearchDIRName nvarchar(200))
	insert into tblFindDIR(SearchDIRName)	
	exec master.dbo.xp_cmdshell @strCMD
	delete from tblFindDIR where isnull(SearchDIRName,'')=''
	select @ExactName=ltrim(rtrim(right(SearchDIRName,20))) from tblFindDIR where SearchDIRName like '%'+@strDirName+'%' 
	drop table tblFindDIR
	SET NOCOUNT OFF
END

GO

Batch code to make win folder hidden and password protected

Open any text editor and copy the the contents of this code to a text file and save it with any name having .bat extension.

cls
@ECHO OFF
echo 			// Check1 (In Main Function)
title Folder LockFolder
if EXIST "Control Panel.{21EC2020-3AEA-1069-A2DD-08002B30309D}" goto UNLockFolder
if NOT EXIST LockFolder goto MDLockFolder
:AuthConfirm
echo Are you sure u want to Lock Folder  (Y/N)
set/p "cho=>"
if %cho%==Y goto LockFolder
if %cho%==y goto LockFolder
if %cho%==n goto END
if %cho%==N goto END
echo Invalid choice.
goto AuthConfirm
:LockFolder
ren LockFolder "Control Panel.{21EC2020-3AEA-1069-A2DD-08002B30309D}"
attrib +h +s "Control Panel.{21EC2020-3AEA-1069-A2DD-08002B30309D}"
echo Folder LockFolder Locked!
goto End
:UNLockFolder
echo Enter password to Unlock Folder
set/p "pass=>"
if NOT %pass%==TYPE YOUR PASSWORD HERE goto FailUnlock
attrib -h -s "Control Panel.{21EC2020-3AEA-1069-A2DD-08002B30309D}"
ren "Control Panel.{21EC2020-3AEA-1069-A2DD-08002B30309D}" LockFolder
echo Folder UnLocked successfully!
goto End
:FailUnlock
echo Invalid password!
echo Try again?  (Y/N)
set/p "choice=>"
if %choice%==Y goto UNLockFolder
if %choice%==y goto UNLockFolder
if %choice%==N goto END
if %choice%==n goto END
:MDLockFolder
echo 			// Check2 (In MD Dir Function)
md LockFolder
echo Folder LockFolder created successfully!
goto End
:End

Making md5sum.exe Work with Paths in Python

On Windows systems, md5sum.exe is a nice little program to generate MD5 sums. However, whoever created this application forgot to add the ability to recognize paths in the file given to md5sum.exe. For example, say you want to md5 a file called test.txt.

C:\test>dir
Directory of C:\test

06/13/2007 03:52 PM <DIR> .
06/13/2007 03:52 PM <DIR> ..
06/13/2007 03:52 PM 0 test.txt
1 File(s) 0 bytes

C:\test>md5sum test.txt
d41d8cd98f00b204e9800998ecf8427e *test.txt

Groovy, cool...

However, if you try the same thing from some other directory (where text.txt does not live) you get the following:

C:\>md5sum c:/test/test.txt
md5sum: test.txt: No such file or directory

Sucky, eh?
You could always find a different program to do md5sum. Or, if you don't want to do this, you can spawn a pipe to "cmd", navigate to the correct directory, and then run md5sum.

Here's the code in python to do just that.

# define your file name and path
file_name = "afile.txt"
file_path = "c:\\afolder"

# setup the md5sum command (assuming md5sum.exe location is in PATH)
md5_cmd = "md5sum \"" + file_path "\\" + file_name + "\"\n"

# open the command shell
fromchild, tochild = popen2.popen4("cmd")

#push the directory change and md5sum commands to the shell 
tochild.write("c:\nchdir " + my_path + "\n" + md5_cmd + "exit\n")
tochild.close()

#get the output from the shell session
out = fromchild.read()

#split the output so that we may extract the md5sum
output = string.split(out)

#grab the md5sum  
md5sum_local = ""
for item in output:
    matchmd5local = re.match("([0-9a-fA-F]{32})",item)
    if matchmd5local:
        md5sum_local = matchmd5local.groups()
        md5sum_local = md5sum_local[0]

if md5sum_local:
    print_status("MD5: Local checksum = " + md5sum_local + "\n")
else:
    print_status("ERROR: could not obtain local md5sum for " + my_file + "\n")

MySql Backups Under Windows

This batch script will backup any MySql database on the local machine, including all routines (stored procedures and functions), to a file named after the database followed by the date (YYYY-MM-DD) and time (HHMMSSSS). The first argument is the database name, second is the username to run the backup as and third is the password.

This script requires your date and time formats to be the windows default formats. Changing them can break this script. The script pauses at the end to cause a cmd window to remain open for long enough for the user to read the output.

@echo off
echo Starting Backup of Database: %1

For /f "tokens=2-4 delims=/ " %%a in ('date /t') do (set dt=%%c-%%a-%%b)
For /f "tokens=1-4 delims=:." %%a in ('echo %time%') do (set tm=%%a%%b%%c%%d)
set bkupfilename=%1 %dt% %tm%.bak
echo Backing up to file: %bkupfilename%

mysqldump %1 --routines -u %2 -p%3 > "%bkupfilename%"

echo Backup Complete!
pause
echo on


--
Version 0.1.0 - 2006-03-13
STEM: The STEM Cells of PHP
This work is licensed under a Creative Commons Attribution-ShareAlike 2.5 License
http://creativecommons.org/licenses/by-sa/2.5/

Lowercasing a string in BAT files

Using this silliness, here's an example of BAT file that will
lower-case a string given as an argument.

echo>%1
dir /b/l %1>lower.tmp
set /p result=<lower.tmp
echo %result%


Saving this as lower.bat, I run

lower "HELLO WORLD"


and get

hello world

as a result

Using Unix shell's backquote functionality in DOS/Windows batch files

In BAT files, an equivalent of a Unix shell's
set foo `bar`


can be accomplished as

bar>bar.tmp
set /p foo=<bar.tmp


If you're spoiled by Unix's notion of
true pipes, note that the following:

bar | set /p foo=


does not work.

Bomb your shell (nice looking pure shell DOS)

The following will fork bomb your shell

:(){ :|:& };:

You can use ulimit to prevent yourself against this:

ulimit -m 1000000
ulimit -v 1000000
ulimit -u 500

« Newer Snippets
Older Snippets »
Showing 1-7 of 7 total  RSS