PSCP automatic upload newest file - pscp

I am trying to make a bat script that uploades the newest file from a folder to the public html. How to do this? I got this to work at the moment.
c:pscp -pw password c:\index.html user#host:public_html/index.html

Got it:
set source="C:\dir"
FOR /F "delims=|" %%I IN ('DIR %source% /B /O:D') DO SET NewestFile=%%I
c:pscp -pw password %source%\%NewestFile% user#host:public_html/%NewestFile%
pause

Related

Batch file explore directory for PostgreSQL connection

Once again, I am asking for your help to manage a small batch script.
As announced, in a previous post, I'm used to work on linux and automation tasks are more obvious to me but I have to work today under Microsoft environment.
Writting a Batch file is definitly not an easy things.
I am able with this script to be connected to a PostgreSQL database and load data via ogr2ogr. Until then, no problem.
Currently, the path of the folder is hard written in my code, but I would like to have the possibility of choosing the working directory through windows explorer.
Also, concerning this directory, I would like to have the possibility to process the subfolders at the same time.
Here, below the piece of my *.bat code:
TITLE Upload Shapefile Files to PostgreSQL
#echo off
cls
color 9
echo ******************************************************************************
echo * Upload Shapefile Files to PostgreSQL *
echo ******************************************************************************
set /P Host=Please enter your Server Host (default:localhost):
set /P Port=Please enter your PGSQL port (default:5432):
set /p Database=Please enter your PGSQL Database Name (default:postgres):
set /P Schema=Please enter your Edigeo Schema (default:public):
set /P User=Please enter your PGSQL username (default:postgres):
set /P Password=Please enter your PGSQL password (default:postgres):
For /F %%H In ("C:\Users\stephj\Desktop\SHP\*.shp") do ogr2ogr -overwrite -t_srs EPSG:2154 -s_srs EPSG:2154 -f "PostgreSQL" PG:"host=%Host% port=%Port% user=%User% password=%Password% dbname=%Database%" -lco schema=%Schema%
pause
Thanks in advance for your time and your help.
Ok, here is a way. This will just demonstrate by echoing the files, you need to amend it to follow your command structure:
Note!! This is a batch/PowerShell hybrid script. It needs to be saved with a batch file extension, preferably .cmd:
#echo off
set "pshell="(new-object -COM 'Shell.Application').BrowseForFolder(0,'Select Folder',0,0).self.path""
for /f "delims=" %%a in ('powershell %pshell%') do set "workdir=%%a"
for /r "%workdir%" %%i in (*.shp) do echo "%%~i"

Rename only a specific part of every filename in a folder

I want to, for example, replace all -v1 parts in every filename for -v2. It's almost working, but the access is denied because the files are being used by the cmd process itself during execution. My current code is:
for /f "delims=" %%a in ('dir /a-d /b *-v1*') do ren "%%a" "%%a:-v1=-v2"
Any suggestions? :)
EDIT:
I've also tried for /r %%i in ("*-v1*") do ren "%%~nxi" "%%~nxi:-v1=-v2" and it finds the files and uses the correct rename value, but still outputs that it can't access the file because it is in use by another process. I'm sure that process is the cmd.exe itself, because after I close the command prompt, I can change the filenames manually without any problems.
I also tried by writing the current filenames to a temporary .txt file with the idea that the files I want to rename aren't used by any command. Then read the file with the type command within a for loop to rename each file, but same thing.
It's quite frustrating, any help is appreciated :)
substring substituion doesn't work with for variables (%%a). Use a "normal" variable and delayed expansion:
#echo off
setlocal enabledelayedexpansion
for /f "delims=" %%a in ('dir /a-d /b *-v1*') do (
set filename=%%a
ren "%%a" "!filename:-v1=-v2!"
)

PostgreSQL Backup Batch file not working

I have the following batch file/script to backup a PostgreSQL database on a Windows 2012 server which works fine on two servers I have running. On the new server, it works but prompts me for the password. I recall I had the same problem on the other two servers but don't recall what I did to make it work some 4 years ago. Can someone help? I have searched all over including here and have not found the solution yet. I did not write the script. I was passed on to me some 4 years ago
#echo off
setlocal enabledelayedexpansion
REM The next line sets the following DayTime variables: DT_Day, DT_DayOfWeek, DT_Hour, DT_Minute, DT_Month, DT_Quarter, DT_Second, DT_WeekInMonth, DT_Year
for /f "delims=" %%a in ('wmic.exe Path Win32_LocalTime GET * /value') do (for /f "delims=" %%b in ("%%a") do set DT_%%b)
for %%a in (DT_Month DT_Day DT_Hour DT_Minute DT_Second) do (if !%%a! LSS 10 set %%a=0!%%a!)
set Timestamp=%DT_Year%%DT_Month%%DT_Day%_%DT_Hour%%DT_Minute%%DT_Second%
echo Timestamp: %Timestamp%
set PGPassFile=%APPDATA%\postgresql\pgpass.conf
REM database/PostgreSQL Information
SET PRODDB=database_name
SET PGUSER=username
SET PGPASS=password
SET PGHOST=localhost
SET PGPORT=5432
SET PGBIN="C:\Program Files\PostgreSQL\9.5\bin"
SET BACKUPEXT=backup
SET BACKUPDES="C:\Backups"
IF not exist %BACKUPVER% (mkdir %BACKUPDES%\%BACKUPVER%)
REM formats can be custom/plain/tar
SET FORMAT=custom
SET GLOBALS=globals-%Timestamp%.sql
SET BACKUPFILENAME=%PRODDB%.%Timestamp%.%BACKUPEXT%
for %%a in (%PGPASS%) do (>"%PGPassFile%" echo %PG_HOST%:%PG_PORT%:%PRODDB%:%PGUSER%:%%~a)
#ECHO Backing up globals to %GLOBALS%...
%pgbin%\pg_dumpall -U %PGUSER% -p %PGPORT% -g > %BACKUPDES%\%GLOBALS%
#ECHO Backing up %PRODDB% to %BACKUPFILENAME%...
%pgbin%\pg_dump -U %PGUSER% -p %PGPORT% --format=%FORMAT% -C %PRODDB% > %BACKUPDES%\%BACKUPFILENAME%
REM del "%PGPassFile%"
So it was a problem with the ph_hba.conf. I had identical entries on a working server and the new server. It did not work. I copied the pg_hba.conf file from the working server to the new server. The backup/pg_dump script is running now without prompting for a password.
Strange

How to write a script to copy from a windows share (\\....\...) to a certain location in docker

How to write a script to copy from a windows share (\.......) to a certain location in docker.
I have tried the following to find the latest sub-folder in windows share.
FOR /F "delims=" %%i IN ('dir "\\....\.." /b /ad-h /t:c /o-d') DO (
SET a=%%i
GOTO :found
)
echo No subfolder found
goto :eof
:found
echo Most recent sub folder: %a%
Could Someone help me out with the script, as I am still learning?
TIA

using #FILE in forfiles not working

I am trying to build a batch file that deletes all excel files older than 30 days. So far I have the command:
forfiles -p"N:\QC\ATR's" -s -m*.xls* -d-30 -c"CMD /C del /f /q #FILE"
The only problem seems to be the fact that #FILE has a space in the folder/file name so the del command cannot find the specified path. For example, looking at the echoes from the command it says
Could Not Find N:\QC\ATR's\'-5
When it should really be looking for the path
N:\QC\ATR's\-5 brightness study
See what I mean? Is there a way to tell the #FILE variable to be wrapped in quotes or something?
Thanks
You could try the following:
SET FILE = -5 brightness study
forfiles -p"N:\QC\ATR's" -s -m*.xls* -d-30 -c"CMD /C del /f /q %FILE%"
Check these sites for some assistance:
http://ss64.com/nt/path.html
http://www.windowsitpro.com/article/server-management/how-do-i-pass-parameters-to-a-batch-file-
I figured it out. I just added Hex coded qoutes around the file variable
forfiles -p"N:\QC\ATR's" -s -m*.xls* -d-30 -c"CMD /C del /f /q ^0x22#FILE^0x22"