-3

sorry I'm new to the batch script just want to save dmp file in a specific folder on the basis of some condition

If DayName = Monday or tuesday then set FileName=C:\backupdb\kgote_%%k_%%j_%%i else if DayName = Wednesday or Thrusday then set FileName=C:\backupdb1\kgote_%%k_%%j_%%i

echo Taking backup of Kenduguri Database
for /F "tokens=1-4 delims=/ " %%i in ('date /t') do (
set DayOfWeek=%%i
set Month=%%j
set Day=%%k
set Year=%%l
REM set Date=%%i %%k %%j %%l
set FileName=C:\backupdb\kgote_%%k_%%j_%%i
)
exp kgote/kgote@ltcdb file=%FileName%.dmp log=%FileName%.log  full=y
XXAXX
  • 5
  • 3
  • 3
    To begin with your code does not seem to attempt anything other that checking for one day name, _(and even then you have a typo)_. What is the task you are requiring us to help you with? Are you wanting to perform a specific task if the dayname is Monday or Wednesday, and a different task if the dayname is Tuesday or Thursday? If so, could you please revisit your question explain exactly what you are having a problem with. – Compo Aug 19 '21 at 11:23
  • 1
    Addition to the above, how does `&` differ from `and` in your condition `(Monday & Wednesday and Tuesday & Thursday)`? This is confusing and nees to be explained. – Dávid Laczkó Aug 19 '21 at 11:25
  • sorry for the previous explanation. If any further explanation please tell. – XXAXX Aug 19 '21 at 12:59
  • 1
    Well if you want the directory to be either `C:\folder1`, or `C:\folder2`, depending upon the day name, why is your code using `C:\backupdb`? It makes things a whole lot simpler, if you do not unnecessarily give us incorrect information, because you'd then need to read, understand, and modify any code submitted as a solution, before you can test it. – Compo Aug 19 '21 at 13:24
  • Actually I have given the actual code here what I m using but for explaining I have used folder1 and folder2 – XXAXX Aug 19 '21 at 13:29
  • Just want to insert the backup file in either one of the folder depending upon the day condition – XXAXX Aug 19 '21 at 13:31
  • Okay, I'll ask it in a different way @XXAXX. What are the real folder names and paths? And can you please provide the actual location of `exp.exe`? If you do that I will consider submitting an answer for you, which you will not need to modify in any way, in order to test and use it! – Compo Aug 19 '21 at 13:35
  • Does this answer your question? [Setting a windows batch file variable to the day of the week](https://stackoverflow.com/questions/11364147/setting-a-windows-batch-file-variable-to-the-day-of-the-week) – Squashman Aug 19 '21 at 13:50
  • backupdb1 and backupdb folder name – XXAXX Aug 20 '21 at 05:55

2 Answers2

0

Here is an example, according to the information in your question at the time of posting, which you'll need to modify properly yourself in order to use it, because you have not confirmed the proper file paths.

@Echo Off
SetLocal EnableExtensions DisableDelayedExpansion
For /F Delims^=^= %%G In ('"(Set _) 2> NUL"') Do Set "%%G="
For /F %%G In ('%SystemRoot%\System32\wbem\WMIC.exe Path Win32_LocalTime Where^
 "DayOfWeek > 0 And DayOfWeek < 5" Get Day^, DayOfWeek^, Month /Value 2^>NUL'
) Do Set "_%%G" 2>NUL
If Not Defined _DayOfWeek (GoTo :EOF) Else Set "_FolderPath=C:\Folder2"
If %_DayOfWeek% Lss 3 Set "_FolderPath=C:\Folder1"
For /F Tokens^=%_DayOfWeek% %%G In ("Monday Tuesday Wednesday Thursday"
) Do Set "_DayOfWeek=%%~G"
For %%G In (Day Month) Do (Set /A _%%G += 100 & SetLocal EnableDelayedExpansion
    For %%H In ("!_%%G:~-2!") Do EndLocal & Set "_%%G=%%~H")
Set "_FileName=%_FolderPath%\pcote_%_Day%_%_Month%_%_DayOfWeek%"
"F:\ull\PathTo\exp.exe" kgote/kgote@ltcdb file=%_FileName%.dmp log=%_FileName%.log full=y

This script will just close with no action, if the day name is not Monday, Tuesday, Wednesday, or Thursday.

The parts you may need to modify are limited to just C:\Folder2 on line 7 (i.e. the location for the backup path on Wednesday and Thursday), C:\Folder1 on line 8, (i.e. the location for the backup path on Monday and Tuesday), and F:\ull\PathTo on line 14, (The location of the exp.exe commandline utility).

Compo
  • 36,585
  • 5
  • 27
  • 39
  • echo Taking backup of Kenduguri Database for /F "tokens=1-4 delims=/ " %%i in ('date /t') do ( set DayOfWeek=%%i set Month=%%j set Day=%%k set Year=%%l REM set Date=%%i %%k %%j %%l set FileName=C:\backupdb\kgote_%%k_%%j_%%i ) exp kgote/kgote@ltcdb file=%FileName%.dmp log=%FileName%.log full=y exit – XXAXX Aug 20 '21 at 05:45
  • buy this batch file command I have successfully generate a file in my c drive >dbbackup folder now the requirement is that if the day is Monday Wednesday Friday it will save in dbbackup folder and if the day is Tuesday Thursday Saturday it will save in dbbackup2 folder of c drive – XXAXX Aug 20 '21 at 05:46
  • Just need if-else condition on the basis of day name to generate a file in different folders – XXAXX Aug 20 '21 at 05:50
  • Don't understand the use of exp.exe. Can you only help me with the if-else statement of the above script provided by me. That will be very grateful as I'm very new to this. – XXAXX Aug 20 '21 at 05:59
  • **First**, I'll not provide a quick fix for your code, because it is not up to standard. Your use of `date /t` will only work on your specific PC, with your current locale and configuration settings. Using Windows default configuration settings for my locale, `date /t` outputs `20/08/2021`, _if I'm not mistaken_, (as you can see there is no day name)! **Second**, My answer above performs the task asked in the question when I posted it, do not change your parameters after the fact, because I'm not changing my answer to suit it. **Third** `exp.exe` is the name of the export utility you're using! – Compo Aug 20 '21 at 11:42
  • thank you sir this actually works for me by removing "F:\ull\PathTo\exp.exe" to "exp". – XXAXX Aug 20 '21 at 12:52
  • I am very naive to this, so sorry for all my miscommunication. Thank you – XXAXX Aug 20 '21 at 12:54
  • Okay, just to explain when you use `exp`, what happens is that the `%PATHEXT%` variable is read, and every extension listed, _(in order first to last)_, within it is appended to the end of `exp` until an executable file with that name is located in the current working directory at that time. So if the end user PC has never modified this variables content, the system would search for `exp.COM` in the current directory, before `exp.EXE` is found and run. If `exp.exe` is not in the current directory, the system will then search each of the locations listed in `%Path%` for the executable file. – Compo Aug 20 '21 at 15:51
  • So it would then check the current directory for `exp.COM`, `exp.EXE`, `exp.BAT`, `exp.CMD`, `exp.VBS`, `exp.VBE`, `exp.JS` `exp.JSE`, `exp.WSF`, `exp.WSH`, `exp.MSC`, and because those aren't found it would search the first location listed under `%Path%`, for each of those, then the next location in `%Path%`, etc. until a matching file is found. As you can imagine this takes time. If however, you provide the full location and extension, as I have recommended in my code above, no searching is done, and the correct file is run immediately. Now do you understand? – Compo Aug 20 '21 at 15:56
  • It is also important that you realise that both the PATHEXT, and Path variables are easily configured, and modified, both correctly by third party processes and the end user. Any mistakes, can render those useless, and could therefore prevent your files from running at all. If the full path is used, that possibility is removed, and the code is therefore considerably more robust as a result of it. You'll also notice that I used the full path and extension, for `WMIC.exe` too. so that a badly configured system, or one with many locations listed in `Path`, would not affect this script's function. – Compo Aug 20 '21 at 16:02
0

Another way to do this would be using PowerShell. Unless your organization has blocked it, PowerShell is available on all supported Windows systems.

=== Invoke-KendugurBackup.bat

"%SystemRoot%\System32\WindowsPowerShell\v1.0\powershell.exe" ^
    -NoLogo -NoProfile -File "%USERPROFILE%\bin\Invoke-KendugurBackup.ps1"

=== Invoke-KendugurBackup.ps1

[CmdletBinding()]
Param()
$DayOfWeek = [int](Get-Date).DayOfWeek
# Test for Monday through Thursday. Do not backup Sunday, Friday, and Satrday.
if ($DayOfWeek -gt 0 -and $DayOfWeek -lt 5) {
    Write-Verbose "DayOfWeek is $DayOfWeek"
    $BaseDir = (@(1,2) -contains $DayOfWeek) ? 'C:\folder1' : 'C:\folder2'
    $FileBaseName = "$BaseDir\kgote_$(Get-Date -Format 'dd_MM_yyyy')"
    Write-Verbose "FileBaseName is $FileBaseName"
    & "exp" kgote/kgote@ltcdb file=$FileBaseName.dmp log=$FileBaseName.log full=y
}
lit
  • 14,456
  • 10
  • 65
  • 119