We have an integration engine, which creates txt files for the opposite host system. Our system writes files to local folder. I created a bat file like that and scheduled for every 1 minute:
xcopy /v /y E:\*.txt Z:\
move E:\*.txt E:\Processed (for backup purpose)
Z:\ is the mapping folder of the host system and that folder is being scanned frequently. If a file is processed it will be deleted immediately by the host system.
My problem is, sometimes files are written duplicated. I mean users see the activies as twice. I think that's because of this; consider a moment which the host system processes my file at the same time of xcopy executes and things get messed up. I know it is impossible to happen those at the same time but maybe network lags causing machines to behave like that?
Any ideas?
Thanks
What about using following batch file?
#echo off
set "SleepTimeInSeconds=60"
rem For sleep time using PING command 1 must be added
rem because the first trial is always successful.
set /A SleepTimeInSeconds+=1
:NextRun
echo %TIME% Searching for files to copy and move ...
for %%I in (E:\*.txt) do (
copy /B /V /Y /Z "%%I" Z:\
move /Y "%%I" E:\Processed\
)
%SystemRoot%\System32\ping.exe 127.0.0.1 -n %SleepTimeInSeconds% >nul
goto NextRun
It copies each file separately on the server and then moves the file to the backup directory.
See How to sleep for 5 seconds in Windows's Command Prompt for the sleep time solution using command PING. You could perhaps also use command TIMEOUT depending on version of Windows which would be even better.
This batch file must be started only once on startup of machine and should run forever until shutdown of machine. Don't run this batch file as scheduled task every minute.
The scheduled task starting the batch file every minute caused the problem with processing files twice because if copying and then moving all *.txt files does not finish within 1 minute caused for example by a temporary network problem, another batch process was started doing the same as the still running batch process.
Related
For my work I install software and configure it using a batch file; setting folder permissions, copying config files from a server share to make it work etc. All this is fine and I have been using stuff I have got from here for some time with no problem. I have to do it on multiple computers and on occasions at places where the link is so slow it takes ages to run everything from the server share.
I made a batch file that lets me choose between network install process or from USB to really speed things up. However I'm now faced with waiting for everything to finish installing from the USB before I can move on to the next PC. Many of the PC's are very slow so I can be waiting best part of 10 mins. I'd like to be able to run a batch file from the USB which then copies the software installer and related files to C:\Temp (or such) then launch another specific batch file from C:\Temp (and runs from that dir). The end result being I can plugin the USB, run the initial copy to C:\Temp bat file, the called batch file from C:\Temp then does the install instead of USB allowing me to remove the USB and get on with the next PC without waiting for the process to finish from USB.
I have had some success on my PC with windows 10 ( I assume will work fine for 7, 8 etc.) However with XP (which in my line of work still crops up more than you'd think) when I remove the USB stick, the process is trashed because for some reason, even though I call the C:\Temp bat installer in a new window, that new window still holds on to the fact it's coming from USB, there fore it wont complete unless I leave the USB in. Which is not what I want of course. Maybe there is a better way I'm sure, I just need a solution that works on all XP, 7 , 8 etc. You can see, I just copy a few things to C:\Temp then choose which bat installer I need at the time, then (try) and fire up the appropriate bat running from C:\Temp instead of USB letting me move on with out having to wait. Hope some one can advise. Many thanks!!
Here is the bat that copies from USB to C:\Temp
if not exist C:\Temp md C:\Temp
echo F| XCOPY %~dp0silentinstall.exe /y C:\Temp
xcopy /herky %~dp0GUIDES C:\Temp\Guides /i
echo 1 -- Standard
echo 2 -- DelR2
echo 3 -- FullWipeInstall
echo;
set /P rmFunc="Enter a choice: "
echo --------------------------------------------------------------------
for %%I in (1 2 3 4 5 x) do if #%rmFunc%==#%%I goto run%%I
goto begin
:run1
echo F| XCOPY %~dp0Standard /y C:\Temp
cd C:\Temp
call cmd /c Standard.bat
exit
:run2
echo F| XCOPY %~dp0DelR2.bat /y C:\Temp
cd C:\Temp
call cmd /c "DelR2.bat"
exit
:run3
echo F| XCOPY %~dp0"FullWipeInstall.bat" /y C:\Temp
cd C:\Temp
call cmd /c "FullWipeInstall.bat"
exit
Your bat file resides on the USB. So it can't load the exit statement if USB is removed. I suspect that you are just lucky that it works on some OS's because there is nothing to do other than exit anyway. Without testing there are probably several ways to fix:
1. Replace these 2 lines with a suitable START command and then exit. See START /?
cd C:\Temp
call cmd /c "FullWipeInstall.bat"
Put the exit on the call and the exit on the same line like this
call cmd /c "FullWipeInstall.bat" & exit
I have an archive.pst file on my C: drive that I use in outlook to backup my email. But my C: is not backed up each night. So I'd like to copy the .pst file to my network drive so it will consistently be backed up. I do not want outlook to open the .pst file directly from the network drive for a variety of reasons.
Therefore I am trying to create a scheduled task that will copy my .pst file to a network location each day. The batch file below works perfectly if double-clicked. If I try to run the scheduled task, only the log file is created. Outlook doesn't close and the .pst file is not copied. I've tried running with the highest privileges but that doesn't seem to help. Any ideas would be appreciated.
cscript.exe close_outlook.vbs
::This is my VBS Script
::Set Outlook = CreateObject("Outlook.Application")
::Outlook.Quit
ping localhost > nul
set idrive="\\myserver\drive\\Outlook Files\"
set current="C:\myfolder\myuser\Documents\Outlook Files"
echo Start Time of Copy: %time% >> %idrive%\Log.txt
copy %current%\archive.pst %idrive%\archive.pst /y
echo End Time of Copy: %time% >> %idrive%\Log.txt
move %idrive%\Log.txt %idrive%\BackupLogs\Log.txt
ren %idrive%\BackupLogs\Log.txt %date:~10,4%-%date:~4,2%-%date:~7,2%_log.txt
cscript.exe open_outlook.vbs
::This is my VBS Script
::set shell = createobject("wscript.shell")
::shell.run "outlook.exe"
EXIT
In reviewing the previous responses, I have shortened the batch file to only the code below. This works when double-clicking, but not when scheduling a task. I've also tried the same task moving the .vbs script to a network drive. Same outcome.
%SystemRoot%\System32\cscript.exe "C:\OutlookBackup\close_outlook.vbs"
%SystemRoot%\System32\ping.exe -n 4 127.0.0.1>nul
%SystemRoot%\System32\cscript.exe "C:\OutlookBackup\open_outlook.vbs"
1. Specify all files in a batch file executed as scheduled task with full path.
Double clicking on a batch file results usually in running the batch file with currently working directory being the directory of the batch file. But when running a batch file as scheduled task, the system32 directory of Windows is the current working directory.
Is close_outlook.vbs and open_outlook.vbs in system32 directory of Windows?
I don't think so. Replace in batch code below Path to\Script File twice by the right path.
2. Assign string values with space to environment variables right.
variable=value is the parameter for command set. With
set idrive="\\myserver\drive\\Outlook Files\"
you assign to variable idrive the value "\\myserver\drive\\Outlook Files\" with the double quotes included. This results on expansion of
echo End Time of Copy: %time% >> %idrive%\Log.txt
in the command line
echo End Time of Copy: 19:21:53 >> 1>"\\myserver\drive\\Outlook Files\"\Log.txt
and this is not right, isn't it.
Correct is:
set "idrive=\\myserver\drive\Outlook Files"
I removed also second backslash after drive and the backslash at end of folder path.
As the environment variable contains now the path with space(s) without double quotes, the double quotes must be added where the value of the environment variable is used concatenated with a file name, see batch code below.
There is one more reason why using "variable=value". A not visible trailing space at end of the line with command set in batch file is also appended to value of the environment variable if double quotes are not used or used wrong. Read this answer for details about correct assignment of string values to environment variables.
3. Define the wait loop using command ping better.
The command
ping localhost > nul
produces a wait. But it is better to use something like
%SystemRoot%\System32\ping.exe -n 4 127.0.0.1>nul
as now the wait is determined with exactly 3 seconds.
4. Do not insert a space left of redirect operators > or >> for stdout.
Why this should not be done was explained by me here in detail.
5. Avoid environment variables not defined in batch file itself or in system account.
Your batch file uses only environment variables defined in batch file itself. So this advice is here not really needed.
However, many batch file working fine on double click but not on running as scheduled task fail because the batch file depends on environment variables like PATH or others which are related to current user account. It is safe to use environment variables which exist for all accounts like SystemRoot.
Reworked batch code
Here is your batch file with the appropriate changes whereby the paths of the two *.vbs files must be set correct by you before the batch file (hopefully) works as scheduled task.
%SystemRoot%\System32\cscript.exe "Path to\Script File\close_outlook.vbs"
%SystemRoot%\System32\ping.exe -n 4 127.0.0.1>nul
set "idrive=\\myserver\drive\Outlook Files"
set "current=C:\myfolder\myuser\Documents\Outlook Files"
echo Start Time of Copy: %time%>>"%idrive%\Log.txt"
copy /B /Y /Z "%current%\archive.pst" "%idrive%\archive.pst"
echo End Time of Copy: %time%>>"%idrive%\Log.txt"
move "%idrive%\Log.txt" "%idrive%\BackupLogs\Log.txt"
ren "%idrive%\BackupLogs\Log.txt" %date:~10,4%-%date:~4,2%-%date:~7,2%_log.txt
%SystemRoot%\System32\cscript.exe "Path to\Script File\open_outlook.vbs"
set "idrive="
set "current="
I have a problem I cannot understand so I hope im putting it on the right section of StackNetwork.
I have a batch file.
It dumps data, zips a file and then removes the source folder that was just zipped.
Here are the last steps
CD "C:\Backup\%DATE%\"
REM ----- NOW ZIP THE FILE AND DELETE THE SOURCE -----
"C:\Program Files\WinRAR\WinRAR.exe" a -r -afzip "C:\Backup\%DATE%.zip"
CD "C:\BackUp\"
RMDIR /s /q "C:\BackUp\%DATE%\"
This line of code, deletes the folder.
RMDIR /s /q "C:\BackUp\%DATE%\"
The problem is:
When I manually execute the Scheduled task, by right clicking the task and selecting RUN, the batch file runs excellent and deletes the folder in the end.
But when the task runs automatically at night, all goes well, except, the folder is not deleted.
I assume it has to do with priviliges as it does work when I manually RUN the task, but whats funny is that the folder %DATE% is being created whitin the batch file.
The task is set to run with highest priviliges, even if the the user is logged in.
Before I write about my ideas for possible reasons on directory deletion failure, I suggest to use in batch file:
RMDIR /s /q "C:\BackUp\%TODAY%\" 2>"C:\BackUp\RemoveDirError.log"
Look on "C:\BackUp\RemoveDirError.log" after next scheduled task run which should contain the error message being hopefully helpful on finding the reason for failed deletion of the directory.
1. DATE different between beginning and end of batch execution
The value of environment variable DATE is always the current date.
The value of DATE at beginning of batch file would be different to value on end of the batch file if this batch file is executed over midnight.
Example:
The batch file is started on 23:30:00 every day and needs 50 minutes to complete.
The batch code on execution would be in this case for example:
CD "C:\Backup\2014-09-08\"
REM ----- NOW ZIP THE FILE AND DELETE THE SOURCE -----
"C:\Program Files\WinRAR\WinRAR.exe" a -r -afzip "C:\Backup\2014-09-08.zip"
CD "C:\Backup\"
RMDIR /s /q "C:\Backup\2014-08-10\"
Note: Format of date string depends on Windows language settings for date.
The result is the error message:
The system cannot find the file specified.
The solution for this problem is to assign value of DATE to a new variable at beginning of the batch file and reference this variable instead of DATE in entire batch file.
set TODAY=%DATE%
CD "C:\Backup\%TODAY%\"
REM ----- NOW ZIP THE FILE AND DELETE THE SOURCE -----
"C:\Program Files\WinRAR\WinRAR.exe" a -r -afzip "C:\Backup\%TODAY%.zip"
CD "C:\BackUp\"
RMDIR /s /q "C:\BackUp\%TODAY%\"
The environment variable TODAY contains here the date string definitely not changed during the execution of the batch file.
2. WinRAR is started as separate process
Another problem could be that WinRAR is a Windows application and not a console application and could be therefore executed in a separate process executed parallel to the batch file with C:\BACKUP\%DATE%\ as current working directory.
The batch job would continue execution immediately after string WinRAR in this case and this is the reason deleting the directory is prevented by Windows.
The solution is to use command start with option /wait to force running WinRAR as separate process but halt execution of batch job until WinRAR has terminated itself after finishing compression.
set TODAY=%DATE%
CD "C:\Backup\%TODAY%\"
REM ----- NOW ZIP THE FILE AND DELETE THE SOURCE -----
start "Backup Compression" /wait /min "C:\Program Files\WinRAR\WinRAR.exe" a -r -afzip "C:\Backup\%TODAY%.zip"
CD "C:\BackUp\"
RMDIR /s /q "C:\BackUp\%TODAY%\"
But this is most likely not the reason as in this case the deletion of the folder would fail also on running the scheduled task manually.
3. WinRAR outputs a question and no one answers it
It is possible that WinRAR detects a problem during compression and opens a message prompt with a question to answer by the user. As on scheduled task execution nobody answers it, WinRAR holds compression for an unusual long time.
The solution would be adding -y to WinRAR command line in batch file.
start "Backup Compression" /wait /min "C:\Program Files\WinRAR\WinRAR.exe" a -r -afzip -y "C:\Backup\%TODAY%.zip"
Again I think, this is not very likely for not deleting the directory.
I have no more ideas. We would need to see complete batch file code if nothing above helps to find the reason for error on deletion of the directory on running the batch file as automatically executed scheduled task.
So I have a bat file that runs just fine when I double click it. But setting it up to run with Task Scheduler and it will not run fully.
In this Bat file I use xcopy to copy two things:
Regular files.
Files modified in the last 30 days.
The "Regular files" part of this bat file runs fine with the task scheduler. However the "Files modified in the last 30 days" does not. (Again, running this bat file manually works.)
Contents of bat file:
set /p mydt=<tmpFile
set YYYY=%mydt:~6,4%
set MM=%mydt:~0,2%
set DD=%mydt:~3,2%
#echo %YYYY%
#echo %MM%
#echo %DD%
xcopy "\\TheServer\c$\TheFiles\*" C:\Dir\Files_younger_than\ /D:%MM%-%DD%-%YYYY% /Y /S
Xcopy "\\TheServer\c$\MoreFiles\*" C:\Dir\Morefiles\ /Y /S
Any thoughts why task scheduler is giving me issues here?
The answer is on the Actions Tab. Under Edit Action, put the bat file name in the program/script box. Then put the directory where the Bat file was located in the "Start in (optional)" area.
This should do the trick and the file should run properly after that.
Task scheduler runs under the System account by default and doesn't have access to network resources. Change it to run under your credentials and it will have the same permissions as you.
I have a batch script that launches a tomcat server. The script starts the service, which unpacks the files and folders in the "webapps" directory. I want to copy image files (that are added by the customer) into the webapps directory so they can be used by the server. However i need to wait until the folder has been unpacked so that the directory exists before i can add the images to it, what command or commands could i used to make this happen (apart from just waiting an arbitrary long enough time)
I am no expert at this stuff but this works for me
#echo OFF
:START
if not exist c:\abc GOTO WAIT
GOTO COPY
:WAIT
:: pause for 1 second
ping 127.0.0.1 > nul
GOTO START
:COPY
ECHO "DO COPYING STUFF"
The ping is to put a 1 second pause between each check if the folder or file exists.