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.
Related
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 found a good post here that queries for the latest sql server backup, and then copies it over to the remote server in preparation for a restore. It's a batch file that is executed using cmd via sql agent step inside the sql restore job. I'm looking for help in adding extra logic to the existing script below:
:Variables
SET DatabaseBackupPath=\\virtualserver1\Database Backups
echo.
echo Restore WebServer Database
FOR /F "delims=|" %%I IN ('DIR "%DatabaseBackupPath%\WebServer\*.bak" /B /O:D') DO SET NewestFile=%%I
copy "%DatabaseBackupPath%\WebServer\%NewestFile%" "D:\"
What I'd like to add are two extra pieces. First add some error handling to the existing script where it would first check the latest backup, but ensure its within the last 24 hours. If it is continue to run. If older than 24 hours, to generate an notification alert (i.e. separate batch file) if backup file is older than 24 hours. Second to generate similar notification, if there was an issue such as not being able to reach the remote share that holds the backup.
Appreciate replies.
It is not easy to check if a file was created or last modified within 24 hours. Date/time calculation is not a trivial task without conversion of the date/time strings to seconds since epoch which would make it possible to use a simple integer comparison.
There are solutions for file date comparison, see
Batch script to check when the newest file in a directory was created/modified
forfiles - Delete all files older than 1 day / 24hours using creation date?
You may use one of those solutions.
Much easier is to check if copy process worked:
copy /V "%DatabaseBackupPath%\WebServer\%NewestFile%" "D:\"
if errorlevel 1 goto CopyFail
goto :EOF
:CopyFail
echo.
echo Copying file "%DatabaseBackupPath%\WebServer\%NewestFile%" failed!
pause
Solution using WinRAR
My solution below is based on usage of Rar.exe, the console version of WinRAR as this compression tool has the feature to compress only files based on file dates. Therefore this solution is only helpful for you if you have installed WinRAR and bought a license of it.
The path of the program files folder of WinRAR must be set at top of the following batch file.
#echo off
SET DatabaseBackupPath=\\virtualserver1\Database Backups
SET WinRarFolder=C:\Program Files\WinRAR
cls
echo.
echo Restore WebServer Database
echo.
rem Find newest *.bak file in backup directory on server.
FOR /F "delims=" %%I IN ('DIR /B /O-D "%DatabaseBackupPath%\WebServer\*.bak"') DO (
SET NewestFile=%%I
goto BackupFound
)
cls
echo.
echo Restore WebServer Database
echo.
echo There is no *.bak file in "%DatabaseBackupPath%\WebServer\"
echo or the backup directory on server cannot be reached at all.
goto EndBatch
:BackupFound
echo Newest backup in "%DatabaseBackupPath%\WebServer"
echo is the file "%NewestFile%".
echo.
if exist "%TEMP%\ServerBackup.rar" del "%TEMP%\ServerBackup.rar"
"%WinRarFolder%\Rar.exe" a -cfg- -ep -inul -m0 -tn24h -y "%TEMP%\ServerBackup.rar" "%DatabaseBackupPath%\WebServer\%NewestFile%"
if errorlevel 1 goto OldBackup
"%WinRarFolder%\Rar.exe" e -cfg- -inul -y "%TEMP%\ServerBackup.rar" F:\Temp
if errorlevel 1 goto ExtractFailed
del "%TEMP%\ServerBackup.rar"
echo File "%NewestFile%" copied to D:\
goto EndBatch
:ExtractFailed
del "%TEMP%\ServerBackup.rar"
echo "Copying file "%NewestFile%" to D:\ failed.
goto EndBatch
:OldBackup
echo File "%NewestFile%" is older than 24 hours.
:EndBatch
set WinRarFolder=
SET DatabaseBackupPath=
SET NewestFile=
echo.
pause
Explanation:
The batch file first uses command DIR to get a list of *.bak files in the specified directory sorted according to last modification file date from newest to oldest.
With the FOR loop the name of the file name at top of the list (= newest file) is assigned to environment variable NewestFile.
The batch file outputs an error message if no *.bak file can be found in the specified directory, for example if the server cannot be reached at all at the moment with the permissions of the used user account.
But on success on finding a *.bak file in specified directory, the batch file informs the batch file user which file was found in which directory.
After making sure that the RAR archive to create next does not already exist in directory for temporary files, Rar.exe is used to create a RAR archive in directory for temporary files into which only the found file is stored without compression, but only if this file has a last modification date within 24 hours because of switch -tn24h.
For details on the switches used on Rar.exe see text file Rar.txt in program files directory of WinRAR.
Rar.exe exits with an return value greater 0 if there is any problem on creating the RAR archive. In this case the reason is most likely that the specified file is older than 24 hours which results now in an appropriate message written into the console window.
Rar.exe is on success on creating the RAR archive without compression used once more to extract the file to target directory.
An appropriate error mesage is shown in unlikely case that this fails. Otherwise the batch file outputs a success message and finishes with cleaning up environment table (not really needed if command prompt window closed next).
The temporarily used RAR archive is deleted in any case.
This is a batch based skill required program
That is used to backup data
The batch file is stored on a removable device like:
Pendrive
Flashdrive
SDCard (memory card)
When the batch file is executed it must collect a folder found at location "X"
D:/music
All data in this folder must be copied to the location of where the batch file is
If the batch file is located at any of these points
E:/
F:/
G:/
Even
Z:/
The batch operation should know where it is located and copy the music folder and paste it where the batch file is run from
My device when connected via USB changes sometimes
(E:/music)
(F:/music)
Batch file root folder
PC
E:/music (fixed location)
Code
Batch copies all content in E:/music
Batch pastes all Content to drive E:/ or F:/
Without errors
Batch file must know where it is
Can you please help me with this its very useful and resourceful because I can save content to my device and its very small
This simple batch file does the job:
#echo off
if exist "D:\music\*" (
echo Deleting %~d0\music ...
rd /S /Q "%~d0\music" 2>nul
echo Copying D:\music to %~d0\music ...
xcopy "D:\music\*" "%~d0\music" /S /E /C /I /Q /H /K
echo Folder D:\music copied to drive %~d0
pause
)
I used the information printed by running in a command prompt window the commands:
cmd /? ... outputs help for command CMD which is the interpreter of batch files, and listing also internal commands of the command line interpreter CMD.
for /? ... outputs help for command FOR with information about ~d to get drive from a string like argument 0 of the batch file referenced by %0 which is the name of the batch file with path.
rd /? ... outputs help for command RD with information how to silently delete an entire directory tree.
xcopy /? ... outputs help for command XCOPY with information how to silently copy an entire directory tree.
The command RD is used to first remove the target directory on removable media before copying the files from source directory to avoid a mixture of new and old folders and files.
The command XCOPY is used to copy the entire directory tree of D:\music to the removable media nearly completely silent. Just the total number of copied files is output which I think is good to see.
Whenever i want to delete the old IIS logs files on my Shared WebServer i run this command on CMD:
for /R C:\HostingSpaces %f in (u_ex*.log) do del /q "%~ff"
It searches for IIS log files and will delete all of them. For sure it can't delete the current day's log files since they are already opened in IIS and can't be deleted.
It shows me the progress of deleting log files one by one in command prompt console.
i need to set a schedule task to run a BAT file to do this automatically each day.
I have made a bat file and pasted this command within it:
for /R C:\HostingSpaces %f in (u_ex*.log) do del /q "%~ff"
But when i run the batch file nothing happens and i don't see any result and action.
How to write a batch file for running this Command?
Your help is much appreciate.
Thank you
You need to use %%A in batch files for For loops rather than %A at the command prompt.
del C:\HostingSpaces\u_ex*.log /s
is easier.
I strongly suggest to use the forfiles command for the task:
forfiles -p C:\inetpub\logs\LogFiles\ -s -m *.log -d -180 -c "cmd /C DEL #File"
Explanation of the switches:
-s or /S : recurse into all subfolders
-p or /P : path
-m or /M : file mask
-d or /D : number of days (-180 = older than 180 days)
-c or /C : command to execute
You can then put it into a Scheduled Task and have it run daily.
For a decent Powershell alternative, see this other answer: for other suggestions on how to properly reduce the IIS LogFiles folder, check out this post that I wrote on the topic.
The reason that you get error is because that the current day's logs are opened in IIS.
IIS keep the current day's log open and write on it constantly. So you will not be able to delete the current day's logs.
This is the command that worked for me. My websites are all placed in a folder named HOSTINGSPACES and i do this to delete all of the log files (only the current day logs will remain at the end)
# for /R C:\HostingSpaces %f in (u_ex*.log) do del /q "%~ff"
u_ex*.log is the syntax of my logs, your logs may have another syntax, so take care of this.
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.