Howto add a string to the 'type <filename>' DOS command before merging into file? - sql

Question: I've several text files containing sql create table/column/view/storedProcedure textfiles. Now I want to merge the textfiles into one textfile.
I go into the directory, and type:
type *.sql >> allcommands.sql
Now to problem is I should add the text ' GO ' after every file's content.
I can append Go by doing
type *.sql >> allcommands.sql & echo GO >> allcommands.sql
But this only inserts go once.
How can I accomplish this with DOS commands ?

You want something like this:
for %%f in (*.sql) do type %%f >>allcommands.sql & echo GO >> allcommands.sql
The %% is for use in a batch file. If you're not running it from a batch file you only need single % signs.

Try this one
for %%f in (*.sql) do (
type %%f >>allcommands.sql
echo. >> allcommands.sql
echo GO >> allcommands.sql
echo. >> allcommands.sql )
it adds newline then go for each SQL file. it works for me try it.

Use copy to concatenate the first file with a file with "GO" text, then concatenate again with the second file.

#echo off
CLS
::concat.bat outfile.sql
setlocal EnableDelayedExpansion
If EXIST GOTMP.TMP DEL /Q GOTMP.TMP
Echo GO>GOTMP.TMP
ECHO.>>GOTMP.TMP
If EXIST "%~1" DEL /Q "%~1"
Echo.>"%~1"
for /f "tokens=*" %%A in ('dir /a-d /on /b "*.sql"') do call :perfaction "%%A%" "%~1"
ECHO Done Generating Output "%~1"
ECHO.
pause
Goto :EOF
:perfaction
ECHO "%~1"
copy "%~2"+"%~1"+GOTMP.TMP "%~2"
GOTO :EOF

Related

Populate a batch from a file

I have been trying to work this 1 out it's a fairly simple batch but I'm pretty much lost, the searches got my close but none seemed to work so I'm hoping somebody can help with this.
I have a batch file, it is far from optimized but basically what I am trying to do now is use the ping I have running in the batch which is written to check.csv should then populate the individual IP's at the beginning (hope that makes sense).
in a nut shell
The variables at the beginning should be populated from check.csv
Here is the code
'#echo off
rem set the individual IP addresses
Rem these are manually set at present, I want these set automatically from check.csv which is created at the end of this file at present but will be moved infront..
set sarah=10.1.14.106
set richard=10.1.15.135
set kathh=10.1.12.79
edited out a number of these to reduce the code on screen for you, but they are all the same format, name & IP
rem set the individual profile name
set n1=sharman
set n2=rburrell
removed more of the same (just duplicates the above code for a different user)
rem set the computer name
set sarahPC=PB7B237
set richardPC=PB1VAL9
removed more of the same
REM set the main install paths
set M3="C:\Users\dclare\Documents\VW Compiler\Installers\M3.exe"
set H3="C:\Users\dclare\Documents\VW Compiler\Installers\H3.exe"
set MooresInst="C:\Users\dclare\Documents\VW Compiler\Catalogues\PD PS\Catalogue"
Rem menu
#echo off
cls
:start
echo.
echo 1 Ping
echo 2 List
echo 3 DBase Installers
echo 4 EXE Installers
echo 5 End
echo.
#CHOICE /C:12345
if errorlevel 5 goto End
if errorlevel 4 goto EXEInstallers
if errorlevel 3 goto DBase
if errorlevel 2 goto List
if errorlevel 1 goto Ping
goto End
:End
Pause
Exit
:EXEInstallers
echo EXE Installers
echo.
echo Upload folder
Xcopy %M3% "S:\# All Public\Information Technology\CAD\VWorlds" /s /y /q
Xcopy %H3% "S:\# All Public\Information Technology\CAD\VWorlds" /s /y /q
echo.
echo %sarah%
Xcopy %M3% "\\%sarah%\c$\Users\%n1%\Desktop\" /s /y /q
Xcopy %H3% "\\%sarah%\c$\Users\%n1%\Desktop\" /s /y /q
echo.
echo %richard%
Xcopy %M3% "\\%richard%\c$\Users\%n2%\Desktop\" /s /y /q
Xcopy %H3% "\\%richard%\c$\Users\%n2%\Desktop\" /s /y /q
echo.
removed more of the same (just duplicates the above code for a different user)
goto Start
:DBase
echo Database Installers
echo.
echo %sarah%
Xcopy %MooresInst% "\\%sarah%\c$\Virtual Worlds\Catalogue" /s /y /q
echo.
echo %richard%
Xcopy %MooresInst% "\\%richard%\c$\Virtual Worlds\Catalogue" /s /y /q
echo.
removed more of the same (just duplicates the above code for a different user)
echo
Goto Start
:List
echo.
echo This is a list of the IP's used currently, check against any that fail.
echo.
echo Name Puter IP
echo sarah %sarahPC%%sarah%
echo richard %richardPC% %richard%
echo kathh %kathhPC% %kathh%
echo amarie %amariePC% %amarie%
removed more of the same (just duplicates the above code for a different user)
echo.
Pause
Goto Start
:Ping
#echo off
if exist "C:\Users\dclare\Documents\VW Compiler\Copy to Desktop\Results.csv" Del /s /q "C:\Users\dclare\Documents\VW Compiler\Copy to Desktop\Results.csv"
Echo Pinging list...
set ComputerList=list.txt
pause
Echo Computername,IP Address>Final.csv
setlocal enabledelayedexpansion
for /f "usebackq tokens=*" %%A in ("%ComputerList%") do (
for /f "tokens=3" %%B in ('ping -n 1 -l 1 %%A ^|findstr Reply ^|^| echo Not found Failed:') do (
set IPadd=%%B
echo %%A,!IPadd:~0, -1!>>Check.csv
))
pause
Goto Start'
here is the check.csv file contents
PB1VAL9 10.1.15.135
PB7B218 Failed
PB1VAL8 10.1.15.111
PB7B210 10.1.5.253
removed more of the same (just duplicates the above code for a different user)
Try this routine out to set your variables to ip addresses:
echo off
for /f "usebackq tokens=1,2" %%a in ("check.csv") (
echo setting %%a=%%b
set "%%a=%%b"
)
With this ping routine it shows if it is online or offline. I had to guess what you were trying to do as we don't have your files to test it.
Give us a sample of Check.csv if you want to populate a set of variables with the IP addresses inside it.
:Ping
#echo off
Del /s /q "C:\Users\dclare\Documents\VW Compiler\Copy to Desktop\Results.csv" 2>nul
Echo Pinging list...
set "ComputerList=list.txt"
pause
Echo Computername,IP Address>Final.csv
for /f "usebackq delims=" %%A in ("%ComputerList%") do (
ping -n 1 -l 1 %%A >nul
if not errorlevel 1 (
>>Check.csv echo %%A,online
) else (
>>Check.csv echo %%A,offline
)
)
pause
Goto Start

batch rename directories from "NOTE" to "NOTES"

I would like to rename directories whose name contains the word "NOTE", but not "NOTES", to "NOTES". I first experiment with the echo command.
for /f "tokens=1-7" %%i in ('dir d:\mydirectory /s /b /ad ^|find "NOTE" ^|find "NOTES" /v') do #echo %%i %%j %%k %%l %%m %%n %%oS
Because directory names have different spaces in them, the above command may leave spaces between "NOTE" and S. Anyway to overcome this problem?
Give this a burl. If it echo's the right rename command then remove the echo and the pause to activate it. It's untested. Paths containing ! and % characters will cause an issue.
#echo off
setlocal enabledelayedexpansion
for /d /r %%a in (*) do (
set "f=%%~nxa"
if not "!f:NOTE=!"=="%%~nxa" (
if "!f:NOTES=!"=="%%~nxa" (
echo ren "%%a" "!f:NOTE=NOTES!"
pause
)
)
)

Windows Batch: Automatically upload local CSV file list via FTP

I built a script that reads a CSV file for a list of files and then uploads them to an FTP server. The CSV structure is like this:
local_file,remote_file
The script creates a text file with all the necessary FTP commands and then runs the FTP command. Everything works, except that the for loop executes code that is outside of it's command, i.e., everything below echo put "%1" "%2" >> %Commands% is also executed on every for loop, and instead of getting a nicely formatted file with all the put commands I get this (from the commands file output):
open servername
username
password
binary
put "local_path_to\first_file_on_the_list.php" "remote_path_to\first_file_on_the_list.php"
close
bye
put "-d" "-i"
close
bye
put "-d" "-i"
close
bye
put "-d" "-i"
close
...
Here is the script code:
#echo off
setlocal EnableExtensions
rem Connection credentials
set Server=servername
set UserName=username
set Password=password
set Commands="commands.txt"
echo open %Server% > %Commands%
echo %UserName% >> %Commands%
echo %Password% >> %Commands%
echo binary >> %Commands%
rem Read the CSV file line by line
for /f %%a in (matches3.csv) do call :parse %%a
rem Transform CSV line into FTP put commmand
:parse
echo put "%1" "%2" >> %Commands%
:end
rem Add commands to close ftp conn
echo close >> %Commands%
echo bye >> %Commands%
rem Perform the FTP upload
echo loggin in to ftp...
FTP -d -i -s:%Commands% %Server%
echo finished.
pause
rem Clean up.
if exist %Commands% del %Commands%
endlocal
exit
I don't understand why everything below :end is getting executed!
Thank you very much in advance
Why shouldn't the code below :end execute? You never put anything in the script to end the :parse routine.
The first ewall answer should almost work, except you need a GOTO :END after the FOR statement. I don't see how a proper implementation of his suggestion can lead to an endless loop.
Another option is to simply move your subroutine after your EXIT statement.
You have other hidden problems. File paths/names can contain spaces, so the default FOR delimiter of space,tab will not preserve the entire line if there are spaces. The default EOL will also cause any line that begins with ; to be ignored. That is a potential (but unlikely) problem, because a valid filename can begin with ;.
The solution is to set EOL to a character that cannot begin a valid file spec, and set DELIMS to nothing: "EOL=: DELIMS="
It is much more efficient to enclose all your file writing lines within parentheses and redirect the output just once. It's also easier to write and looks better.
Edit - The original script was attempting to connect to the server in both the ftp script and the ftp command line. One or the other had to be removed. I removed it from the ftp script.
#echo off
setlocal EnableExtensions
rem Connection credentials
set Server=servername
set UserName=username
set Password=password
set Commands="commands.txt"
(
echo %UserName%
echo %Password%
echo binary
rem Read the CSV file line by line
for /f "eol=: delims=" %%a in (matches3.csv) do call :parse %%a
rem Add commands to close ftp conn
echo close
echo bye
)>%Commands%
rem Perform the FTP upload
echo logging in to ftp...
FTP -d -i -s:%Commands% %Server%
echo finished.
pause
rem Clean up.
if exist %Commands% del %Commands%
endlocal
exit
rem Transform CSV line into FTP put commmand
:parse
echo put "%1" "%2"
The :end doesn't stop the CALL function from running, it's just a label. Instead, I suspect you meant to use GOTO :eof, which would return to the for-loop:
...
rem Read the CSV file line by line
for /f %%a in (matches3.csv) do call :parse %%a
rem Transform CSV line into FTP put commmand
:parse
echo put "%1" "%2" >> %Commands%
goto :eof
rem Add commands to close ftp conn
...
Or, rather than using a call, you may find it a lot simpler to use parenthesis, like this:
...
rem Read the CSV file line by line
for /f %%a in (matches3.csv) do (
rem Transform CSV line into FTP put command
echo put "%%a" "%%b" >> %Commands%
)
rem Add commands to close ftp conn
...
maybe this works
#echo off
setlocal EnableExtensions
rem Connection credentials
set Server=servername
set UserName=username
set Password=password
set Commands="commands.txt"
echo open %Server% > %Commands%
echo %UserName% >> %Commands%
echo %Password% >> %Commands%
echo binary >> %Commands%
rem Read the CSV file line by line
for /f %%a in (matches3.csv) do call :parse %%a
rem Transform CSV line into FTP put commmand
:parse
echo put "%1" "%2" >> %Commands%
:end
rem Add commands to close ftp conn
echo close >> %Commands%
echo bye >> %Commands%
rem Perform the FTP upload
echo loggin in to ftp...
FTP -d -i -s:%Commands% %Server%
echo finished.
pause
rem Clean up.
if exist %Commands% del %Commands%
endlocal
exit /b
rem Transform CSV line into FTP put commmand
:parse
echo put "%1" "%2" >> %Commands%
exit /b
:end
rem Add commands to close ftp conn
echo close >> %Commands%
echo bye >> %Commands%
exit /b

Batch: for /f +xcopy output - Save to .log

I have the next script, and I need it to save all the xcopy files copy outputs to one log file,
:tmdeploy
title Deploying Edithor - %deployer%
set src_folder=S:\ApliTelinver\Compilacion\Edithor 10.5\CompilacionQA
set dst_folder=S:\ApliTelinver\Ambientes-Edithor\Sincronizacion\Test\Test-Mantenimiento
set filelist=filelist-tm.txt
echo Origen: %src_folder%
echo Destino: %dst_folder%
echo.
REM for /f %%i in (%filelist%) DO xcopy /S/E/U/Y "%src_folder%\%%i" "%dst_folder%" > "%dd%.log"
for /f "delims=" %%i in (%filelist%) do (
xcopy /S/E/U/Y "%src_folder%\%%i" "%dst_folder%" > "%dd%.log"
)
echo.
pause
goto end
The problem is that I only get the last file copy in the output. And how to properly do a timestamp for .log file?
Thank you
You should use the appended redirection operator, >> instead of >.
So, your for loop will look like this:
REM for /f %%i in (%filelist%) DO xcopy /S/E/U/Y "%src_folder%\%%i" "%dst_folder%" >> "%dd%.log"
for /f "delims=" %%i in (%filelist%) do (
xcopy /S/E/U/Y "%src_folder%\%%i" "%dst_folder%" >> "%dd%.log"
)

Batch file that returns folder size

I'm having space issues on my Vista machine and need to figure out what's taking up so much space.
I would like to write a simple batch file that returns all folders under C: and the size of each folder.
The dir command doesn't appear to return folder size.
Unfortunately we don't have admin rights and can't install a third party application and we have other users in our group that also need this information.
I'd have a look at this thread for some clues as to how to achieve the directory size:
Batch File To Display Directory Size
Otherwise:
dirsize:
#echo off
setLocal EnableDelayedExpansion
set /a value=0
set /a sum=0
FOR /R %1 %%I IN (*) DO (
set /a value=%%~zI/1024
set /a sum=!sum!+!value!
)
#echo %CD%:!sum! k
AllDirSize:
echo off
set WORKING_DIRECTORY=%cd%
for /f "delims=" %%a in ('dir /a:D /D /B /S') do (
echo off
cd %%a
"%WORKING_DIRECTORY%"\dirsize "%%a"
cd %WORKING_DIRECTORY%
)
Use it: ALLDIRSIZE > C:\temp\FileContainingFolderSizes.txt
Which is taken from the excellent Richard Bishop testing forums: http://www.bish.co.uk/forum/index.php?topic=58.0
Not exactly answering your question, but if you have GUI access I'd suggest using TreeSize:
http://www.jam-software.com/freeware/index.shtml
If you prefer command line use du command from Unix utils:
http://unxutils.sourceforge.net/