P
P
Pavel2017-09-22 15:00:08
cmd/bat
Pavel, 2017-09-22 15:00:08

How to prevent reselection of bat files?

I don't know how to formulate the question for the title, I'll describe the whole problem:
I have a video "log", .wmv files are added to a certain folder every minute. They need to be sent to FTP, I wrote the following code

@echo off
ren "C:\Users\NAME\Documents\log\Videos\*.wmv" *.zip
"C:\folder2\WinSCP\WinSCP.com" ^
  /log="C:\folder2\WinSCP\WinSCP.log" /ini=nul ^
  /command ^
    "open ftp://логин:пароль@сервер/" ^
    "synchronize remote -criteria=time ..\send\ /log" ^
  "put ..\send\*.zip /logServer/" ^
    "exit"
set WINSCP_RESULT=%ERRORLEVEL%
if %WINSCP_RESULT% equ 0 (
  echo Success
  del /q C:\Users\NAME\Documents\log\Videos\*.zip
) else (
  echo Error
)
exit /b %WINSCP_RESULT%

the script takes all the files from the Videos folder, changes the extension to zip
, starts WinSCP ftp in console mode, sends all the .zip files to the server (renamed to zip for this :) ). After a successful transfer, all zip files from the folder will be deleted, and the newly added .wmv files will remain and wait for the next script run.
The problem is that 1 file is sent for more than a minute and when the scheduled script starts again, it again starts sending a .zip file that has not yet been deleted after running the script for the first time. If the sending speed is very low, then we will get an endless sending of the same file. Such sadness.
Who will tell you how to get around this embarrassment?

Answer the question

In order to leave comments, you need to log in

3 answer(s)
P
Pavel, 2017-09-22
@mrusklon

Thank you all, I'll post the finished code, maybe it will be useful to someone

@echo off
Set Process=winscp
tasklist | Find /i "%Process%.exe" || (goto Else)
echo net
Goto end
:ELSE
ren "C:\Users\name\Documents\log\Videos\*.wmv" *.zip

"C:\Users\name\Documents\log\WinSCP\WinSCP.com" ^
  /log="C:\Users\name\Documents\log\WinSCP\WinSCP.log" /ini=nul ^
  /command ^
    "open ftp://логин:пароль@сервер/" ^
  "put ../Videos\*.zip /logServer/" ^
    "exit"

set WINSCP_RESULT=%ERRORLEVEL%
if %WINSCP_RESULT% equ 0 (
  echo Success
  del /q C:\Users\name\Documents\log\Videos\*.zip
) else (
  echo Error
)
exit /b %WINSCP_RESULT%

R
res2001, 2017-09-22
@res2001

In the batch file, create a temporary flag file, something like this:
echo.> file.flag
At the very beginning of the batch file, check whether this file exists, if the file exists, then complete the batch file (the previous run has not yet worked), if the file does not exist, we work further ( and create this file). At the end of the work - delete the flag file.
Other options for detecting running copies of the same batch file:
1.similar to the flag file - a flag in the registry
2.using tasklist, you can check whether a process is running with the same batch file.
Side effects: if the batch file for some reason does not delete the flag file, then the next process will never start.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question