DELETE FILE WITH FOLDER XP_CMDSHELL - sql

I need to delete file with folder using sql
so i am using xp_cmdshell.
My Folder structure is
Folder-1
|
|
Folder 2 ----------------------------------------------Folder -3
| |
files.csv files.csv
I need to delete Folder-1, so that it will delete Folder 2 and folder 3 and the files containing it.
I tried using set #cmd= 'RMDIR "C:\Folder-1'
exec master..xp_cmdshell #cmd
RMDIR needs the folder to be empty. so we need to first delete the file using del command.
Then using RMDIR to delete folder 2 and folder 3, then using RMDIR to delete folder-1
Is there anyway to delete the folder with file using single command by xp_cmdshell

Try RMDIR /S /Q C:\Folder-1
The /S Removes all files and directories in the specified directory/folder. the /Q is quiet mode and will not ask if its ok to delete.
HTH

Related

batch: create relative folders based on archives name and only then extract multiple .rar archives in their respective folder

I have these .rar/zip archives into C:\test
test1.rar
test2.rar
Inside test1.rar I haven't folders, just files while in test2.rar I have also a folder inside
I try to extract every rar/zip archives into relative folders with archive content into C:\test2. I want to use same name of archives for relative folder name
test1 <-- in this folder there are only files because test1.rar had not folders within
test2 <-- content should extracted preserving structure folder inside archive
Script that I use is this
#echo off
set "SourceFolder=C:\test"
set "TargetFolder=C:\test2"
if not exist "%TargetFolder%" md "%TargetFolder%"
"C:\Program Files (x86)\WinRAR\Rar.exe" x -cfg- -idq -r -y "%SourceFolder%\*.rar" "%TargetFolder%"
del /F /Q /S "%SourceFolder%\*.rar">nul
for /D %%D in ("%SourceFolder%\*") do rd "%%D" 2>nul
but this script works bad
After extraction this script delete my .rar archives from Source Folder, but I don't want this
It doesn't create test1 folder before to move test1.rar content inside test1 folder. It simply extract content
THIS is output that I expected
C:\test2
|
|--- test1 [folder]
| |
| |--a.txt
| |--b.txt
|
|--- test2 [folder]
|
|--simple [folder]
| |
| |- c.txt
|
|-d.txt
But I get this bad output
C:\test2
|
|
|--a.txt
|--b.txt
|--simple [folder]
| |
| |- c.txt
|
|-d.txt
Solution is this. Thanks to #Mofi for -ad switch
#echo off
set "SourceFolder=C:\temp"
set "TargetFolder=C:\temp2"
if not exist "%TargetFolder%" md "%TargetFolder%"
"C:\Program Files\WinRAR\Rar.exe" x -ad -cfg- -idq -r -y "%SourceFolder%\*.rar" "%TargetFolder%"
for /D %%D in ("%SourceFolder%\*") do rd "%%D" 2>nul

Using 7-zip to add folders to separate archive (windows)

With Winrar I can use this command for create archives for each folder
for folder in */; do rar a -m0 -r "${folder%/}.rar" "$folder"; done
I use windows and I want cloning this command using 7z Console because software is more strong.
I try also this batch
#ECHO OFF
FOR %%A IN (*) DO "c:\program files\7-zip\7z.exe" a -mx9 -m0=lzma2:d256m "%%~nA.7z" "%%A"
but archive me only files, not separated folders.
Solution here
for /d %%X in (*) do "c:\Program Files\7-Zip\7z.exe" a "%%X.zip" "%%X\"

Uninstalling IE 10 Using a batch file

I found a way to uninstall IE 10 using a batch file which below and it works great like it's suppose to. It uninstalls and restarts the computer after about a minute.
FORFILES /P %WINDIR%\servicing\Packages /M Microsoft-Windows-InternetExplorer-10..mum /c "cmd /c echo Uninstalling package #fname && start /w pkgmgr /up:#fname /norestart /quiet"
shutdown -r
But I wanted to take this further and Hide IE 10 from windows updates which will prevent windows to install this update actually in the future and to do that, Microsoft provides an executable file which you can download from here
http://www.microsoft.com/en-us/download/details.aspx?id=36512 After extracting this executable, you get 3 files, IE10_Blocker.adm, IE_blocker.cmd and IE10_BlockerHelp.htm and Microsoft instructions are "In the Command Prompt, goto the folder where you have extracted these 3 files and type “ie9_blocker.cmd /B” and hit Enter to set the blocker on the machine."
I decided to make one batch file for all this so right now I got this far.
FORFILES /P %WINDIR%\servicing\Packages /M Microsoft-Windows-InternetExplorer-10..mum /c "cmd /c echo Uninstalling package #fname && start /w pkgmgr /up:#fname /norestart /quiet"
mkdir “C:\IE10”
copy /Y \file01p\Users\test\public\IE Update blocker\IE10*.* “C:\IE10”
start /d C:IE10\IE10_Blocker.cmd /b
shutdown -r
mkdir “C:\IE10” This command is suppose to create the directory called IE10 on C drive
copy /Y \file01p\Users\test\public\IE Update blocker\IE10*.* “C:\IE10” This command will copy those executable from the network public folders to their C:\IE10 which I just created in the previous step.
This script doesn't work. Only up to uninstalling IE 10 it works but it doesn't create the directory and so it doesn't copy the files from the network. How can do this?
Thanks
Thanks
Try running your batch as administrator.
in an administrator : command prompt it makes the directory without issue.
also, it could be that you do not have access to make the directory on C:
you could try locating it elsewhere that you are sure you have write access.

Calling one sql file from another

Is there an easy way to call one sql file from another?
We have 2 sql files - a.sql and b.sql for our products. Locally we create the db's A and B and then run a.sql an b.sql.
Now, there is another team that has been maintaining an sql file MasterFile.sql which is supposed to create all the dbs needed for this project and configure them. We now need to ensure that the contents of our sql files (a.sql and b.sql) are also called from this masterFile.sql.
Both a.sql and b.sql are subject to change in the near future and therefore we dont want to manually add the contents to MAsterFile.sql.
What is the best way to do this? I tried doing sqlcmd as well as source and :r a.sql, but none of them worked.
How about using a batch file with 3 lines
sqlcmd /S servername /d databaseName /E /i "master.sql"
sqlcmd /S servername /d databaseName /E /i "a.sql"
sqlcmd /S servername /d databaseName /E /i "b.sql"
You can use xp_cmdshell and sqlcmd, as explained in this question:
Is it possible to execute a text file from SQL query?

Run all SQL files in a directory

I have a number of .sql files which I have to run in order to apply changes made by other developers on an SQL Server 2005 database.
The files are named according to the following pattern:
0001 - abc.sql
0002 - abcef.sql
0003 - abc.sql
...
Is there a way to run all of them in one go?
Create a .BAT file with the following command:
for %%G in (*.sql) do sqlcmd /S servername /d databaseName -E -i"%%G"
pause
If you need to provide username and passsword
for %%G in (*.sql) do sqlcmd /S servername /d databaseName -U username -P
password -i"%%G"
Note that the "-E" is not needed when user/password is provided
Place this .BAT file in the directory from which you want the .SQL files to be executed, double click the .BAT file and you are done!
Use FOR. From the command prompt:
c:\>for %f in (*.sql) do sqlcmd /S <servername> /d <dbname> /E /i "%f"
In the SQL Management Studio open a new query and type all files as below
:r c:\Scripts\script1.sql
:r c:\Scripts\script2.sql
:r c:\Scripts\script3.sql
Go to Query menu on SQL Management Studio and make sure SQLCMD Mode is enabled
Click on SQLCMD Mode; files will be selected in grey as below
:r c:\Scripts\script1.sql
:r c:\Scripts\script2.sql
:r c:\Scripts\script3.sql
Now execute
The easiest way I found included the following steps (the only requirement is it to be in Win7+):
open the folder in Explorer
select all script files
press Shift
right click the selection and select "Copy as path"
go to SQL Server Management Studio
create a new query
Query Menu, "SQLCMD mode"
paste the list, then Ctrl+H, replace '"C:' (or whatever the drive letter) with ':r "C:' (i.e. prefix the lines with ':r ')
run the query
It sounds long, but in reality is very fast (it sounds long as I described even the smallest steps).
Make sure you have SQLCMD enabled by clicking on the Query > SQLCMD mode option in the management studio.
Suppose you have four .sql files (script1.sql,script2.sql,script3.sql,script4.sql) in a folder c:\scripts.
Create a main script file (Main.sql) with the following:
:r c:\Scripts\script1.sql
:r c:\Scripts\script2.sql
:r c:\Scripts\script3.sql
:r c:\Scripts\script4.sql
Save the Main.sql in c:\scripts itself.
Create a batch file named ExecuteScripts.bat with the following:
SQLCMD -E -d<YourDatabaseName> -ic:\Scripts\Main.sql
PAUSE
Remember to replace <YourDatabaseName> with the database you want to execute your scripts. For example, if the database is "Employee", the command would be the following:
SQLCMD -E -dEmployee -ic:\Scripts\Main.sql
PAUSE
Execute the batch file by double clicking the same.
General Query
save the below lines in notepad with name batch.bat and place inside the folder where all your script file are there
for %%G in (*.sql) do sqlcmd /S servername /d databasename -i"%%G"
pause
EXAMPLE
for %%G in (*.sql) do sqlcmd /S NFGDDD23432 /d EMPLYEEDB -i"%%G"
pause
sometime if login failed for you please use the below code with username and password
for %%G in (*.sql) do sqlcmd /S SERVERNAME /d DBNAME -U USERNAME -P PASSWORD -i"%%G"
pause
for %%G in (*.sql) do sqlcmd /S NE8148server /d EMPLYEEDB -U Scott -P tiger -i"%%G"
pause
After you create the bat file inside the folder in which your Script files are there just click on the bat file your scripts will get executed
You could use ApexSQL Propagate. It is a free tool which executes multiple scripts on multiple databases. You can select as many scripts as you need and execute them against one or multiple databases (even multiple servers). You can create scripts list and save it, then just select that list each time you want to execute those same scripts in the created order (multiple script lists can be added also):
When scripts and databases are selected, they will be shown in the main window and all you have to do is to click the “Execute” button and all scripts will be executed on selected databases in the given order:
I wrote an open source utility in C# that allows you to drag and drop many SQL files and start running them against a database.
The utility has the following features:
Drag And Drop script files
Run a directory of script files
Sql Script out put messages during execution
Script passed or failed that are colored green and red (yellow for running)
Stop on error option
Open script on error option
Run report with time taken for each script
Total duration time
Test DB connection
Asynchronus
.Net 4 & tested with SQL 2008
Single exe file
Kill connection at anytime
What I know you can use the osql or sqlcmd commands to execute multiple sql files. The drawback is that you will have to create a script for both the commands.
Using SQLCMD to Execute Multiple SQL Server Scripts
OSQL (This is for sql server 2000)
http://msdn.microsoft.com/en-us/library/aa213087(v=SQL.80).aspx
#echo off
cd C:\Program Files (x86)\MySQL\MySQL Workbench 6.0 CE
for %%a in (D:\abc\*.sql) do (
echo %%a
mysql --host=ip --port=3306 --user=uid--password=ped < %%a
)
Step1: above lines copy into note pad save it as bat.
step2: In d drive abc folder in all Sql files in queries executed in sql server.
step3: Give your ip, user id and password.
I know this question is more focused on SQL Server. I had the same question, but for PostgreSQL. The solution is very close for what I needed, so I thought I would share what I got for anyone that needs it:
for %f in (*.sql) do psql -U [username] -d [database name] --command="\i %f";
I ran this from the folder containing all of my sql scripts.
To avoid being prompted for a password, I had to add
*:*:*:[user]:[password]
to my pgpass.conf file that lives in
%APPDATA%\Roaming\postgresql\
folder on windows. I had to create the file myself.
You can create a single script that calls all the others.
Put the following into a batch file:
#echo off
echo.>"%~dp0all.sql"
for %%i in ("%~dp0"*.sql) do echo #"%%~fi" >> "%~dp0all.sql"
When you run that batch file it will create a new script named all.sql in the same directory where the batch file is located. It will look for all files with the extension .sql in the same directory where the batch file is located.
You can then run all scripts by using sqlplus user/pwd #all.sql (or extend the batch file to call sqlplus after creating the all.sql script)
For executing every SQLfile on the same directory use the following command:
ls | awk '{print "#"$0}' > all.sql
This command will create a single SQL file with the names of every SQL file in the directory appended by "#".
After the all.sql is created simply execute all.sql with SQLPlus, this will execute every sql file in the all.sql.
If you can use Interactive SQL:
1 - Create a .BAT file with this code:
#ECHO OFF ECHO
for %%G in (*.sql) do dbisql -c "uid=dba;pwd=XXXXXXXX;ServerName=INSERT-DB-NAME-HERE" %%G
pause
2 - Change the pwd and ServerName.
3 - Put the .BAT file in the folder that contains .SQL files and run it.