Running Commands in Batch FIle - postgresql

I am trying to create a batch file to run start some micro services and database
1 FOR /F "tokens=4 delims= " %%P IN ('netstat -a -n -o ^| findstr :1000') DO #ECHO TaskKill.exe /PID %%P
2 FOR /F "tokens=4 delims= " %%P IN ('netstat -a -n -o ^| findstr :1001') DO #ECHO TaskKill.exe /PID %%P
3 FOR /F "tokens=4 delims= " %%P IN ('netstat -a -n -o ^| findstr :5432') DO #ECHO TaskKill.exe /PID %%P
4 start cd "C:\Program Files\PostgreSQL\10\bin\" & pg_ctl.exe -D "c:\Program Files\PostgreSQL\10\data" start
#REM to start service
5 start javaw -jar -Dserver.port=1000 text-annotation-tool-1.0-SNAPSHOT.jar
Line 1 to 3 and Line 5 execute correctly when executed by commenting line 4.
Line 4 is to start a Postgres server in a new prompt (beacuse of the Dir change). I think the problem is with the way I have used quotes. The 'start' at the beginning and ending of line 4 serve different purpose.
Also, if I execute line 4 in different prompt, How do I close the prompt after execution (nohup equivalent)

There are two errors: You can't "pass" cd to the start command. And start has the quirk to interpret the first quoted parameter as the new window's title. So start "C:\Program Files\PostgreSQL\10\bin\" ... wouldn't work, you need to supply a dummy window title. The & also seems wrong
So you need:
start "Postgres Server" "C:\Program Files\PostgreSQL\10\bin\pg_ctl.exe" -D "c:\Program Files\PostgreSQL\10\data" start
As the full path to pg_ctl.exe is supplied, there is no need for a cd.
But if you want to define the default directory for the new process, you have to use the /D parameter:
start "Postgres Server" /D "C:\Program Files\PostgreSQL\10\bin" pg_ctl.exe -D "c:\Program Files\PostgreSQL\10\data" start
Unrelated, but: putting the Postgres data directory into c:\Program Files\ is a very bad idea. That directory has special permissions for a purpose. You should use %ProgramData% or %AppData% instead

Related

How do I configure mongodb to remove old log files?

I've read the mongodb docs and am rotating my log files on Windows 10. However, the number of logs grow until I manually delete them. I'd like to keep 30 days worth of logs.
I was hoping there was a config setting similar to NLog where I can specify how many files to keep before they are re-written. Maybe I am misunderstanding something in the docs. What is the "best practice" for automatically removing old log files?
Looks like the easiest way to do this without 3rd party utilities is to create a batch file that runs each night via Task Scheduler.
years later, but still helpful might be this cmd-script. It's written to remove old stuff in my profile, but really simple to extend:
#echo off
#setlocal
REM Remove everything older then "/D" days (ten/10 days in our examples below)
REM BUT we can not remove directories with files in it, so we do it in two-steps ways:
REM 1st remove all files by using the command: del
REM 2nd remove all files by using the command: rd
REM cleanup %TMP%
forfiles -p "%TMP%" -m * /D -10 /C "cmd /c del /s/q #path > nul 2> nul"
forfiles -p "%TMP%" -m * /D -10 /C "cmd /c rd /s/q #path > nul 2> nul"
REM cleanup %TEMP%
forfiles -p "%TEMP%" -m * /D -10 /C "cmd /c del /s/q #path > nul 2> nul"
forfiles -p "%TEMP%" -m * /D -10 /C "cmd /c rd /s/q #path > nul 2> nul"
REM cleanup the Downloads folder in the current profile
forfiles -p "%USERPROFILE%\Downloads" -m * /D -10 /C "cmd /c del /s/q #path > nul 2> nul"
forfiles -p "%USERPROFILE%\Downloads" -m * /D -10 /C "cmd /c rd /s/q #path > nul 2> nul"
#endlocal
#echo on

powershell $lastexitcode does not work when running batch files

I try to get the exitcode/errorlevel code from a bat file into the powershell script calling the bat file.
Althought the %ErrorLevel% is 1:
BAT file
call aCONFIGURATION
for %%f in (.\createData\*.g.sql) do sqlcmd -b -U %UserName% -P %Password% -S %sqlClonedServer% -d %sqlClonedDatabaseName% -i %%f -r1 1> NUL
echo %ERRORLEVEL%
When I do in the powershellscript
$lastexitcode its ALWAYS 0 but the %ErrorLevel% says 1
$build = "c:\my.bat"
$state = & $build
Write-Host $LASTEXITCODE
I am pulling my hairs of this crap powershell full with bugs.
I have read these links but they did not help as the result is different:
http://blogs.technet.com/b/heyscriptingguy/archive/2011/06/06/get-legacy-exit-codes-in-powershell.aspx
https://social.technet.microsoft.com/Forums/scriptcenter/en-US/05e37b8d-761b-4fd7-881e-977f114ad8d7/obtaining-errorlevel-return-codes-in-powershell-is-it-possible
How can I fix the $lastexitcode
echo %ERRORLEVEL% just writes the error code to stdout, after which the batch file exits normally.
If you want the batch script to actually return an error on exit, use exit /b [exitcode]:
call aCONFIGURATION
for %%f in (.\createData\*.g.sql) do sqlcmd -b -U %UserName% -P %Password% -S %sqlClonedServer% -d %sqlClonedDatabaseName% -i %%f -r1 1> NUL
exit /b %ERRORLEVEL%

looping through files for postgresql give error in windows

i'm trying to read files and load them all csv files, and once done move the files to some other location :
cd E:\data\
for /f %%a in (’dir /b filename*.CSV) do (
psql -U postgre -W password -c "COPY INTO LA from %%a" zipcodes
mv %%a E:\data\bc\
)
but it gives me following error:
E:\data>for /F %a in (ΓÇÖdir /b filename*.CSV) do (
psql -U postgre -W password-c "COPY INTO LA from %a" zipcodes
mv %a E:\data\bc\
)
The system cannot find the file ΓÇÖdir.
thanks for help
#echo off
setlocal enableextensions disabledelayedexpansion
set "psql=c:\wherever\psqlIs\psql.exe"
pushd "e:\data" && (
for %%a in ("filename*.CSV") do (
"%psql%" -U postgre -W password -c "COPY INTO LA from %%~a" zipcodes
move "%%~a" "E:\data\bc\"
)
popd
)
for /f is intended for file/string processing. To iterate over a set of files use a simple for
Anyway, the problem in your code are the opening single quote (as Alex K has pointed), the missing closing quote and the non existing mv command in windows that should be move
edited it seems there are problems with file loading. psql copy command indicates it is better to use full path names (with backslashes doubled), so
#echo off
setlocal enableextensions enabledelayedexpansion
set "psql=c:\wherever\psqlIs\psql.exe"
pushd "e:\data" && (
for %%a in ("filename*.CSV") do (
set "file=%%~fa"
"%psql%" -U postgre -W password -c "COPY INTO LA from E'!file:\=\\!'" zipcodes
move "%%~a" "E:\data\bc\"
)
popd
)

Parse Windows Command Line Output To Get PIDs

I'm looking to gather only the PID value outputted when you run this netstat command:
netstat -a -o -n -p tcp | findstr -i "CLOSE_WAIT"
My intention is to use the PIDs and create a script that will run taskkill /PID pidfoundhere to remove any sockets with CLOSE_WAIT state.
#echo off
for /f "tokens=5" %%a in ('
netstat -noa -p tcp ^| find /i "CLOSE_WAIT"
') do if not "%%a"=="0" echo taskkill /pid %%a
Use for command to split the line using the spaces as delimiters, get the 5th token in the line and if there is PID, kill the process
The taskkill commands are only echoed to console. If the output is correct, remove the echo command

Build a command and execute it from within a batch file

I have a batch file that outputs a list of commands which themselves can be executed at the command line.
Here is a simplified version of what I'm doing:
set %foldername%="c:\my_folder"
set %exename%="c:\my_utility.exe"
cd %foldername%
FOR /F "tokens=*" %%G IN ('dir *.xml /s /b /a:-d') DO #echo %exename% /x="%%G"
This basically outputs a batch file. It looks like this:
c:\my_utility.exe /x="c:\my_folder\file1.xml"
c:\my_utility.exe /x="c:\my_folder\file2.xml"
c:\my_utility.exe /x="c:\my_folder\file3.xml"
c:\my_utility.exe /x="c:\my_folder\file4.xml"
I want to execute these commands. Currently I have to redirect the output to a batch file and then run that. Is there any way to just say "execute this command I just constructed" in the dos prompt?
Just remove echo in the FOR loop:
FOR /F "tokens=*" %%G IN ('dir *.xml /s /b /a:-d') DO %exename% /x="%%G"