I am using .bat script to access sftp and download file. The ftp folder consist of a lot of file which can be uniquely identify by the file name in format YYYYMMDD. eg: 20180307.csv, 20180306.csv etc. Currently I am using mget to get all the file, then at later filter it locally. How can I modify the script to get the file on today only?
This is my .bat script.
D:
CD\myTargetFolder
psftp user#sftp.myftpserver.com -pw password -b mySftpScript.ftp
exit
This is my ftp script.
cd /myFTPSourceFolder
mget *.csv
bye
Thank you.
If you don't mind dynamically creating the ftp script then perhaps your .bat script could look like this:
#Echo Off
CD /D D:\myTargetFolder 2>Nul || Exit /B
Set "csv="
For /F "Tokens=1-3 Delims=/ " %%A In ('RoboCopy /NJH /L "\|" Null'
) Do If Not Defined csv Set "csv=%%A%%B%%C.csv"
( Echo cd /myFTPSourceFolder
Echo mget %csv%
Echo bye)>"mySftpScript.ftp"
psftp user#sftp.myftpserver.com -pw password -b mySftpScript.ftp
::Del "mySftpScript.ftp"
I've commented out the last line, remove the :: from it if you wish to delete the dynamically created ftp script.
Edit
To get yesterday's date csv then something like this, leveraging PowerShell, would be my recommendation:
#Echo Off
CD /D D:\myTargetFolder 2>Nul || Exit /B
Set "csv="
For /F UseBackQ %%A In (
`PowerShell "(Get-Date).AddDays(-1).ToString('yyyMMdd')"`
) Do Set "csv=%%A.csv"
( Echo cd /myFTPSourceFolder
Echo mget %csv%
Echo bye)>"mySftpScript.ftp"
psftp user#sftp.myftpserver.com -pw password -b mySftpScript.ftp
::Del "mySftpScript.ftp"
Related
I would like to ask if anyone has an idea how to send search result to console in single command line but with some specific code before and after the search results output
I need to run program Xconverter that is made to be operated by console, and has parameters:
-s :silent mode (only mode suported yet)
-i [files] : list location of the files in quotes: {-i "a.txt" "b.txt"..}
-t [tool file] : tool file location
-m 5 : conversion style
syntax accepted for example (single command for XConverter):
"C:\XConverter.exe" -s -i "C:\a.pgm" "C:\abc\b.pgm" -t "tool.tlgx" -m 5
but I need about 500 -i [files] to list
so i created search that fill look for those .pgm in tree mode where .bat is located and adds search result (file locations) one after other
my code is working but the only way i made it to work is that i outputed echo result in to extra .bat file and started the file. Is there i way to reprogram my code that it will send these outpust in to single one line command?
echo off
SET mypath=%~dp0
::THIS STARTS PROGRAM
(
echo | set /p="""
echo | set /p="C:\Program Files (x86)\Scm Group\Maestro\XConverter.exe" -s -i "
)>seznam_konvertovanych_suboru.bat
::THIS SEARCHERS FOR PGM FILES AND ADDS PATH OF EACH FILE TO THE LINE
for /f "delims=" %%A in ('forfiles /s /m *.pgm /c "cmd /c echo #relpath"') do (
set "file=%%~A"
setlocal enableDelayedExpansion
echo | set /p="""
echo | set /p="%mypath:~0,-1%\!file:~2!"
echo | set /p="" "
)>>seznam_konvertovanych_suboru.bat
::THIS ADDS SOME EXTRA FILE PATH TO THE LINE OF TOOL FILE, THAT IS NEEDED
(
echo | set /p=-t "Tlgx\def.tlgx" -m 5
)>>seznam_konvertovanych_suboru.bat
seznam_konvertovanych_suboru.bat
This code will output file seznam_konvertovanych_suboru.bat exactly in this format
"C:\XConverter.exe" -s -i "C:\a.pgm" "C:\abc\b.pgm" -t "tool.tlgx" -m 5
Anyone knows how to reprogram this to send that output to console in single line without creating extra file to store that code?
Edited:
What i need is having START_CONVERTING.BAT file that will send to console one command :
"C:\XConverter.exe" -s -i "C:\a.pgm" "C:\abc\b.pgm" -t "tool.tlgx" -m 5
but "C:\a.pgm" "C:\abc\b.pgm" are the search results of all .pgm files in that folder and child folders. So i will copy this START_CONVERTING.BAT anywehere where i store .pgm files. (some of them are in folders and those folders in other folders) so i will just copy STAR_CONVERTING.BAT in major folder that will thake care of all files with all files in child folders.
I will just click START_CONVERTING.BAT and it will convert those .pgm files. (without creating creating any other output to any other .bat file.
SOLVED (code)
#echo off & setlocal enableDelayedExpansion
PushD "%~dp0"
Set "XC=C:\Program Files (x86)\Scm Group\Maestro\XConverter.exe"
Set "TOOLFILE=Tlgx\def.tlgx"
Set "Files="
:: Concatenate all files in one string
for /f "delims=" %%A in ('Dir /B /S *.pgm') do set Files=!Files! "%%~A"
:: command, command line length may be an issue
"%XC%" -s -i%Files% -t "%TOOLFILE%" -m 5
If I understand right this might work
#echo off & setlocal enableDelayedExpansion
PushD "%~dp0"
Set "XC=C:\Program Files (x86)\Scm Group\Maestro\XConverter.exe"
Set "Files="
:: Concatenate all files in one string
for /f "delims=" %%A in ('Dir /B /S *.pgm') do set Files=!Files! "%%~A"
:: store command in batch file, command line length may be an issue
>"seznam_konvertovanych_suboru.bat" Echo "%XC%" -s -i %Files% -t "tool.tlgx" -m 5
try with that batch
Xconverter is for xxl to PGMX
1 set all path for converter
2 creat to do list of xxl files on desktop on this exemple or desktop sub folder!
3 Masetro can't be install as Office PC you must run it on CN PC!
#echo OFF
COLOR F1
TITLE "Convertor XXL to PGMX"
echo ÉÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍ»
echo º Converteur Xconverter º
echo ÈÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍͼ
echo.
echo "Stop command with Ctrl+C"
echo.
Echo "Path SET"
TIMEOUT /t 5 >nul
Set todo="%userprofile%\desktop\todo.txt"
Set def="%PROGRAMFILES(x86)%\Scm Group\Maestro\Tlgx\def.tlgx"
Set Compil="%PROGRAMFILES(x86)%\Scm Group\Maestro\Xconverter.exe"
Set desktop="%userprofile%\desktop"
echo "To do list of *.XXL files"
TIMEOUT /T 10 >nul
cd %userprofile%\desktop
dir /a /b /o /s *.xxl > %todo%
echo "Files convert command"
TIMEOUT /T 10 >nul
for /f "delims=" %%a in (%todo%) do %Compil% ^ -s -m 4 ^
-i "%%a" ^
-t %def%
echo "Rapport files"
Timeout /T 10 >nul
cd %desktop%
dir /a /b /o /s *.pgmx > Rapport.txt
Echo "Delete to do list"
TIMEOUT /T 10 >nul
DEL %todo% /F /S /Q >nul
But haven't go detail of Convesion style -m X
if you can replie me detail of -m number it was great!
All the best, and hope this is helpful
D-Alexandre V.
I have problems to upload files to an ftp using command mput*. When I run the batch file it shows no file found. The filename have spaces.
#echo off
for /F "skip=1 delims=" %%F in ('
wmic PATH Win32_LocalTime GET Day^,Month^,Year /FORMAT:TABLE
') do (
for /F "tokens=1-3" %%L in ("%%F") do (
set CurrDay=0%%L
set CurrMonth=0%%M
set CurrYear=%%N
)
)
set CurrDay=%CurrDay:~-2%
set CurrMonth=%CurrMonth:~-2%
echo Current day : %CurrDay%
echo Current month: %CurrMonth%
echo Current year :%CurrYear%
set LOG=%CurrYear%%CurrMonth%.log
set FTPFolder=D:\FTP
echo %FTPFolder%
cd /D %FTPFolder%
ECHO.
#ECHO OFF
ftp -s:TryFTP.txt
ECHO.
ECHO ***** FTP transfer complete successfully! *****
pause
The TryFTP.txt
open Servername
user
password
lcd D:\FTP
cd /HTML
binary
mput *
bye
Error I get:
binary
invalid command
mput*
file not found
bye
invalid command
but if I try it manually using the same command it works. Can you guys please advise what wrong with it?
i am looking into making a simple bath file that will loop through few simple commands about 1200 times (length of text file). My caveat comes in where those commands need a variable read in from a text file
Lets see if this pseudo code makes sense to anyone
start loop (for # of lines in txt file)
net use \\"IP from txt file" /user:Username *
robocopy \\server\share\path \\"IP from txt file"\c$\Folder
robocopy \\server\share\path \\"IP from txt file"\c$\AnotherFolder
net use \\"IP from txt file" /delete
loop
because of the environment and transferring of files from a domain server to a workgroup pc, this is about the only way i have found i can make this work. For reasons entirely too long to type here i am unable to use psexec and its list function for this unfortunately.
Any help you might be able to provide would be greatly appreciated
Check this batch file and if the output is correct remove each echo before your commands :
#echo off
set "File=List.txt"
for /f "delims=" %%A in ('Type "%File%"') do (
echo net use \\%%A /user:Username *
echo robocopy \\server\share\path \\%%A\c$\Folder
echo robocopy \\server\share\path \\%%A\c$\AnotherFolder
echo net use \\%%A /delete
)
pause
EDIT : To save the result into a LogFile
#echo off
set "File=List.txt"
set "LogFile=LogFile.txt"
If Exist "%LogFile%" Del "%LogFile%"
for /f "delims=" %%A in ('Type "%File%"') do (
echo net use \\%%A /user:Username *
echo robocopy \\server\share\path \\%%A\c$\Folder
echo robocopy \\server\share\path \\%%A\c$\AnotherFolder
echo net use \\%%A /delete
)>>"%LogFile%"
Start "" "%LogFile%" 2>&1
I would like to add a single password or password protect all the pdf files in a particular directory for the files starting with particular word "Retail.. " using batch script.
Updated my code but this does not apply password to my pdf file.
#echo on
SETLOCAL ENABLEDELAYEDEXPANSION
set filepattern="CE*.pdf"
REM cd /d C:\reports
cd C:\Desktop
for %%F in (%filepattern%) do (
set pass=test
::echo pdftk "%%~nxF" cat output "PDF\R_%%~nxF" owner_pw !pass!
pdftk CE.pdf output CE.pdf user_pw !pass!
)
Thanks in Advance !!
#echo on
set filepattern="Retail*.pdf"
set pass=test
cd C:\Desktop
for %%F in (%filepattern%) do (
pdftk %%~nxF output PDF\%%~nxF owner_pw %pass%
)
This should encrypt:
RetailQuotes.pdf to PDF\RetailQuotes.pdf
I am trying to run several hundreds queries that are located in many folders. All the folders are located under one "Master" directory.
I have the following batch code that allows me to run all the queries in one folder:
for %%G in (*.sql) do psql -U postgres -f "%%G" Satellites_Updated
pause
I am unsure how to make this batch file recursive so I can also check sub folders.
I have the following code, which does not work (only prints the current directory in the cmd window, then exits).
#echo off
call :treeProcess
goto :eof
:treeProcess
for %%G in (*) do (
if exist %1\* (
cd %1
call :treeProcess
cd ..
)
else (
psql -U postgres -f "%%G" Satellites_Updated
)
)
exit \b
Thanks in advance!
Here is the solution:
for /r %%G in (*.sql) do psql -U postgres -f "%%G" Satellites_Updated
pause
When it is so simple sometimes ...