Creating a batch file to back up a specified folder - windows

I am actually pretty new to this batch file thing. I know it's important to know at least the basic commands. How do I do the following?
Zipping a specified folder.
Move the folder to another place.
When zipping it, the ZIP file name will be the current date and
if there is another zipped file with the same name, it should be named like 20090924-2.
PS: 7-Zip is installed on my computer as an archive software.

The batch script listed below will do it (I have tested it
and it works to specifications). The directory to zip and
move is specified as its parent directory and the name of
the directory (PARENT_FOLDERTOZIP and FOLDERTOZIP in the
beginning) - I couldn't figure out how to copy entire
directories (I think XCOPY can only copy content of
directories and sub-directories.). The copy location is
specified as FOLDERTARGETLOCATION, and the directory to place
the compressed files in is specified as ZIPDIR.
The location of 7-Zip is configured through SEVENZIP_EXE.
Note also that getting the current date in the required
format depends on the short date format in regional
settings. I have listed three different versions for
ISO-8601, Central European and U.S.A. The active one in the
listing is for the U.S.A. (the "set FDATE=" line). If a
different one is needed then just copy-paste from one of the
other two.
That said it should be noted that this kind of thing is much
easier with Perl, Python or PowerShell.
#echo off
#title=Folder zip and move...
rem Parameters
rem Folder to zip and move
set PARENT_FOLDERTOZIP=T:\to delete
set FOLDERTOZIP=Folder to Compress
rem Target folder for moving the input folder to.
set FOLDERTARGETLOCATION=s:\move Here
rem Where to place compressed folders
set ZIPDIR=D:\toDelete\2009-09-24a
rem Configuration
set SEVENZIP_EXE=D:\Program Files\7-Zip\7z.exe
rem =================== Date ==============================================
rem There is no universal way inside batch itself to get a
rem date that is independent of regional settings (but is
rem quite trivial if an external program or script
rem (Perl/Python) is available).
rem
rem For short date formats:
rem
rem -------------------------------------------------------
rem
rem ISO-8601:
rem 0123456789
rem yyyy-MM-dd/ E.g.: 2009-09-24
rem
rem set FDATE=%DATE:~0,4%%DATE:~5,2%%DATE:~8,2%
rem
rem -------------------------------------------------------
rem
rem Central european:
rem 0123456789
rem dd/MM/yyyy E.g.: 24/09/2009
rem
rem set FDATE=%DATE:~6,4%%DATE:~3,2%%DATE:~0,2%
rem
rem -------------------------------------------------------
rem
rem US:
rem
rem 0123456789
rem MM/dd/yyyy E.g.: 09/24/2009
rem
rem set FDATE=%DATE:~6,4%%DATE:~0,2%%DATE:~3,2%
set FDATE=%DATE:~6,4%%DATE:~3,2%%DATE:~0,2%
set ZIPFILE=%ZIPDIR%\%FDATE%.7z
set FOLDERTOZIP_FULLPATH=%PARENT_FOLDERTOZIP%\%FOLDERTOZIP%
mkdir %FOLDERTARGETLOCATION%
rem Does a zip file already exist?
if exist "%ZIPFILE%" GOTO L_ZIPFILE_EXISTS
GOTO L_ZIPFILENAME_OK
rem Find a compressed file that does not already exist.
:L_ZIPFILE_EXISTS
set RNUM=0
:L_TRYANOTHER
set /a RNUM=%RNUM% + 1
set ZIPFILE=%ZIPDIR%\%FDATE%-%RNUM%.7z
echo Candidate: %ZIPFILE% ...
if exist "%ZIPFILE%" GOTO L_TRYANOTHER
rem Zip the folder!
:L_ZIPFILENAME_OK
"%SEVENZIP_EXE%" a %ZIPFILE% "%FOLDERTOZIP_FULLPATH%"
if exist "%ZIPFILE%" GOTO L_OKZIP
GOTO L_ERROREND
:L_OKZIP
rem Move folder: copy, then delete source.
set DEST_FOLDER=%FOLDERTARGETLOCATION%\%FOLDERTOZIP%
mkdir "%DEST_FOLDER%"
xcopy /Y /S "%FOLDERTOZIP_FULLPATH%"\*.* "%DEST_FOLDER%"\
rmdir /S "%FOLDERTOZIP_FULLPATH%"
GOTO L_END
:L_ERROREND
echo 7-Zipping failed !!!
:L_END
pause

Related

Using 7zip to archive file by older than 5 daya

i wanted to archive folder olden than 5 days.it seems set ARCHIVE_FILE_OLDER_THEN=-to5d is not working. can someone what i can add to archive files to 5 days .Previously it was working for RAR but not working 7z
set EDI832_US_ARCHIVE_DIR=C:\apps\harmony\data\EDI832\US\archive
REM -------------------- FILE NAME LIST ---------------------------------
set EDI832_US_FILE=EDI832_US_ARCHIVE
REM --------------- Extra CommandLine Parameters --------------------------------
REM Exclude file types for archiving
REM set EXCLUDE_FILES_TYPES=-x!*.rar -x!*.zip -x!*.7z
set EXCLUDE_FILES_TYPES=-xr!*.zip
REM Delete files after archiving
set DELETE_AFTER_ARCHIVE=-sdel
REM Archive files only older then days
set ARCHIVE_FILE_OLDER_THEN=-to5d
set EXTRA_PARAM=%EXCLUDE_FILES_TYPES% %DELETE_AFTER_ARCHIVE% %ARCHIVE_FILE_OLDER_THEN%
REM ------------------------------------------------------------------------------------
REM Path to WinRAR executable in Program Files
set path="C:\Program Files\7-Zip";%path%
REM $Env:Path = $Env:Path + ";C:\Program Files\7-Zip"
REM Set your WinRAR installation path for local testing
REM set path="C:\Program Files\7-Zip";%path%
REM Replace space in hour with zero if it's less than 10
SET hr=%time:~0,2%
IF %hr% lss 10 SET hr=0%hr:~1,1%
REM This sets the date like this: mm-dd-yr-hrminsecs1/100secs
Set TODAY=%date:~4,2%-%date:~7,2%-%date:~10,4%-%hr%%time:~3,2%%time:~6,2%%time:~9,2%
IF EXIST %EDI832_US_ARCHIVE_DIR% (
cd %EDI832_US_ARCHIVE_DIR%
**7z a "%EDI832_US_FILE%_%TODAY%" %EXTRA_PARAM%**

ClearCase can't merge from a snapshot view

I am using IBM Rational Clear Case,
I have a snapshot view, with some checked-out files. This view is about to be obsolete, and I need these checked-out files to be merge to a new version (New view).
My problem: I am using ClearCase Version Tree Browser (clearvtree.exe) to do my merge. I opened the Version Tree for one of the checked-out files, on the view to which I want to merge the file. Now when ever I try to select the checked out file: right click -> and select "Merge to" I get the following error:
"The selected version is not accessible from this view".
Note that when doing the same procedure on Dynamic View it works fine.
I know I can copy these files manually, but I am trying to find a way to do this, using the ClearCase tools (such as the Merge Tool and off-course the Version Tree).
OK, I have written a script (Actually two - which might be merged to one) that do what I need: To automatically merge from a snapshot view into a dynamic view. I assume it will also work with any other combination - but dynamic to dynamic or dynamic to snapshot is already supported by the IBM ClearCase "Merge Manager" tool.
First Scrip will find all checkouts and format them accordingly, while adding them to files.txt:
#echo off
REM ------------------------------- synopsis ----------------------------------
REM This script creates a list of all checked out into files.txt under the
REM batch-file directory.
REM files in the following format:
REM \VOB1\file1.txt##\main\branch_v111\CHECKEDOUT##Comment for file1
REM \VOB2\file2.txt##\main\branch_v111\CHECKEDOUT##Comment for file2
REM \VOB2\file3.txt##\main\branch_v111\CHECKEDOUT##Comment for file3
REM ------------------------------- synopsis ----------------------------------
set source_view_path=C:\Snapshot\some-snapshot-john
set currentDirectory=%~dp0
set chekedOutOutputFile=%currentDirectory%find_co.txt
set resultFile=%currentDirectory%files.txt
#echo Getting checkouts of %source_view_path%
#echo %currentDirectory%
REM ---------------------------------------------------------------------------
REM The next code produces a find_co.txt intermediate file with the following
REM format of checkouts:
REM <File Full Path>##<Version ID>##<File Comment>
REM %n - for file Name (With full path)
REM %Vn - for file Version ID.
REM %c - for file Comment
REM
REM Example:
REM C:\MY_VIEW_PATH\VOB1\file1.txt##\main\branch_v111\CHECKEDOUT##Comment for file1
REM C:\MY_VIEW_PATH\VOB2\file2.txt##\main\branch_v111\CHECKEDOUT##Comment for file2
REM C:\MY_VIEW_PATH\VOB2\file3.txt##\main\branch_v111\CHECKEDOUT##Comment for file3
REM ---------------------------------------------------------------------------
pushd %source_view_path%
cleartool lsco -cview -avobs -fmt "%%n##%%Vn##%%c" > "%chekedOutOutputFile%"
popd
del /q "%resultFile%"
REM ---------------------------------------------------------------------------
REM The following code formats the find_co.txt into files.txt with the desired
REM result - <File VOB Path>##<Version ID>##<File Comment>
REM Example:
REM From -
REM C:\MY_VIEW_PATH\VOB1\file1.txt##\main\branch_v111\CHECKEDOUT##Comment for file1
REM C:\MY_VIEW_PATH\VOB2\file2.txt##\main\branch_v111\CHECKEDOUT##Comment for file2
REM C:\MY_VIEW_PATH\VOB2\file3.txt##\main\branch_v111\CHECKEDOUT##Comment for file3
REM To
REM \VOB1\file1.txt##\main\branch_v111\CHECKEDOUT##Comment for file1
REM \VOB2\file2.txt##\main\branch_v111\CHECKEDOUT##Comment for file2
REM \VOB2\file3.txt##\main\branch_v111\CHECKEDOUT##Comment for file3
REM ---------------------------------------------------------------------------
for /F "usebackq tokens=*" %%A in ("%chekedOutOutputFile%") do (
call ::removeSourceViewPath "%%%A"
)
goto endOfScript
REM ---------------------------------------------------------------------------
REM Manipulate the path of each file to exclude the view from it e.g:
REM C:\MY_VIEW_PATH\MY_VOB\file.txt -> \MY_VOB\file.txt
REM >>>-----------------> start of :removeSourceViewPath
:removeSourceViewPath
set str=%1
call set "resultStr=%%str:%source_view_path%=%%"
set resultStr=%resultStr:~1,-1%
#echo %resultStr%
#echo.%resultStr%>>"%resultFile%"
exit /b
REM <<<-----------------< end of :removeSourceViewPath
REM ---------------------------------------------------------------------------
:endOfScript
pause
#echo ------------------------------------------------------------------
The second script takes the files.txt and merges them from a source view to a target view:
#echo off
REM ------------------------------- synopsis ----------------------------------
REM This script takes a list of all files from the files.txt which is under
REM this batch-file directory and merges them from TARGET to SOURCES views
REM files in the following format:
REM <File VOB Path>##<Version ID>##<File Comment>
REM are merged from <SOURCE_VIEW>\<File VOB Path> to
REM <TARGET_VIEW>\<File VOB Path> with the <File Comment> as comment.
REM ------------------------------- synopsis ----------------------------------
setlocal
set TARGET_VIEW=V:\v11-john-local-nt
set SOURCE_VIEW=C:\Snapshot\some-snapshot-john
REM ---------------------------------------------------------------------------
REM The following takes the line:
REM <File VOB Path>##<Version ID>##<File Comment> and checks out the target
REM file, then it automatically merges the file from source to target.
REM Note that the version is not required here (it might be required if we
REM want to merged from a version which is not the latest one in the branch).
REM ---------------------------------------------------------------------------
for /f "tokens=1,2,3 delims=##" %%i in (files.txt) do (
cleartool co -unreserved -c "%%k" %TARGET_VIEW%%%i
cleartool merge -to %TARGET_VIEW%%%i %SOURCE_VIEW%%%i
)
endlocal
pause
Both of this scripts merged all the file I needed from the source View to the target View.
Notes:
You can create a batch file that get the SOURCE_VIEW and TARGET_VIEW
in the command line as %1 and %2,
I splittend this into two scripts so I can remove some files from the
list, before actually doing the merge.
The scripts will preserve the original comment of the file.
%~dp0 - This is how I can force working in the current batch file
directory.
Feel free to comment. If you have a better solution I will be glad to
move my V to yours :-)
Since it is a snapshot view, its checked out files are only accessible in the actual view path, not in its view storage (like a dynamic view)
If you have access to the snapshot view path, you can use clearfsimport in order to automatically import the modified/new files from said snapshot view to your current view.
See a clearfsimport example here.
The description seems to contradict itself. Are you in ONE snapshot view and attempting to merge FROM a checked out version in another one? That generally will not work for the reason #VonC mentioned. The ClearCase core doesn't officially "know" where the most recent snapshot view workspace is for that other view, so it can't access the view-private copy. This could also fail for a dynamic view depending on view permissions.
If you are trying to merge TO an arbitrary version FROM your checked out version, you should be getting "element already checked out in this view" (Or words to that effect) since only one version of an element can be checked out in a given view.

How to find the new files after a specific time point in windows ?(command line)

I know there is a way in linux to filter all file generated after a specific time.
But how could we do that in windows command line ? Or in bash .
For example, I have three files in a folder. After 10/10/2016, 12:12:54, a new files was generated into this folder and I need to know the new file's name,size and path.
Or,
I don't know when the new files will be generated. I want to check each 10 mins. If there are some new files generated after a specific , I can get the file's name, path and size.
I search something about that , I know I can use forfiles /P directory /S /D +08/01/2013 to do that. But it will displays all the files which are modified after 08/01/2013 under directory. But I want it displays the folders in directory and all files in directory folder(not in its sub directory).
Although you did not show any own efforts to solve your task, I decided to provide a script that returns a list of files created since the previous execution. It does not check the file creation time stamp, because date/time maths is not natively supported in pure batch-file solutions. Instead it generates a list of files, stores it in a temporary file and compares it with a previously saved list.
Opposed to relying on file time stamps, this will for sure recognise every new files. When checking the time stamps, files may be considered as new since the last run erroneously or new files may not be recognised erroneously, particularly such files that are created during execution of the script.
So here is the code:
#echo off
setlocal EnableExtensions DisableDelayedExpansion
rem // Define constants here:
set "TARGET=D:\Data" & rem // (path to the directory to observe)
set "PATTERN=*.*" & rem // (search pattern for matching files)
set "LIST=%TEMP%\%~n0" & rem // (file base name of the list files)
set "FIRSTALL=#" & rem /* (defines behaviour upon first run:
rem set to anything to return all files;
rem set to empty to return no files) */
rem /* Determine which list file exists, ensure there is only one,
rem then toggle between file name extensions `.one`/`.two`: */
set "LISTOLD=%LIST%.two"
set "LISTNEW=%LIST%.one"
if exist "%LIST%.one" (
if not exist "%LIST%.two" (
set "LISTOLD=%LIST%.one"
set "LISTNEW=%LIST%.two"
) else (
erase "%LIST%.one"
if defined FIRSTALL (
> "%LIST%.two" rem/
) else (
erase "%LIST%.two"
)
)
) else (
if not exist "%LIST%.two" (
if defined FIRSTALL (
> "%LIST%.two" rem/
)
)
)
rem /* Create new list file, containing list of matching files
rem sorted by creation date in ascending order: */
> "%LISTNEW%" dir /B /A:-D /O:D /T:C "%TARGET%\%PATTERN%"
if not exist "%LISTOLD%" (
> nul 2>&1 copy /Y "%LISTNEW%" "%LISTOLD%"
)
rem // Search new list file for items not present in old one:
2> nul findstr /V /I /X /L /G:"%LISTOLD%" "%LISTNEW%"
if ErrorLevel 2 type "%LISTNEW%"
rem // Delete old list file:
erase "%LISTOLD%"
endlocal
exit /B
The first time the script runs, all files in the monitored directory are returned, unless you change set "FIRSTALL=#" to set "FIRSTALL=", in which case no files are returned the first time.
The core command is findstr which is configured so that the old list file provides literal search strings for being used to search the new list file and to return not matching lines, so the output are those lines of the new list file which do not occur in the old one.
Supposing the script is saved as new-files-since-last-run.bat, you could wrap around another tiny script that constitutes an endless loop with a polling rate of 10 minutes, like this:
#echo off
:LOOP
> nul timeout /T 600 /NOBREAK
call "%~dp0new-files-since-last-run.bat"
goto :LOOP
Setting up Windows Task Scheduler might be a better option though.

How to create flat directory from folder hierarchy and handle duplicate files?

I want to parse a directory and its subdirectories and copy all files in a target directory ignoring the original folder structure. All files shall be copied directly in the target directory.
I found a nice solution here for the Windows command line:
for /r FolderA %f in (*) do #copy "%f" target
https://stackoverflow.com/a/1502183/772434
That in general works fine, but I have duplicate files in the source directory which have the same name. There are too many files, so I can not handle those exceptions by hand.
How can I handle those files in the script automatically?
Options:
overwrite files during copying
keep copies and rename file e. g. by adding "__123" a number at the end of the file name
compare files (MDS5 sum or similar) and create copy only if files are different.
I would prefer option 3 (compare file content and keep numbered copies only if files are really different), but I'm also interested in solutions for options 1 and 2 (more pragmatic).
Here's an approach. i haven't tested it, so i hope it works as expected.
There is a global variable called CURRENT_DUPLICATE_SUFFIX. every time a file with the same name is found, but with different content, this value gets incremented and appended to the resulting file name
#echo off
SET SOURCE_DIR=C:\temp\LogAnalyzer
SET TARGET_DIR=C:\temp\test\output
SET CURRENT_DUPLICATE_SUFFIX=1
for /r "%SOURCE_DIR%" %%f in (*) do CALL :SUB1 "%%f"
GOTO END
:SUB1
SET PURE_FILE_NAME=%~nx1
SET TARGET_FILE_NAME=%TARGET_DIR%\%PURE_FILE_NAME%
IF NOT EXIST "%TARGET_FILE_NAME%" (
COPY %1 "%TARGET_DIR%"
GOTO :EOF
)
REM if we are here, the target file exists.
echo n |comp %1 "%TARGET_FILE_NAME%" >NUL 2>NUL
REM in case the files have the same content
IF %ERRORLEVEL%==0 GOTO :EOF
SET TARGET_FILE_NAME=%TARGET_FILE_NAME%_%CURRENT_DUPLICATE_SUFFIX%
SET /A CURRENT_DUPLICATE_SUFFIX=%CURRENT_DUPLICATE_SUFFIX%+1
COPY %1 "%TARGET_FILE_NAME%
GOTO :EOF
:END

Loop through files in FTP directory using bat file

I have a batch file that is executed by a scheduled windows service every 5 minutes. The batch files executes mget for a specific filemask: *.ext.
After mget it does mdel *.ext.
Recently I encountered a problem: When a file z.ext gets created between the execution of mget and mdel, that file gets deleted but it was not in the mget. Which off course makes sense.
So now I am looking for the proper windows ftp commands that will loop through the files and does a mget and mdel file by file.
Is that possible?
ftp scriptng is very limited, but I think you can use this approach. Basically, you have to run the ftp twice, first to collect the list of files to download, then the second time to download (and delete) each file (combined with lots of cmd scripting) You still don't get any error handling and ftp servers are not exactly known for their error tolerance.
The relevant example from the website above is:
#Echo Off
REM -- Define File Filter, i.e. files with extension .txt
Set FindStrArgs=/E /C:".txt"
REM -- Extract Ftp Script to create List of Files
Set "FtpCommand=ls"
Call:extractFileSection "[Ftp Script 1]" "-">"%temp%\%~n0.ftp"
Rem Notepad "%temp%\%~n0.ftp"
REM -- Execute Ftp Script, collect File Names
Set "FileList="
For /F "Delims=" %%A In ('"Ftp -v -i -s:"%temp%\%~n0.ftp"|Findstr %FindStrArgs%"') Do (
Call Set "FileList=%%FileList%% "%%A""
)
REM -- Extract Ftp Script to download files that don't exist in local folder
Set "FtpCommand=mget"
For %%A In (%FileList%) Do If Not Exist "%%~A" Call Set "FtpCommand=%%FtpCommand%% "%%~A""
Call:extractFileSection "[Ftp Script 1]" "-">"%temp%\%~n0.ftp"
Rem Notepad "%temp%\%~n0.ftp"
For %%A In (%FtpCommand%) Do Echo.%%A
REM -- Execute Ftp Script, download files
ftp -i -s:"%temp%\%~n0.ftp"
Del "%temp%\%~n0.ftp"
GOTO:EOF
:extractFileSection StartMark EndMark FileName -- extract a section of file that is defined by a start and end mark
:: -- [IN] StartMark - start mark, use '...:S' mark to allow variable substitution
:: -- [IN,OPT] EndMark - optional end mark, default is first empty line
:: -- [IN,OPT] FileName - optional source file, default is THIS file
:$created 20080219 :$changed 20100205 :$categories ReadFile
:$source http://www.dostips.com
SETLOCAL Disabledelayedexpansion
set "bmk=%~1"
set "emk=%~2"
set "src=%~3"
set "bExtr="
set "bSubs="
if "%src%"=="" set src=%~f0& rem if no source file then assume THIS file
for /f "tokens=1,* delims=]" %%A in ('find /n /v "" "%src%"') do (
if /i "%%B"=="%emk%" set "bExtr="&set "bSubs="
if defined bExtr if defined bSubs (call echo.%%B) ELSE (echo.%%B)
if /i "%%B"=="%bmk%" set "bExtr=Y"
if /i "%%B"=="%bmk%:S" set "bExtr=Y"&set "bSubs=Y"
)
EXIT /b
[Ftp Script 1]:S
!Title Connecting...
open example.com
username
password
!Title Preparing...
cd public_html/MyRemoteDirectory
lcd c:\MyLocalDirectory
binary
hash
!Title Processing... %FtpCommand%
%FtpCommand%
!Title Disconnecting...
disconnect
bye
Probably better to just do it in python or some other scripting environment (assuming you don't have deployment issues.)

Resources