I have a script for a customers of us, which transfers one or more files to a remote ftp server.
#echo off
#echo %DATE% %TIME% Starte FTP-Transfer. Bitte warten. >>C:\Users\xx\Desktop\log.txt
ftp -s:X:\xxx\FTP-Transfer\Transfer.txt >>C:\Users\xx\Desktop\log.txt
#echo %DATE% %TIME% FTP-Transfer beendet >>C:\Users\xx\Desktop\log.txt
xcopy X:\xxx\FTP-Transfer\* X:\xxx\FTP-Transfered\* <<-- copies the transfered files away
echo --- Complete ---
pause REM delete this in live version!!
However i need an error handling of some sort. What i need is, a way to make sure the transfer has been completed before i copy the files away. The script in Transfer.txt is designed to copy all files in a folder to the remote server and it runs several times a day. Simpliest would be to just let the files in the transfer folder till the transfer has been completed.
Thanks for all suggestions. I'm open to use another ftp program, as long as i can access it in a batch file.
It looks like it should be as simple as checking to see if the file exists in a different directory, correct?
if exist File.txt (
ECHO File exists
)
Then in the code block, set a variable that will describe if the file exists or not, then insert another If block for copying the files away
Related
If I have call C:\Users\user\Desktop\Main\folder\edit.bat
What is the right way to set this up this:
I want to eliminate C:\Users\user\Desktop, and make my batch universal so that I can plug in to any computer, run it from a usb, or external or simply add my batch to the desktop of anyone's computer. And run it without having to edit source or target again.
If I want it to run my edit.bat from any computer do I set it up like this
call %SystemRoot%\Main\folder\edit.bat
or
call %UserProfile%\Desktop\Main\folder\edit.bat
or
call %UserProfile%\Main\folder\edit.bat
If I understand Mofi right I can change this
call "C:\Users\user\Desktop\Main\folder\edit.bat" Original
to this
call "C:\users\%username%\Desktop\Main\folder\edit.bat" Works
and to this
call "%UserProfile%\Desktop\Main\folder\edit.bat" Works
Can anyone tell me if this is the correct
call "%~dp0\edit.bat"
If I am understanding your question correctly you want to have the .bat on the USB, plug it in, run it, and copy the files from a directory to the USB. In that case the simple script below will work. Assuming the path you choose on the client is static and does not vary, e.g. %userprofile%\desktop\ or %userprofile%\documents\.
#echo off
REM This .bat file will be on the USB drive.
set "usb=%~dp0"
set "new_path=%usb%%computername%\%username%"
if not exist "%new_path%" mkdir "%new_path%"
xcopy "%userprofile%\main\folder\files\*" "%new_path%\" /Y
if errorlevel 1 (
echo ERROR OCCURRED - %ERRORLEVEL%
pause
exit /b
)
echo Successfully copied files to "%new_path%"
timeout /t 3
exit /b
EXPLANATION
First we make a directory on the flash drive, if it doesn't already exist, so all the files are neat and organized on the USB.
if not exist checks for the directory. mkdir creates the destination directory. Pretty obvious but none-the-less.
%~dp0 defines the working directory where the .bat file is located. For more information Here is a great post.
%userprofile% environment variable is by default equal to the directory C:\users\%username%\, and %computername% expands to the computer's name.
xcopy takes the source directory and copies to our destination we created prior. /Y forces the copy and does not prompt to overwrite files.
* is a wildcard. Here is a good site for that and also everything in this script. Note that a wildcard can only be used at the end of a directory. Something like C:\users\*\desktop\*\files\* will not resolve. For something like that you would need to use for /D.
Lastly I always like to check for errors, and see if it was successful. If not we pause to make sure we see the error, or we put in a timeout /t seconds to see a success.
EDIT
Set variables for paths to account for spaces in user names, and also fixed a couple other errors in the original script that were brought to my attention.
If you want to open a file in your portable drive you can do something like this
%~d0\folder\folder\file
I want to copy a Microsoft Excel file from source folder to destination folder with the condition to copy the file only if it does not exist in destination folder. I want this command being executed in a loop which is run endless with a wait time of 3 seconds between each execution.
Below is my batch file:
:start
If Not Exist D:\Users\00002829\Downloads\QAS\QAS.xls xcopy "D:\Users\00002829\Downloads\Origin" "D:\Users\00002829\Downloads\QAS"
(
timeout /t 3
)
goto start
But this batch file is not working. It copies always the file even if the file exists in destination folder.
As everyone suggested, you are not specifying the file to be copied.
Also, for a single file I rather prefer to use copy; keep it simple. However, do notice that, if that file (qas.xls) is being used, you may want to switch to robocopy and take advantages of his almost infinite options.
The suggested code:
:start
if not exist "d:\users\00002829\downloads\qas\qas.xls" (
xcopy "d:\users\00002829\downloads\origin\qas.xls" "d:\users\00002829\downloads\qas"
)
timeout /t 3
goto start
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.
I wrote a batch file that is supposed to iterate through all .csv files in a folder and do the following with each one: MOVE/Rename it to solar.csv in a different folder while overwriting any file with the same name, start some service that utilizes solar.csv, wait 7.5 minutes, stop the service. This should be done for each .csv file in the folder. I would like it to report each step as it does it as well. Here is my code so far:
#ECHO OFF
CD "D:\PI Data Import\Data"
For /R %%G IN (*.csv) DO (
MOVE /Y %%G.csv "D:\PI Data Import\solar.csv"
ECHO "%%G.csv Moved"
NET START RDBMSPI4
ECHO "Service Started"
ECHO "Waiting"
TIMEOUT /T 450 /NOBREAK
NET STOP RDBMSPI4
Echo "%%G.csv Complete"
)
It reports that my command syntax is not correct, but doesn't say which part exactly. I assume the FOR /R line is a bit wrong, maybe the MOVE command, and possibly how I handle the %%G in the command section.
All I really need is a syntax check, and I am afraid to test this too terribly much before it is golden since the server it will operate on is kind of important. All help appreciated, I'm not at all experienced with working on Batch files.
I'd remove the /R from the for command. Also, don't put .csv after %%g anywhere. They will already have .csv appended as they are .csv files.
You're going to overwrite the solar.csv file, every time you move a CSV file BTW. If you wish the CSV files to be appended to solar.csv, then you may have to type them out to that file. E.g.
Before you loop...
if exist solar.csv del solar.csv
And in your loop...
type %%g >> solar.csv
Unless your service is going to process each solar.csv file and then you just overwrite the file with the next one, in which case you're golden :)
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="