Windows 7: Scheduler with xcopy don't copy new files - windows

I've created a batch file to copy files from my machine to a backup machine. The batch file is this:
#echo off
cd D:\Backup
echo %date% %time% - Starting backup process. >> D:\Backup\backup_log.txt
xcopy D:\T\Source I:\AREA\USER\Backup\T\Source /s/d/y/c/v/r/i/e >> D:\Backup\backup_log.txt
echo %date% %time% - Stopping backup process. >> D:\Backup\backup_log.txt
exit
When running this bat file as command line, it copies all the new files and folders from source folder to a destination folder. Makes an update of the Source folder in I:\AREA\USER\Backup\T\Source. This is what I want.
D:\Backup>Backup.bat
But when I configure the same bat execution using Windows Task Scheduler, the scheduler runs without any errors but doesn't copy/update the new files.
I'm using Windows 7. I've made some tests and with this scenario, I can't see where the problem is. Maybe some configuration on Task Scheduler. I've read the information of the xcopy parameters. But with these parameters work in command line, why it doesn't work using windows scheduler?
Any help?
Thanks!
Filipe
Edit1: I've found something that can help to parse the error. If, in the Task Scheduler, I select the option Run only when user is logged on, it works. But when I select the other option Run whether user is logged on or not, it doesn't work.

Well... I resolve my problem with this.
The bat file is:
#echo off
cd D:\Backup
echo %date% %time% - Starting backup process. >> D:\Backup\backup_log.txt
xcopy "D:\T\Source" "I:\AREA\USER\Backup\T\Source" /D /E /C /R /H /I /K /Y /V >> D:\Backup\backup_log.txt
echo %date% %time% - Stopping backup process. >> D:\Backup\backup_log.txt
exit
In the Task Scheduler, I've changed the General tab and check "Run only when user is logged on".
I don't know, but in my understanding, the "Run whether user is logged on or not" should work when the user is logged.

I had a similar issue running bat file with XCOPY via Task Scheduler with "Run whether user is logged on or not" set. The ussue was that I have a network location \\domain mapped as disc D: thus my cmd was like
XCOPY /d /y \\192.168.1.1\Data D:\Data
It was running fine just by executing bat file, but task runner didn't copy anything. I've changed it to
XCOPY /d /y \\192.168.1.1\Data \\domain\Data
And everything was fine since then.

Related

Program started from batch file starts in background

I have a batch file that I run from a flash drive. This file attempts to
1. Close 2 programs
2. Update data on the computer based on the data on the flash drive
3. Restart the programs
The code in this batch is:
taskkill /IM "MyProgram".exe
taskkill /f /fi "imagename eq MyProgram.exe"
copy e:\File1.xml C:\Folder\SubFolder1\Themes\Data\File1.xml /Y
copy e:\File2.xml C:\Folder\SubFolder1\Themes\Data\File2.xml /Y
copy e:\File3.xml C:\Folder\SubFolder2\Themes\Data\File3.xml /Y
copy e:\File4.xml C:\Folder\SubFolder2\Themes\Data\File4.xml /Y
start /MAX "" "C:\Folder\SubFolder2\MyProgram.exe"
start /MAX "" "C:\Folder\SubFolder1\MyProgram.exe"
The first two steps work fine, with the commands that are on lines 1-6. My issue starts with step 3 and the commands on lines 7 and 8.
Is there something wrong with the batch commands?
EDIT: After digging a little more, I have found that the programs appear to be starting in the background. When looking at task manager, the programs appear in the "Background Processes" section instead of showing in the foreground as expected.
What I believe is happening is your program does not know where the configuration files are located because the working directory is where the batch file started itself. So your program is looking for its files on your usb drive. By using the /D option with the START command it will switch the working directory to whatever path you set it to.
start "" /MAX /D "C:\Folder\SubFolder2\" MyProgram.exe

Batch file fails on startup, but works when I click it

I have a small problem with my batch script. The idea is simple:
I have an app that checks for updates periodically (checks for one exe file). If it finds one it then downloads it, renames it and puts it in the same directory as the old file. Then the app creates a .bat file and also puts it in the same directory. The contents of .bat file are :
#ECHO OFF
ECHO Waiting while old application closes...
ping 127.0.0.1 -n 5
taskkill /IM myApp.exe /f
ECHO Updating application
move /y myApp_TEMP.exe myApp.exe
START myApp.exe
pause
Before I close my application (myApp.exe) I instruct it to execute the .bat file, ant wait 5 sec. so it will properly close, that’s why the ping 127.0.0.1 -n 5 is here for. So ideally this script should:
Close myApp.exe
Rename myApp_TEMP.exe to myApp.exe
Overwrite old myApp.exe with new myApp.exe
Start myApp.exe
And it works when I double click on the .bat file, the problem occurs when I put myApp.exe in Windows startup list.
So the app start's, downloads update, generates the .bat file (keep in mind that everything is happening in the same directory) and then runs it. After some digging i found out that the line move /y myApp_TEMP.exe myApp.exe is not executing. But when I run this script manually everything works. Maybe someone has already experienced similar issues?
You are using relative paths in your filenames. Check that it's being run with the correct current directory or switch to absolute paths.
I would suggest using a CD /D <directory of the .exe file> before the move, that way you always know for sure you're in the right directory. Also, you can use timeout 5 /NOBREAK to wait for 5 seconds in regular batch-files
Might need to add some redundancy to the script. Such as:
#ECHO OFF
ECHO Waiting while old application closes...
taskkill /IM myApp.exe /f
timeout 5 /nobreak >nul
ECHO Updating application
IF EXIST "%cd%\MyApp_TEMP.exe" move /y myApp_TEMP.exe myApp.exe
IF EXIST "%cd%\MyApp.exe" GOTO STARTAPP
ECHO File not found. Unable to update.
pause
exit
:STARTAPP
START myApp.exe
pause
The problem seems to stem from a problem in directory searching. While this could be solved with a rather long search time, it'd be easier to make sure that the batch is in the same directory as "MyApp_TEMP.exe" or using
cd <path to MyApp_TEMP.exe>
ECHO Updating application
move /y myApp_TEMP.exe myApp.exe
START myApp.exe
pause
You could also use an ELSE if the file is not found, I just Personally prefer to use label jumping with GOTO.

copy command in batch file is not getting executed when calling the batch file from another batch file, but is getting executed when i double click

i am trying to execute a command xcopy path1 path2 /Y /C and it is getting executed successfully when i tried from command line and also when i copied it to a batch file and double clicking on that batch file.
But it is not getting executed when i cal this batch file from another fail.
Could anyone please help here?
Not working case:
C:\abcd>cmd.exe /C "xcopy "C:\folder1\itsme.bat" "Y:\" /C /Y /Z"
0 File(s) copied
Working case:
C:\abcd>cmd.exe /C "xcopy "C:\folder1\itsme.bat" "Y:\" /C /Y /Z"
C:\abcd\itsme.bat
1 File(s) copied
Extra Info:
Runme.bat:
call C:\folder1\copy.bat
call C:\folder1\clean.bat
copy.bat:
#echo off
xcopy "C:\folder1\runrun.bat" "Z:\" /C /Y /Z /Q
xcopy "C:\folder1\runrun.bat" "Y:\" /C /Y /Z /Q
xcopy "C:\folder1\runrun.bat" "X:\" /C /Y /Z /Q
Here, If I double click on Runme.bat, copy.bat is getting executed and copying all the files.
1 File(s) copied
1 File(s) copied
1 File(s) copied
But issue is, it is not copying anything when i try to run the same batch file from windows scheduler.
Output:
0 File(s) copied
0 File(s) copied
0 File(s) copied
looks like issue is only with copy command in the second batch file, which will return output. But all the commands in the other batch file clean.bat (which i am calling from the first batch file) are getting executed without any issues.
second batch file has simple echo commands, so that is why it is working fine.
Use xcopy with 'START' command.
For example, c:>> START xcopy "C:\folder1\itsme.bat" Y:\ /C /Y /Z
As Mofi pointed out, you can remove 'CMD'
The command cmd is for running a new instance of the command line interpreter and need to be used usually only for opening a command prompt window. Execute in a command prompt window cmd /? to get help about this command.
The command cmd does not need to be used usually if a command prompt window is opened already and a command is entered. A batch file is interpreted/processed by cmd and therefore it usually does not make sense to use cmd in a batch file.
So use only xcopy "C:\folder1\itsme.bat" "Y:\" /C /Y /Z in already opened command prompt window and in the batch file.
To process another batch file like batch file 2 from within a batch file like batch file 1 and continue processing of batch file 1 after processing of batch file 2 finished, use in batch file 1 the command call for calling batch file 2 like a subroutine.
Example for batch file 1:
#echo off
echo This is batch 1 calling now batch 2 and is waiting until it finished.
call "batch file 2.bat"
echo Batch 1 continues.
Example for batch file 2:
echo This is batch 2 running XCOPY.
xcopy "C:\folder1\itsme.bat" "Y:\" /C /Y /Z
echo XCOPY finished, batch 2 terminates.
Run batch file 1 and you get the output:
This is batch 1 calling now batch 2 and is waiting until it finished.
This is batch 2 running XCOPY.
XCOPY finished, batch 2 terminates.
Batch 1 continues.
Remove command call in batch file 1, execute it again and look what you get now.
This time without call in batch file 1 the processing of batch file 1 continues on batch file 2 without coming back to batch file 1 on reaching end of batch file 2 processing.
From your extra information it may be that x: y: z: are network drives and task scheduler doesn't have network privileges under the system account.
Also, change the name of copy.bat because copy is a system command and using system command names will bite you one day, if not today.

How to run a set of parallel batch files sequentially

I am trying to write a test bat file that will do the following:
Download a file from a server
Upload a file to the same server
I need three laptops to perform download and upload simultaneously. therefore, I create such a script named 3-chanD.bat:
*echo /////////3-channel-Downloading////////
start cmd.exe /k >time2 timeit chan6D.bat
start cmd.exe /k >time3 timeit chan11D.bat
start cmd.exe /k >time1 timeit chan1D.bat*
chan6D.bat is basely a script coping file from a server to one of my laptops:
*xcopy "C:\Documents and Settings\All Users\Documents\test" "\\192.168.0.102\SharedDocs\test" /Y /E /S*
same as chan1D.bat and chan11D.bat:
*echo /////////channel 6 Downloading////////
xcopy "C:\Documents and Settings\All Users\Documents\test" "\\192.168.0.102\SharedDocs\test" /Y /E /S*
*echo /////////channel 11 Downloading////////
xcopy "C:\Documents and Settings\All Users\Documents\test" "\\192.168.0.104\SharedDocs\test" /Y /E /S*
The upload script does the opposite job.
The problem is I need to sit beside laptops to wait those three laptops complete downloading, then manually run the upload script. I try to write a script:
*#echo off
echo /////////3-channel-Downloading////////
call 3-chanD.bat
echo /////////3-channel-Uploading////////
call 3-chanU.bat
pause*
but it just run the 3-chanD.bat and 3-chanU.bat in a second. It doesn't wait till download is finished.
Is there a way to execute a set of commands in parallel, then execute another set of commands in parallel?
Have each batch file create a flag file, perform the xcopy, and delete the flag file when the xcopy has finished.
type nul > "%temp%\xcopy1.tmp"
xcopy "C:\Documents and Settings\All Users\Documents\test" "\\192.168.0.102\SharedDocs\test" /Y /E /S
del "%temp%\xcopy1.tmp"
Your launching batch file can check for the existence of all three flag files in a loop, and launch the upload processes when they no longer exist.

Windows batch script to output log

I am writing a simple batch script to execute 2 programs. I would want to create a log file everytime this batch script is executed regardless it is success or failed.
#echo on
START "" /b /wait 7za.exe a C:\nexus.7z c:\nexus -pTESTING -mx7 >> c:\7zip.txt
START "" /b /wait xcopy.exe C:\nexus.7z \\ketsrv14\Software >> c:\copy.txt
The script above will create log for each program. Is there a way to combine the log and create only 1 log for both program?
This should work. I think 7-zip will wait on it's own.
I use the 7z.exe and don't have a 7za.exe in mine.
#echo on
7z.exe a C:\nexus.7z c:\nexus -pTESTING -mx7 >> c:\7zip.txt
xcopy.exe C:\nexus.7z \\ketsrv14\Software >> c:\7zip.txt

Resources