I need a robocopy script that can run to replicate data from a source to a destination. the problem I have is I need to replicate certain folders from the source from different drives over to a destination server which will need to go to different drives also.
I'm using this script to copy a single folder over to the destination server (which this is running on)
#echo off
SET Day=%date:~6,4%.%date:~3,2%.%date:~0,2%
SET _source=\\10.10.10.18\R$\User\customer1
SET _target=E:\customer1
SET _log=E:\test_%Day%_Replication_Log.txt
SET _what=/MIR /COPYALL /B
SET _options=/NS /NC /NFL /NDL /R:0 /W:0 /RH:0615-1730 /MON:900 /MOT:30 /LOG+:"%_log%"
robocopy %_source% %_target% %_what% %_options%
I have a number of folders on the R drive as well as other drives that I wish to copy like this but I ideally dont want separate jobs for each and would like to run it under one batch file if possible.
Is there a way of having the source location specified in a text document and having the script read this and pass it to the command same would I also be able to specify the destination for this data i.e.:
customer1 - e drive
customer2 - f drive
etc
I would like this as easy as possible so that folders can be added / removed from the text file so the commands don't need to be altered.
Is this possible?
I think you are going to need to do something like the following..
SET _source=\\10.10.10.18\R$\User\customer1
rem SET _source=C:\windows
SET _target=E:\customer1
SET _log=E:\test_%Day%_Replication_Log.txt
SET _what=/MIR /COPYALL /B
SET _options=/NS /NC /NFL /NDL /R:0 /W:0 /RH:0615-1730 /MON:900 /MOT:30 /LOG+:"%_log%"
for /f "tokens=1*delims=-" %%a in ('dir /B/ad/s %_source%\drive*') do (
robocopy %%a %_target% %_what% %_options%
)
pause
Now, what I suspect you are going to need to do is something like the following..
Notice the oddball: "%~p0" - if you go to CMD (command line) and type in FOR /? - it will explain what these mean.. It like two or three screens in on the help..
SET _source=\\10.10.10.18\R$\User\customer1
rem SET _source=C:\windows
SET _target=E:\customer1
SET _log=E:\test_%Day%_Replication_Log.txt
SET _what=/MIR /COPYALL /B
SET _options=/NS /NC /NFL /NDL /R:0 /W:0 /RH:0615-1730 /MON:900 /MOT:30 /LOG+:"%_log%"
for /f "tokens=1*delims=-" %%a in ('dir /B/ad/s %_source%\drive*') do (
cd "%~p0%_source%\drive"
copy *.txt "%~p0 %_source%\drive"
FOR /F "delims=" %%A IN ('type %~p0ALIST.txt') do (
rem cd "%~p0%%A\%_target%"
rem copy *.txt "%~p0%%A\%_target%"
robocopy %%a %_target% %_what% %_options%
rem cd "%~p0%%A\drive"
)
pause
Related
I have 5000+ files inside a directory.
I want to manage these files 'splitting' every 500 files so first pack is copied inside folder1, pack2 of other 500 files is copied in folder2 and so on.
Which .bat script can I use ?
#ECHO OFF
SETLOCAL ENABLEDELAYEDEXPANSION
SET "sourcedir=U:\sourcedir\t w o"
SET "destdir=U:\destdir"
SET /a destcount=0
SET /a maxcount=5
SET /a filecount=maxcount
FOR /f "delims=" %%a IN (
'dir /b /a-d "%sourcedir%\*" '
) DO (
SET /a filecount +=1
IF !filecount! geq %maxcount% (
SET /a filecount=0
SET /a destcount +=1
MD "%destdir%\folder!destcount!"
)
ECHO(COPY "%sourcedir%\%%a" "%destdir%\folder!destcount!\"
)
GOTO :EOF
You would need to change the settings of sourcedir and destdir to suit your circumstances.
maxcount sets the number of files to assign to a group.
The required COPY commands are merely ECHOed for testing purposes. After you've verified that the commands are correct, change ECHO(COPY to COPY to actually copy the files. Append >nul to suppress report messages (eg. 1 file copied)
Essentially, count the number of transfers and increment the destination-directoryname each time a group is complete, using delayedexpansion to access the run-time values of the counters
the last 4-5 hours I spent trying to get my script fixed, and tried several things, but it still doesn't work as supposed. Here is the working part:
#echo off
rem automatically sort MP3s into an existing structure or add folders if needed
rem example of MP3-file pattern: Wu-Tang Clan - Gravel Pit (LP Version Clean).mp3
SETLOCAL
rem set variables
SET "source=g:\music\folder"
SET "str=Wu-Tang Clan - Gravel Pit (LP Version Clean).mp3"
SET "test=%source%\%str%"
rem split the filename from the remaining path. Take the left part
rem and assign it to %band% while the right part gets assigned to %song%
FOR /f "delims=" %%i IN ("%test%") DO SET "result=%%~nxi"
SET "band=%result: - =" & SET "song=%"
rem If there is no such folder (%band%), create it
IF not exist "%source%\%band%" MD "%source%\%band%"
rem As soon as there definetely is a fitting folder
rem move the file over there.
MOVE /-Y "%source%\%result%" "%source%\%band%"
The next step would be to enhance the code by a for-loop so I don't have to edit the script for every single one of my 3k+ files ;)
I tried really hard to get this code going, but failed:
#echo off
SETLOCAL
SET "source=g:\music\folder"
FOR %%f IN (%source%\*.mp3) DO (
echo %%f
FOR /f "delims=" %%i IN ("%%f") DO SET "result=%%~nxi"
SET "band=!result: - =" & SET "song=%"
IF not exist "%source%\%band%" MD "%source%\%band%"
MOVE /-Y "%source%\%result%" "%source%\%band%"
)
pause
So I added a for-loop and %%f was correctly filled at first and wasn't in the next for-loop.
FOR %%f IN (%source%\*.mp3) DO (
echo %%f
That resulted in: "g:\music\folder\Wu-Tang Clan - Gravel Pit (LP Version Clean).mp3"
Like it was supposed to. But after that
FOR /f "delims=" %%i IN ("%%f") DO SET "result=%%~nxi"
result and every following variable was empty, always.
I tried to fix it with a second variable as a 'helper':
FOR %%f IN (%source%\*.mp3) DO (
SET "helper=%%f"
FOR /f "delims=" %%i IN ("%helper%") DO SET "result=%%~nxi"
Even added 'enabledelayedexpansion' after I read about that and went for
FOR /f "delims=" %%i IN ("!helper!") DO SET "result=%%~nxi"
still doesn't work.
Now I could really need some help and would really appreciate it :)
regards Phoenix
Next code snippet could work. Note that the SET "band=%result: - =" & SET "song=%" trick can't be performed using ! delayed expansion unlike %-expansion. Therefore, that command is moved to the :myset subroutine and executed via call command.
#echo off
SETLOCAL EnableExtensions EnableDelayedExpansion
SET "source=g:\music\folder"
FOR %%f IN (%source%\*.mp3) DO (
echo %%f
FOR /f "delims=" %%i IN ("%%f") DO SET "result=%%~nxi"
call :myset
IF not exist "%source%\!band!" MD "%source%\!band!"
MOVE /-Y "%source%\!result!" "%source%\!band!"
)
goto :skipMyset
:myset
SET "band=%result: - =" & SET "song=%"
goto :eof
:skipMyset
pause
Resources (required reading):
(command reference) An A-Z Index of the Windows CMD command line
(additional particularities) Windows CMD Shell Command Line Syntax
(special page) EnableDelayedExpansion
Win7/64bit/batch/beginner
To sum this up, I'm trying to
search an/all external drive/s for a certain folder and
copy it to another location.
I looked up for-loops and the xcopy stuff but I'm completely new to batch scripting so this is what I got so far:
#echo off
::I found this to be useful for variables in for-loops
Setlocal EnableDelayedExpansion
set odrive=%odrive:~0,2%
set backupcmd=xcopy /S /C /G /D /E /I /R /Y
::this part is meant to find all available drives
for %%i in (D E F G H I J) do if exist %%i: (
::and store the drive name
set target=!target!%%i:\
echo "!target!"
::now I want to find a certain folder and use its filepath
for /d /r "!target!" %%a in (*) do if /i "%%~nxa"=="foldername"
set folderpath=!folderpath!%%a ::I want to use this just like I did earlier
echo "!folderpath!" ::but it turns out to be empty
::the last step is to use the folderpath for the backupcmd
%backupcmd% "!folderpath!" "%drive%\backedUpFiles"
::reset variables
set "target="
set "folderpath="
)
No idea if any of this is actually "good" batch scripting but so far I can follow and understand it. I'm expecting the error to be somewhere between the lines (literally) and maybe it has just to do with how I'm using variables.
The error is something about "invalid drive name".
I hope someone can help me since I'm really done searching code after some days.
Try this:
#echo off
Setlocal EnableDelayedExpansion
set searchitem=level 3
set target=%drive%\backedUpFiles
set backupcmd=xcopy /S /C /G /D /E /I /R /Y
set backupcmd=echo
for %%X in (C D E F G H I J) do if exist %%X:\ (
if exist "%%X:\%searchitem%\" %backupcmd% "%%X:\%searchitem%\" "%target%"
for /f "delims=" %%D in ('dir /b/s/ad "%%X:\%searchitem%" 2^>nul') do %backupcmd% "%%D" "%target%"
)
As examples I've set the foldername to search for to "level 3" to demonstrate foldernames with embedded spaces. In the loop we've got to handle firstlevel subfolders separately (a quirk of dir, not listing the foldername but it's content). I found dir to be much faster than a for /R loop, which I didn't get to run without errors anyway.
I was very disappointed that NetApp just doesn't add Feature to automatically update the (MS)SQL Job automatically every time a new Database gets created. (for a Full backup)
So i now want to share with you what I did to automatically add a new Database to out full SQL backup job.
Maybe someone has some ideas on how to make things a little bit more efficient or create not so much temporary files :)
Just Create a SQL job with executes the flowing batch file to update the NetApp SnapManager SQL Job.
#echo off
set SQLinstance=YOUR_SQL_SERVERNAME
REM if something differs
set SQL_SERVER=%SQLinstance%
REM SQL Verify Instalce
set SQL_VERIFY=ANOTHER_SERVERNAME\VERIFY
set db_list=list_%SQL_SERVER%.txt
set db_list_wo_summary=list_new_%SQL_SERVER%.txt
set linecount_file=linecount_%SQL_SERVER%.txt
set output-Full-User-file=output-Full-%SQL_SERVER%.txt
set output-Inc-User-file=output-Inc-%SQL_SERVER%.txt
set SQLCMD="C:\Program Files\Microsoft SQL Server\110\Tools\Binn\sqlcmd.exe"
set arg_start=C:\Program Files\NetApp\SnapManager for SQL Server\SmsqlJobLauncher.exe new-backup -svr ''%SQLinstance%'' -d ''%SQLinstance%'',
set arg_end_full= -ver -verInst ''%SQL_VERIFY%'' -mp -mpdir ''C:\Program Files\NetApp\SnapManager for SQL Server\SnapMgrMountPoint'' -RetainBackupDays 8 -RetainShareBackupDays 17 -cpylgbkshare COPYLOG_TOSHARE -lb -bksif -RetainSnapofSnapInfoDays 17 -rudays 1 -gen -mgmt daily
set arg_end_incremental= -ver -verInst ''%SQL_VERIFY%'' -mp -mpdir ''C:\Program Files\NetApp\SnapManager for SQL Server\SnapMgrMountPoint'' -RetainBackupDays 8 -RetainShareBackupDays 17 -cpylgbkshare COPYLOG_TOSHARE -lgbkonly -bksif -RetainSnapofSnapInfoDays 17 -gen -mgmt daily
set SQL_QUERIE="SELECT NAME FROM master.sys.databases AS dtb WHERE NOT (CAST(case when dtb.name in ('master','model','msdb','tempdb') then 1 else dtb.is_distributor end AS bit)=1) ORDER BY NAME"
set JOBNAME_FULL_BACKUP=jobname_full
set JOBNAME_INCREMENTAL_BACKUP=jobname_incremental
set SQL_UPDATE_COMMAND_START="UPDATE msdb.dbo.sysjobsteps SET command='"
set SQL_UPDATE_COMMAND_END=' FROM msdb.dbo.sysjobs, msdb.dbo.sysjobsteps WHERE msdb.dbo.sysjobs.job_id = msdb.dbo.sysjobsteps.job_id AND name = '%JOBNAME_FULL_BACKUP%'"
set SQL_UPDATE_COMMAND_END_INC=' FROM msdb.dbo.sysjobs, msdb.dbo.sysjobsteps WHERE msdb.dbo.sysjobs.job_id = msdb.dbo.sysjobsteps.job_id AND name = '%JOBNAME_INCREMENTAL_BACKUP%'"
REM Delete Old files (just to be sure)
del %db_list_wo_summary% %db_list% %linecount_file%
REM Get a List af all Databases and write them into a File
%SQLCMD% -E -S %SQLinstance% -o %db_list% -h -1 -s "," -W -Q %SQL_QUERIE%
setLocal EnableDelayedExpansion
REM Remove the last 2 Lines (one is a summary line, ant the other is empty)
set count=
for /f %%x in ('type %db_list% ^| find /c /v ""') do set /a lines=%%x-2
copy /y nul %tmp%\tmp.zzz > nul
for /f "tokens=*" %%x in ('type %db_list% ^| find /v ""') do (
set /a count=count+1
if !count! leq %lines% echo %%x>>%tmp%\tmp.zzz
)
move /y %tmp%\tmp.zzz %db_list_wo_summary% > nul
REM removes all linebreaks and adds a ' on every star and ending of the string (and adds a comma)
for /f "tokens=* delims= " %%a in (%db_list_wo_summary%) do (set s=!s!%%a'', '')
REM removes the last four chars (, '')(comma, space, and 2 '')
set s=!s:~0,-4!
REM counts the lines of the file NOT containing the string XXXYYYZZZ (basicaly a line count)
type %db_list_wo_summary% | find /V /C "XXXYYYZZZ" > %linecount_file%
for /f "tokens=* delims= " %%c in (%linecount_file%) do (set cn=%%c)
REM Creates the final string for the SQL job
set s1=%arg_start% ''%CN%'', ''!s!%arg_end_full%
set s2=%arg_start% ''%CN%'', ''!s!%arg_end_incremental%
REM Update the SQL Job step command with the new string via SQLCMD
%SQLCMD% -E -S %SQLinstance% -Q %SQL_UPDATE_COMMAND_START%%s1%%SQL_UPDATE_COMMAND_END%
%SQLCMD% -E -S %SQLinstance% -Q %SQL_UPDATE_COMMAND_START%%s2%%SQL_UPDATE_COMMAND_END_INC%
REM deleting the temp file
del %db_list_wo_summary% %db_list% %linecount_file%
I hope this can help anyone make things a bit more efficient.
The answer is above :)
Hopefully someone will find that user full, or at least inspiring ;)
Don't specify a database to backup at all ... then SMSQL will backup all databases, also the new ones
I am trying to copy files from one directory to another using a DOS batch script. The files I want to copy are the 4 or 3 latest files. That number will be static, but yet to be determined. Is there anyway to copy based on date modified?
Thank you
You could:
1) have the dir command sort files in the descending order of date modified;
2) use the output of the dir command in a `for loop to copy the corresponding files;
3) count to 3 (or 4) in the for loop to limit the number of files copied.
#ECHO OFF
SET "srcdir=D:\Source"
SET "tgtdir=D:\Target"
SET /A topcnt=3
SET /A cnt=0
FOR /F "tokens=*" %%F IN ('DIR /A-D /OD /TW /B "%srcdir%"') DO (
SET /A cnt+=1
SETLOCAL EnableDelayedExpansion
IF !cnt! GTR !topcnt! (ENDLOCAL & GOTO :EOF)
ENDLOCAL
COPY "%srcdir%\%%F" "%tgtdir%"
)