Batch file that creates folder with wildcard in path - batch-file

I want to write a batch file that creates a folder (if it does not exist) and copies a certain file into that folder. So far so good.
The problem is that one folder in the path varies slightly from time to time, so a wildcard becomes necessary.
The following code works just fine but obviously misses to create the folder (Reports). So if the folder is not there, it simply does nothing.
for /r "c:\Users\%USERNAME%\AppData\Local\Packages" &&G in ("LocalState\acn\Reports") do #if exist %%G xcopy /s /i /y c:\temp\Reporting "%%G"
The full path is:
c:\Users\FSchneider\AppData\Local\Packages\“WILDCARD"\LocalState\acn\Reports\
Any idea?

Add /d switch in for to indicate you're looking for a directory, not a file
Add * and omit quotes in the wildcard to indicate it's actually a wildcard
No need for if exist now
for /d /r "%LocalAppData%\Packages" %%G in (LocalState\acn.*) do xcopy /s /i /y c:\temp\Reporting "%%G\Reports"

Next script could help.
#ECHO OFF
SETLOCAL enableextensions
set "_fldrtop=%USERPROFILE%\AppData\Local\Packages"
set "_fldrsub=LocalState\acn"
if not "%~1"=="" set "_fldrsub=%~1" :: my testing data, remove this line
set "_fldrlow=Reports"
if not "%~2"=="" set "_fldrlow=%~2" :: my testing data, remove this line
for /F "delims=" %%G in ('dir /B /AD "%_fldrtop%"') do (
if exist "%_fldrtop%\%%G\%_fldrsub%\" (
if exist "%_fldrtop%\%%G\%_fldrsub%\%_fldrlow%\" (
echo echo "%_fldrtop%\%%G\%_fldrsub%\%_fldrlow%\"
) else (
echo md "%_fldrtop%\%%G\%_fldrsub%\%_fldrlow%\"
)
rem echo xcopy /s /i /y c:\temp\Reporting "%_fldrtop%\%%G\%_fldrsub%\%_fldrlow%\"
)
)
Output:
==>D:\bat\SO\31672436.bat
==>D:\bat\SO\31672436.bat "LocalState\Cache"
md "C:\Users\UName\AppData\Local\Packages\winstore_cw5\LocalState\Cache\Reports\"
==>D:\bat\SO\31672436.bat "LocalState\Cache" 2
echo "C:\Users\UName\AppData\Local\Packages\winstore_cw5\LocalState\Cache\2\"

Related

Move files with batch and browse sub folders

I created a batch file that allow to move a file from a folder to another.
My issue is that my bat file should also browse source subfolders in order to find files that have a specific pattern.
Actually :
#ECHO ON
SET SourceDir=C:\Users\me\Documents\source
SET CopyDir=C:\Users\me\Documents\repository
SET FilePatterName=*pattern*.pdf
FOR %%A IN ("%SourceDir%\%FilePatterName%") DO (
ECHO F | XCOPY /Y /F "%%~A" "%CopyDir%\"
DEL /Q /F "%%~A"
)
GOTO EOF
For example : in my source folder, if i have sub1, sub2, sub3 folders and a sub1-1 folder in sub1, i would like to check each folders, check the files and move them without creating any folder in the repository
As per my comment, use for /r which will recurse through the directories:
for /R "%SourceDir%" %%A in ("%FilePatterName%") do...
if it feels too ugly then first pushd to the directory, then recursively search from there:
#Echo off
Set "SourceDir=C:\Users\me\Documents\source"
Set "CopyDir=C:\Users\me\Documents\repository"
Set FilePatterName=*pattern*.pdf
Pushd "%SourceDir%"
For /R %%a in ("%FilePatterName%") do (
Echo F | Xcopy /Y /F "%%~a" "%CopyDir%\"
Del /Q /F "%%~a"
)
Popd
Here's an alternative, using the same structure but robocopy instead of xcopy and del:
#Set "SourceDir=%UserProfile%\Documents\source"
#Set "CopyDir=%UserProfile%\Documents\repository"
#Set "FilePatterName=*pattern*.pdf"
#If Exist "%SourceDir%\" For /R "%SourceDir%" %%# In ("%FilePatterName%")Do #"%__AppDir__%Robocopy.exe" "%%~dp#." "%CopyDir%" "%%~nx#" /Mov>NUL 2>&1
If you really needed to see the filenames, I suppose you could include additional RoboCopy options like /FP, /NDL, /NS, /NC, /NJH and /NJS.

batch add leading zeroes filename

I've got a script that I'm using for all kinds of things, including renaming and repacking comics. Variants of this script are used by others as well.
I've been seeing a limitation in repacking comics, however; I've found that some comics don't have the leading zeroes at their pages, which makes the pages appear out of order. So, I've added a part that should add leading zeroes.
The file does the following:
loop over all subfolders of the current folder
rename comic archives to their proper extension (so I can see where it is)
loop over all archives, and extract them to a temporary folder
loop over all files in the temporary folder, and add leading zeroes
repack it as a 7zip
rename the file to a comic book extension
Somehow, it's not renaming the files properly, and the name of the archive appears as a subfolder in the archive. For example:
let's repack 'testfolder', which contains images from 1 to 100. It renames, extracts, packs, and renames again, without problem. However, the new archive contains the folder named 'testfolder' in the archive next to the images, which don't have the leading zeroes. I'm not sure what's going on, and I've been fighting with it for a while now, so I thought to put it online (it's a good script to share, anyway). Does anyone have an idea on what's going wrong here?
#ECHO ON
rem mode con: cols=80 lines=60
for /f "delims=" %%F in ('dir /ad/s/b') do (
cd %%F
IF EXIST *.cbr (
RENAME *.cbr *.rar
)
IF EXIST *.cbz (
RENAME *.cbz *.zip
)
IF EXIST *.cb7 (
RENAME *.cb7 *.7z
)
FOR %%I IN (*.RAR, *.ZIP *.7Z) DO (
ECHO Extracting %%I...
"C:\Program Files\7-Zip\7z.exe" e "%%I" -oC:\TMPPACKDIR\* -y | FIND /V "ing "
echo %%~nI
cd C:\TMPPACKDIR\%%~nI\
FOR /f "delims=" %%P IN ('dir *.JPG, *.PNG, *.BMP') DO (
SET %%N = %%P
SET %%N = 00%%N
SET %%N = %%N:~-2%
echo %%P
echo %%N
pause
rename 'C:\TMPPACKDIR\%%~nI\%%P' %%N
)
pause
echo %%F
cd %%F
ECHO Repacking
"C:\Program Files\7-Zip\7z.exe" a -t7z "%%~nI.7z" "C:\TMPPACKDIR\%%~nI*" -mx=9 | FIND /V "ing "
IF %ERRORLEVEL% EQU 0 RD /S /Q C:\TMPPACKDIR
ECHO Renaming new file
RENAME *.7z *.CB7
ECHO Removing original file
DEL "%%I"
ECHO File %%I is done
)
)
REM del /f/q "%~0" | exit
Yes, the problem is in the code portion between cd C:\TMPPACKDIR\%%~nI\ and pause. You are trying to set a for variable reference %%N, which does not work. You need to use a normal environment variable instead, like NAME, for example; you can only do sub-string expansion (like ~-2 in your code) using normal environment variables. In addition, since you are setting and reading the same environment variable within a single block of code, you need to use delayed expansion; otherwise, you would always receive the value present when the entire block is read.
The code portion should look like this:
cd /D "C:\TMPPACKDIR\%%~nI"
for /F "delims=" %%P in ('dir /B *.JPG, *.PNG, *.BMP') do (
set "FILE=C:\TMPPACKDIR\%%~nI\%%P"
set "NAME=00%%~nP"
setlocal EnableDelayedExpansion
set "NAME=!NAME:~-2!"
rename "!FILE!" "!NAME!%%~xP"
endlocal
)
pause
I've removed some unnecessary stuff and made a few changes, (the main error being that which aschipfl has already identified).
FOR /F "DELIMS=" %%F IN ('DIR/AD/S/B') DO (
PUSHD "%%F"
IF EXIST *.cbr REN *.cbr *.rar
IF EXIST *.cbz REN *.cbz *.zip
IF EXIST *.cb7 REN *.cb7 *.7z
FOR %%I IN (*.RAR, *.ZIP *.7Z) DO (
ECHO Extracting %%I...
"%ProgramFiles%\7-Zip\7z.exe" e "%%I" -o"C:\TMPPACKDIR\*" -y
PUSHD "C:\TMPPACKDIR\%%~nI"
FOR %%P IN (*.JPG, *.PNG, *.BMP) DO (
SET "_N=100%%~nP"
SETLOCAL ENABLEDELAYEDEXPANSION
SET "_N=!_N:~-2!"
REN "%%P" "!_N!%%~xP"
ENDLOCAL
)
POPD
ECHO Repacking
"%ProgramFiles%\7-Zip\7z.exe" a -t7z "%%~nI.7z" "C:\TMPPACKDIR\%%~nI*" -mx=9
IF NOT ERRORLEVEL 1 RD/S/Q "C:\TMPPACKDIR\%%~nI"
ECHO Renaming new file
REN "%%~nI.7z" "%%~nI.CB7"
ECHO Removing original file
DEL "%%I"
ECHO File %%I is done
)
POPD
)
Things to look into:Can 7z.exe not just extract .cbr, .cbz & .cb7 directly without renaming them first. In the same way, when repacking once you've provided the file type, -t7z can the file not be given the name "%%~nI.CB7" directly instead of later renaming it.

Iterate Directories with Batch

The objective is to display the directory name in MYFOLDER.
MY.exe exists in the folder, but curiously, without the wildcard in ...\desktemp*, the "#echo Showing subfolders" is never displayed, but "#echo G is working" is. However MY.exe is never found when moved to one of the subfolders.
OTOH the current code never finds MY.exe and never displays "#echo G is working" but properly lists each subfolder: "#echo Showing subfolders".
The other problem is the pauses at the end of the block are never reached.
Substituting the inner For with
cd \Users\%USERNAME%\Desktop
for /D /r %%G in ("desktemp*") do (
gets essentially the same result. My.exe isn't found if moved to one of the subfolders of desktemp.
Setlocal EnableDelayedExpansion
set CURRDRIVE=C
SET MYFOLDER=
:SEARCHDRIVES
REM BLOCK
for %%B in (C) do (
if exist %%B: (
PUSHD %%B:\
if NOT DEFINED MYFOLDER (
ECHO "%CD%"
REM This always displays path batch is run from.
REM The above Pushd doesn't change to C:\
for /f "tokens=*" %%G in ('dir /b /s /a:d "%%B:\Users\%USERNAME%\Desktop\desktemp*" ^| find "\"') do (
#echo Showing subfolders
#echo %%G
pause
if exist "%%G\MY.exe" (
call set MYFOLDER=%%G
#echo %%G
#echo G is working
call echo %MYFOLDER%
pause
GOTO GOTMYFOLDER
)
)
)
)
REM Exist Drive
)
REM Drives Loop
:GOTMYFOLDER
cd /d %CURRDRIVE%:\
echo %MYFOLDER%
cd %MYFOLDER%
pause
The above is a chunk whittled from a larger code block: the ultimate aim will be to get the folder names"\Users\New\Desktop\desktemp" into a variable via prompt.
Are the Escape Characters, Delimiters and Quotes in the nested blocks implemented properly?
The answer escaped this poor little brain until it cottoned on to what the "DIR" and "For /D /R" were really up to. What was sought for was in the addition of a new "For /D" (no /R).
This first (extra) "For /D" determines the folder names to iterate from.
(Specifically anything but the Windows directory where we run into problems with >260 filenames.)
This locates the MY.exe file somewhere in the Users folder (more precisely in any root folder beginning with U):
Setlocal EnableDelayedExpansion
set CURRDRIVE=C
SET MYFOLDER=
:SEARCHDRIVES
REM BLOCK
for %%B in (C) do (
if exist %%B: (
PUSHD %%B:\
if NOT DEFINED MYFOLDER (
ECHO "%CD%"
REM This always displays path batch is run from.
REM The above Pushd doesn't change to C:\
for /D %%Z in (U*) do (
cd \%%Z
for /D /r %%G in ("*") do (
if exist "%%G\MY.exe" (
call set MYFOLDER=%%G
#echo %%G
#echo G is working
call echo %MYFOLDER%
pause
GOTO GOTMYFOLDER
)
)
)
)
)
REM Exist Drive
)
REM Drives Loop
:GOTMYFOLDER
cd /d %CURRDRIVE%:\
echo %MYFOLDER%
cd %MYFOLDER%
pause
Edit:
The source of the error spam in the comment below is this command:
Insert batch code to elevate UAC privileges [code][1] from TanisDL
Setlocal EnableDelayedExpansion & pushd "%CD%" & CD /D "%~dp0"
set CURRDRIVE=C
FOR /F "usebackq delims==" %%G IN (dir %CURRDRIVE%:\ /A:D /O:G /S /B ^| FIND /I "myString") DO (set "foundMyString=%%~pG")

Excluding a folder in a recursive copy in Batch

Basically what's going on is that we are migrating about 50 desktops from XP to 7. I cannot install any extra programs to complete this task. What I am doing is writing a script that copies the Desktop, Favourites, and My Documents, along with a few specific file types from the originating machine to a shared drive for the user. Which later will be able to have all files moved to the new machine they are getting. I'm trying to recursively search through Windows and get all .pst files and other files you will see in the script, and back them up to a folder on the share (but not in a directory structure, I just want all the files in a single directory no matter where they originated from) with the exception of My Documents. Anything they have put in My Documents should be excluded in the search. Anyway, this is what I started with:
#echo off
cls
set USRDIR=
set SHARE=
set /P USRDIR=Enter Local User Directory:
set /p SHARE=Enter Shared Drive Name:
set UPATH="c:\Documents and Settings\%USRDIR%"
set SPATH="g:\!MIGRATION"
set ESRI="%UPATH%\Application Data\ESRI"
net use g: /delete
net use g: \\server\%SHARE%
md %SPATH% %SPATH%\GIS %SPATH%\Outlook %SPATH%\Desktop %SPATH%\Documents %SPATH%\Favorites
if exists %ESRI% md %SPATH%\ESRI
md %SPATH%\misc %SPATH%\misc\GISfiles %SPATH%\misc\XMLfiles %SPATH%\misc\CSVfiles
for /R %%x in (*.mxd) do copy "%%x" "%SPATH%\GIS\"
for /R %%x in (*.dbf) do copy "%%x" "%SPATH%\misc\GISfiles\"
for /R %%x in (*.xml) do copy "%%x" "%SPATH%\misc\XMLfiles\"
for /R %%x in (*.csv) do copy "%%x" "%SPATH%\misc\CSVfiles\"
for /R %%x in (*.pst) do copy "%%x" "%SPATH%\Outlook\"
if exist %ESRI% xcopy /y /d /s /i /z %ESRI% %SPATH%\ESRI && echo ESRI YES || ESRI NO
xcopy /y /d /s /i /z "%UPATH%\Desktop" "%SPATH%\Desktop" && echo DESK YES || DESK NO
xcopy /y /d /s /i /z "%UPATH%\My Documents" "%SPATH%\Documents" && echo DOCS YES || DOCS NO
xcopy /y /d /s /i /z "%UPATH%\Favorites" "%SPATH%\Favorites" && echo FAVS YES || FAVS NO
echo "Script Complete!"
pause
I then decided that I wanted to exclude My Documents just in case so I don't end up with a bunch of duplicates where anything they put in My Documents ends up getting copied twice. So I changed that recursive block to this:
for /R %%x in (*.mxd) do xcopy /y /d /z /exclude:"\My Documents\" "%%x" "%SPATH%\GIS\"
for /R %%x in (*.dbf) do xcopy /y /d /z /exclude:"\My Documents\" "%%x" "%SPATH%\misc\GISfiles\"
for /R %%x in (*.xml) do xcopy /y /d /z /exclude:"\My Documents\" "%%x" "%SPATH%\misc\XMLfiles\"
for /R %%x in (*.csv) do xcopy /y /d /z /exclude:"\My Documents\" "%%x" "%SPATH%\misc\CSVfiles\"
for /R %%x in (*.pst) do xcopy /y /d /z /exclude:"\My Documents\" "%%x" "%SPATH%\Outlook\"
Anyway, my question is this, is this the most efficient way of doing this? Is there a better way? I'm curious because I have no one here to bounce ideas or anything off of, I'm the sole on site support here. If someone sees a better way or thinks this is how they would do it, please let me know. There are a bunch more filetypes in there, but I removed most of them so the code sample wasn't so long, left enough to get the point across.
EDIT: Just to make it clear, I am not the IT Department for this organization. I'm a technical support liaison for the department to a separate IT division. Our actual IT department calls this a migration, but it's more or less a simple "lets in stall new machines without doing anything to prepare for it" sort of soup sandwich operation. I can't install, remove, or change the systems in any way, I can only backup the files.
Usually, the best option is reduce processing in batch files to the minimum, leaving as much as possible to commands. But if you have to iterate over the file system several times, it is better to do only one pass and process in batch.
Adapted from a more general batch. I have made changes to adjust to what you need, but somethings more specific (your ESRI folder) are not added. Adapt as needed.
#echo off
setlocal enableextensions enabledelayedexpansion
rem Ask for share name
set /P share=Enter Shared drive name:
if "%share%"=="" (
call :error "No share name provided"
goto endProcess
)
rem Configure target of copy
set target=g:
rem Connect to target
net use %target% /delete
net use %target% \\server\%share%
if not exist %target% (
call :error "No connection to server"
goto endProcess
)
rem Configure directory by extension
set extensions=.mxd .dbf .xml .csv .pst
set .mxd=GIS
set .dbf=misc\GISFiles
set .xml=misc\XMLFiles
set .csv=misc\CSVFiles
set .pst=Outlook
rem adjust target of copy to user name
set target=%target%\!MIGRATION\%username%
if not exist "%target%" (
mkdir "%target%"
)
rem Configure source of copy
set source=%userprofile%
if not exist "%source%" (
call :error "User profile directory not found"
goto endProcess
)
rem Resolve My Documents folder
call :getShellFolder Personal
set myDocPath=%shellFolder%
if not exist "%myDocPath%" (
call :error "My Documents directory not found"
goto endProcess
)
rem Ensure target directories exists
mkdir "%target%" > nul 2>nul
for %%e in ( %extensions% ) do mkdir "%target%\!%%e!" >nul 2>nul
rem We are going to filter file list using findstr. Generate temp file
rem with strings, just to ensure final command line is in limits
rem This will contain not desired folders/files or anything that will be
rem copied later
set filterFile="%temp%\filter.temp"
(
echo %myDocPath%
echo \Temporary Internet Files\
echo \Temp\
) > %filterFile%
rem Process user profile, excluding My Documents, IE Temp and not needed extensions
for /F "tokens=*" %%f in ('dir /s /b /a-d "%source%" ^| findstr /v /i /g:%filterFile% ^| findstr /i /e "%extensions%" ') do (
call :processFile "%%~xf" "%%f"
)
rem Now, process "especial" folders
mkdir "%target%\Documents"
xcopy /y /d /s /i /z "%myDocPath%" "%target%\Documents"
call :getShellFolder Desktop
if exist "%shellFolder%" (
mkdir "%target%\Desktop"
xcopy /y /d /s /i /z "%shellFolder%" "%target%\Desktop"
if errorlevel 1 (
call :error "Failed to copy desktop files"
)
)
call :getShellFolder Favorites
if exist "%shellFolder%" (
mkdir "%target%\Favorites"
xcopy /y /d /s /i /z "%shellFolder%" "%target%\Favorites"
if errorlevel 1 (
call :error "Failed to copy favorites"
)
)
rem Finish
goto :endProcess
rem ** subroutines *******************************************
:processFile
rem retrieve parameters
rem : %1 = file extension
rem : %2 = file
set ext=%~1
set file=%~2
rem manage .something files
if "%ext%"=="%file%" (
set file=%ext%
set ext=
)
rem manage no extension files
if "%ext%"=="" (
rem WILL NOT COPY
goto :EOF
)
rem determine target directory based on file extension
set extCmd=%%%ext%%%
for /F "tokens=*" %%d in ('echo %extCmd%^|find /v "%%" ' ) do set folder=%%d
if "%folder%"=="" (
rem file extension not in copy list
goto :EOF
)
copy /y /z "%file%" "%target%\%folder%" >nul 2>nul
if errorlevel 1 (
call :error "Failed to copy [%file%]"
) else (
echo %file%
)
goto :EOF
:getShellFolder
set _sf=%~1
set shellFolder=
if "%_sf%"=="" goto :EOF
for /F "tokens=2,*" %%# in ('reg query "HKCU\Software\Microsoft\Windows\CurrentVersion\Explorer\Shell Folders" /v "%_sf%" ^| find "%_sf%"') do (
set shellFolder=%%$
)
goto :EOF
:error
echo.
echo ERROR : %~1
echo.
goto :EOF
:endProcess
endlocal
exit /b
Try User State Migration Tool from Microsoft - I used it to move around 400 systems in less than 30 days. It takes a bit of setup for the script to run right, but it does a really really good job (and no program to install).
In default mode, it gets all PST files, Document, Desktop, Favorites, and a ton of other folders/registry settings. It also does this for all users on a computer (which can be limited by last login date or number of profiles) as long as the user running it is a local admin.
It may or may not work for what you need, but it's a good choice when designing a migration. It sounds like what you are trying to do can be done with USMT just by removing extra code in the ini files.
This checks and excludes any folder that ends with documents\ (or includes it, so subdirectories too) as IIRC the folder may be called \documents\ or \my documents\ and is case insensitive.
setlocal enabledelayedexpansion
for /R %%x in (*.mxd) do set "check=%%~dpx" & if /i "!check!"=="!check:documents\=!" copy "%%x" "%SPATH%\GIS\"
for /R %%x in (*.dbf) do set "check=%%~dpx" & if /i "!check!"=="!check:documents\=!" copy "%%x" "%SPATH%\misc\GISfiles\"
for /R %%x in (*.xml) do set "check=%%~dpx" & if /i "!check!"=="!check:documents\=!" copy "%%x" "%SPATH%\misc\XMLfiles\"
for /R %%x in (*.csv) do set "check=%%~dpx" & if /i "!check!"=="!check:documents\=!" copy "%%x" "%SPATH%\misc\CSVfiles\"
for /R %%x in (*.pst) do set "check=%%~dpx" & if /i "!check!"=="!check:documents\=!" copy "%%x" "%SPATH%\Outlook\"
endlocal

How to copy files from a random folder into a destination folder in batch?

I need a batch script to copy files from a random subfolder of a specific directory into a destination directory.
For example, there will be a number of files in their own subdirectory, for example
.../source/a/file.txt
.../source/b/file.txt
So there a number of these files, and I would like to randomly select one of them and have it copied into the new directory
.../destination/file.txt
So the file.txt in the destination is just being overwritten with other files that have the same name but different content.
I'm new to batch scripting and I can't quite figure out how to select each file from a random subfolder. I'd also like it to repeat every 30 seconds until I terminate the script, but I think it should be easy enough to just make a second script that calls this .bat file every 30 seconds once I get it going.
Thanks!
This can do what you request. Just set your source directory, destination directory, and your file name filter.
#echo off
setlocal EnableExtensions EnableDelayedExpansion
pushd "...\source\"
:: Enumerate Files. Method 1
set "xCount=0"
for /r %%A in (file.txt) do if exist "%%~A" set /a "xCount+=1"
echo %xCount%
:: Select a Random File.
set /a "xIndex=%Random% %% %xCount%"
echo %xIndex%
:: Find an Copy that File. Method 1
set "xTally=0"
for /r %%A in (file.txt) do if exist "%%~A" (
if "!xTally!" EQU "%xIndex%" (
xcopy "%%~fA" "...\destination\file.txt" /Y
goto End
)
set /a "xTally+=1"
)
:End
popd
endlocal
pause
Type xcopy /? to see all of its options.
Here are some alternate loop methodologies for the file enumeration.
:: Enumerate Files. Method 2
set "xCount=0"
for /f %%A in ('dir *.txt /a:-d /s ^| find "File(s)"') do set "xCount=%%~A"
echo %xCount%
:: Find an Copy that File. Method 2
set "xTally=0"
for /f "delims=" %%A in ('dir *.txt /a:-d /b /s') do (
if "!xTally!" EQU "%xIndex%" (
xcopy "%%~fA" "...\destination\file.txt" /Y
goto End
)
set /a "xTally+=1"
)
Enjoy :)
#echo off
setlocal EnableDelayedExpansion
rem Enter into the directory that contain the folders
pushd \Fullpath\source
rem Create an array with all folders
set i=0
for /D %%a in (*) do (
set /A i+=1
set folder[!i!]=%%a
)
rem Randomly select one folder
set /A index=(%random%*i)/32768 + 1
rem Copy the desired file
copy "!folder[%index%]!\file.txt" "\Fullpath\destination" /Y
rem And return to original directory
popd
The features of batch script is
It copies all files from source to destination folder with similar structure(even retains empty folders).
Can retain N number of days recent files in Archive folder(source) remaining files will be moved to backup folder(destination).
Can be scheduled to N number of days to N number of years.
Can be used in any Source to destination folder backup.
Source folder can add N number of folder any time and delete folder or files, but Destination folder always adds the folder and never deletes any folder or file.
#echo off
Set "sourcefolder=E:\Interfaces"
Set "destinationfolder=E:\BackupInterface"
If Exist %sourcefolder% (
For /F %%* In ('Dir /b /aD "%sourcefolder%" 2^>nul') do (If Not Exist "%destinationfolder%\%%*" ( RD /S /Q "%destinationfolder%\%%*")
xcopy /e /v /i /y /q "%sourcefolder%\%%*" "%destinationfolder%\%%*"
forfiles /p "%sourcefolder%\%%*" /s /d -30 /c "cmd /c del /Q /S #file" )
) Else (echo.Source folder could not be found)
:end of batch
echo.&echo.finished!

Resources