I have a backup program that saves .bak files into a folder which it rotates on a weekly basis automatically.
The files are given given names like this:
DB_Live_19052015.bak
DB_Test_19052015.bak
DB_Live_18052015.bak
DB_Test_18052015.bak
The backup program doesn't allow me to edit these names and I actually don't want it to.
What I do need is to be able to copy the newest file of each set DB_Live_XXXXXXXX.bak & DB_Test_XXXXXXXX.bak and rename them to drop the date so I end up with files like this for DR:
dr/DB_Live.bak
dr/DB_Test.bak
This would be overwritten each time the script was run.
No I can copy the latest file in a folder and rename it using scripting but I cannot get my head round how to
A. get the set of latest files (multiple)
B. rename these files based on their original names to drop only the on the end.
What I am expecting to have to do is the following:
copy the latest files to a dr folder
get the file names for each file
rename the files and overwrite anything already there with that name
I'm going to be adding this scripting into the backup program so it runs when the backup has finished.
The reason for these files is so I can RSYNC them off site without sending the whole file every time.
#echo off
setlocal enableextensions disabledelayedexpansion
set "source=%cd%\source"
set "target=%cd%\target"
for %%a in (DB_Live DB_Test) do (
set "first=1"
for /f "delims=" %%b in ('
dir /a-d /tw /o-d /b "%source%\%%a_*.bak"
') do if defined first (
set "first="
copy /b /y "%source%\%%~b" "%target%\%%a%%~xb"
)
)
For each set of files, execute a dir command in reverse modified date order. In this list the first file is the last modified. Copy this file to the target overwritting the existing file (if present).
#echo off
echo.>%temp%\tempone.txt
if not exist dr md dr
if not exist dblive md dblive
if not exist dbtest md dbtest
setlocal enabledelayedexpansion
for %%I in (*) do (
set red=%%I
set blue=!red:~0,7!
if "!blue!"=="DB_Live" copy /y !red! dblive>nul
if "!blue!"=="DB_Test" copy /y !red! dbtest>nul
)
pushd %cd%
cd dblive
for %%I in (*) do (
set green=%%I
set green=!green:DB_Live_=!
set green=!green:.bak=!
echo !green! >>%temp%\tempone.txt
)
set max=0
for /f %%x in (%temp%\tempone.txt) do (
set "FN=%%~nx"
if !FN! GTR !max! set max=!FN!
)
echo the latest dblive file is DB_Live_!max!.bak--copied to dr folder
set dblatest=DB_Live_!max!.bak
copy /y !dblatest! %temp%>nul
popd
copy /y %temp%\!dblatest! dr>nul
::same for dbtest
pushd %cd%
cd dbtest
for %%I in (*) do (
set green=%%I
set green=!green:DB_Test_=!
set green=!green:.bak=!
echo !green! >>%temp%\temptwo.txt
)
set max=0
for /f %%x in (%temp%\temptwo.txt) do (
set "FN=%%~nx"
if !FN! GTR !max! set max=!FN!
)
echo the latest dbtest file is DB_Test_!max!.bak--copied to dr folder
set dblatest=DB_Test_!max!.bak
copy /y !dblatest! %temp%>nul
popd
copy /y %temp%\!dblatest! dr>nul
::rename both files
pushd %cd%
cd dr
ren DB_Live_* DB_Live.bak
if %errorlevel%==0 echo renamed dblive
ren DB_Test_* DB_Test.bak
if %errorlevel%==0 echo renamed dbtest
pause
put in the same folder as the .bak files. hope this helps!
Related
I have many .zip files and folders some of them have identical names.
I will check zip file name and folder name if it is identical.
I will copy zip file into the folder and delete the zip file.
Is it possible to Automate the process?
I have found following code but couldn't edit it:
#echo off
setlocal EnableDelayedExpansion
pushd "C:\New folder"
FOR %%G IN (*.zip DO (
FOR /F "tokens=1 delims= " %%a IN ("%%G") do (
set "outFolder=%%a Random Center"
for /D %%i in (*.*) do (
for /F "tokens=1 delims= " %%b IN ("%%i") do (
if "%%a"=="%%b" set "outFolder=%%i"
)
)
if not exist "!outfolder!" md "!outfolder!"
move "%%G" "!outfolder!"
)
)
popd
pause
It creates Random Center folder and copy all the zip files in it which I don't want.
#echo off
setlocal
pushd "C:\New folder" || exit /b 1
for %%A in (*.zip) do if exist "%%~nA" (
pushd "%%~nA" && (
move /y "..\%%~nxA"
popd
)
)
popd
pause
Using move as see no need to
copy and del the zip file.
Argument /y will automatically
overwrite an existing file without
prompt.
Initial pushd changes the current directory
else exits with errorlevel 1.
The for loop iterates through each zip filename.
It checks if the name of the filename, without extension,
does exist, which is expected to be a folder.
If does exist, pushd into the directory, and then
move the zip file from the parent directory into
the current directory. popd will restore to the
previous directory.
Does final popd to restore to initial current directory.
We have many Oracle-Database-Client versions in our company.
The tnsnames.ora will be changed often.
I like to create a package (batch) to backup (rename) the present file in all versions of Oracle Client (if there are two or more versions installed) and after that, copy the new file.
My script for now looks so:
setlocal enabledelayedexpansion
set /a count=0
set OracleDir="%SystemDrive%\oracle\product"
for /d %%i in (%OracleDir%\*) do (
set dir=%%i\client_1\network\admin\
PushD !dir:\\=\!\
for /f "tokens=*" %%a in ('dir /b /od tnsnames.ora') do (
rename "%%a" "tnsnames-%Date%-Backup-!count!.bak"
)
set /a count+=1
IF NOT EXIST "%%i\client_1\network\admin\tnsnames.ora" (
copy "%~dp0tnsnames.ora" "%%i\client_1\network\admin\" /Y
)
)
endlocal
So far so good. But the script will only create a backupfile once.
But it must create a new "tnsnames-%Date%-Backup-!count!.bak" everytime I run the script.
Hope you know what I mean?
I'm sure, i need a jump point somewhere but no idea where.
IMO you did overcomplicate things.
The inner for is useless doing a dir for a file at a fixed location. This is better checked with an if exist.
If you then rename that file the next if is also useless, it can't exist - you just renamed it (better check if the rename was succesful)
setlocal enabledelayedexpansion
set /a count=0
set OracleDir="%SystemDrive%\oracle\product"
for /d %%i in (%OracleDir%\*) do (
set "dir=%%i\client_1\network\admin\"
PushD "!dir:\\=\!\" || (Echo couldn't pushd "!dir:\\=\!\"&Pause)
Set "Stamp=!Date!.!Time:~,2!.!Time:~2,2!"
if exist tnsnames.ora if not exist "tnsnames-!Stamp!-Backup.bak" (
rename tnsnames.ora "tnsnames-!Stamp!-Backup-!count!.bak"
)
set /a count+=1
if not exist tnsnames.ora copy "%~dp0tnsnames.ora" "!dir!"
PopD
)
endlocal
I'm new to scripting and I'm writing a batch file that will copy the recent or newly created files (.doc and .log) from two different source/directory and create a destination folder name "backup(date today)"
Basically , the two files are from different source and copy to one folder
New_doc.doc from C:\Doc_backup
New_log.log from C:\Log_backup
Destination: d:\file_backup\backup20140330
My code below will create a folder (with date) and copy the only file from source.
but I don't know copying two different file from different source to single folder.
#echo off
setlocal
::Create Directory with date
SET dd=%date:~0,2%
SET mm=%date:~3,2%
SET yy=%date:~6,4%
SET date=%yy%%mm%%dd%
md c:\file_backup\backup%date%
set srcDir=c:\doc_backup
set srcDir2=c:\log_backup
set destdir=c:\file_backup\backup%date%
set lastmod=
pushd %srcDir%
for /f "tokens=*" %%a in ('dir *.DOC /b /od 2^>NUL') do set lastmod=%%a
if "%lastmod%"=="" echo Could not locate files.&goto :eof
copy "%lastmod%" "%destDir%"
pause
All copy task could be performed, supposing variables srcDir and destDir are defined correctly, with next code snippet without for loop:
pushd "%destDir%"
copy /B "%srcDir%\*.doc"
popd
However, let's correct some mistakes in your code:
#echo off
setlocal
::Create Directory with date
SET "dd=%date:~0,2%"
SET "mm=%date:~3,2%"
SET "yy=%date:~6,4%"
SET "xdate=%yy%%mm%%dd%"
md "c:\file_backup\backup%xdate%"
set "srcDir=c:\doc_backup"
set "destdir=c:\log_backup\backup%xdate%"
md "%destdir%" 2>NUL
rem set lastmod=
pushd %srcDir%
for /f "tokens=*" %%a in ('dir *.DOC /b /od 2^>NUL') do (
rem set lastmod=%%a
rem if "%lastmod%"=="" echo Could not locate files.&goto :eof
copy /B "%%a" "%destDir%\"
)
pause
popd
endlocal
i have list of folders
files in all the folder are named in the same way
I want copying them in one folder without losing the order (folder 01(file01-02...) to folder 10)
I didn't find the way to do it with cmd or another way because I want to do it without any software just with windows
try this:
#ECHO OFF &SETLOCAL ENABLEDELAYEDEXPANSION
SET "startfolder=c:\data"
SET "targetfolder=x:\data"
for /d /r "%startfolder%" %%a in (*) do (
SET "fname=%%~a"
SET "fname=!fname:%startfolder%=!"
ECHO MD "%targetfolder%!fname!\%%~nxa" 2>nul
for %%b in ("%%~fa\*") do ECHO COPY "%%~fb" "%targetfolder%!fname!\%%~nb-new name%%~xb"
)
Look at the output and remove the word echobefore MDand COPY if it looks good.
I am making a batch file that copies my Google bookmarks and pastes them into another folder ( Kinda like a backup) called " backup of bookmarks". I wanted to make the script run every day at the end of the day so I can always have a back up ( may make it a scheduled task), and so I inserted a command to make a new folder( inside of the folder called backup of bookmarks) called " Backup" and have it increment by "1" every time the script is run ( ex: backup1; backup2; etc). I need help taking the file that I've just copied and have it pasted into the incremented folder. Here is the script, it copies and increments the folder, but I dont know how to make the copied file go into the incremented folder.
::this script backs up your Chrome bookmarks.
::Checks to see if the folder exits and makes a new folder incremented by 1
#echo off
setlocal enableDelayedExpansion
set "baseName=Backup"
set "n=0"
for /f "delims=" %%F in (
'2^>nul dir /b /ad "%baseName%*."^|findstr /xri "%baseName%[0-9]*"'
) do (
set "name=%%F"
set "name=!name:*%baseName%=!"
if !name! gtr !n! set "n=!name!"
)
set /a n+=1
md "%baseName%%n%"
xcopy "C:\Users\jnicholas\AppData\Local\Google\Chrome\User Data\Default\bookmarks" "C:\Users\jnicholas\Desktop\backup of bookmarks\"
pause
xcopy "C:\Users\jnicholas\AppData\Local\Google\Chrome\User Data\Default\bookmarks.bak" "C:\Users\jnicholas\Desktop\backup of bookmarks"
pause
move /y | /-y "C:\Users\jnicholas\Desktop\backup of bookmarks\bookmarks" "C:\Users\jnicholas\Desktop\backup of bookmarks\"%baseName%%n%""
I would recommend using time stamps rather than sequential numbering. Also here is everything in one command and it is user independent.
#xcopy "%LocalAppData%\Google\Chrome\User Data\Default\bookmarks*" "%UserProfile%\Desktop\Backup of Bookmarks\%Date:~10,4%-%Date:~4,2%-%Date:~7,2%\" /I /Y
This will copy both the bookmarks and bookmarks.bak file to a time-stamped folder inside the Backup of Bookmarks folder.
If you are using XP Pro or above then this creates a date and time stamped folder for your backups. The date and time formats are independent of local regional settings on the PC.
#echo off
for /f "delims=" %%a in ('wmic OS Get localdatetime ^| find "."') do set dt=%%a
set datestamp=%dt:~0,8%
set timestamp=%dt:~8,6%
set YYYY=%dt:~0,4%
set MM=%dt:~4,2%
set DD=%dt:~6,2%
set HH=%dt:~8,2%
set Min=%dt:~10,2%
set Sec=%dt:~12,2%
set stamp=%YYYY%-%MM%-%DD%_%HH%-%Min%-%Sec%
echo stamp: "%stamp%"
echo datestamp: "%datestamp%"
echo timestamp: "%timestamp%"
xcopy "C:\Users\jnicholas\AppData\Local\Google\Chrome\User Data\Default\bookmarks" "C:\Users\jnicholas\Desktop\backup of bookmarks\%stamp%\"
xcopy "C:\Users\jnicholas\AppData\Local\Google\Chrome\User Data\Default\bookmarks.bak" "C:\Users\jnicholas\Desktop\backup of bookmarks\%stamp%\"
pause