I have this script which is created to backup a cube within an application. It runs every saturday and it created a sub folder within the application folder. Due to space requirements I've had to delete the oldest folder every week. I need to create a script to do that.
Here's the backup code:
:: Create folder for App Db if it does not exist
IF NOT EXIST %BACKUPDIR%%1"\"%2 GOTO MAKEDIR
GOTO CONT
:MAKEDIR
mkdir %BACKUPDIR%%1"\"%2
GOTO :CONT
:CONT
cd %BACKUPDIR%%1"\"%2
:: Get todays date
for /f "tokens=2,3,4 delims=/ " %%i in ('date /t') do (
set my_day=%%j
set my_month=%%i
set my_year=%%k )
set datestamp=%my_month%%my_day%%my_year%
::echo %datestamp%
:: Create date folder for app db
IF NOT EXIST %BACKUPDIR%%1"\"%2"\"%datestamp% GOTO MAKEDIRTIMEDATE
:MAKEDIRTIMEDATE
G:
cd %BACKUPDIR%%1"\"%2"\
mkdir %BACKUPDIR%%1"\"%2"\%datestamp%
:: Copy otl, csc, rul, rep files from App to Backup fodler
XCOPY %APPFOLDER%"\app\"%1"\"%2"\*.csc" %BACKUPDIR%%1"\"%2"\"%datestamp% /Y
XCOPY %APPFOLDER%"\app\"%1"\"%2"\*.rul" %BACKUPDIR%%1"\"%2"\"%datestamp% /Y
XCOPY %APPFOLDER%"\app\"%1"\"%2"\*.otl" %BACKUPDIR%%1"\"%2"\"%datestamp% /Y
XCOPY %APPFOLDER%"\app\"%1"\"%2"\*.rep" %BACKUPDIR%%1"\"%2"\"%datestamp% /Y
XCOPY %APPFOLDER%"\app\"%1"\"%2"\*.ind" %BACKUPDIR%%1"\"%2"\"%datestamp% /Y
cd %SCRIPTDIR%
:: Move data file from script dir to backupdir if data file exist If not exit out of code
IF NOT EXIST %SCRIPTDIR%%1"_"%2"_AllData.txt" GOTO EXIT
:END
MOVE %SCRIPTDIR%%1"_"%2"_AllData*.txt" %BACKUPDIR%%1"\"%2"\"%datestamp%
GOTO EXIT
:EXIT
exit /b
As you can see ever week a new folder is created with the creation data as its title (i.e. 102913). That folder is a subfolder of an application. I need a script to deleted the oldest folder.
So far I've tried (using test folders):
FORFILES -p "C:\Oracle\test\New folder\new folder" -s -m *.rul /D -<7> /C "cmd IF #isdir == TRUE" /c del #path
and
forfiles -p "C:\Oracle\test\New folder\new folder" -s -m *.rul /D -<7> /C "cmd /c del #path"
Any suggestion would be useful.
set "backupDir=C:\Oracle\test\New folder\new folder"
for /F "tokens=*" %%f in ('dir /ad /tc /o-d /b "%backupDir%"') do set "oldest=%backupDir%\%%f"
echo "%oldest%"
Get directory list ordered by creation date descending, only incuding directories, in brief format. Last line in list will be stored in variable %oldest% with the working directory prefixed.
Related
At my office we have to regularly open many different job folders on the fly at the same time. I was wondering if there was a script that could allow a user to input the folder name in CMD which will search specified directories on the network drive to open that folder in explorer.
An Example of the folder structure is below:
G/Work_A/2017:
Folder_A
Folder_B
Folder_C
Folder_D
G/Work_A/2018:
Folder_E
Folder_F
Folder_G
Folder_H
(I would like to search both 2017 & 2018 folders)
Also all the folders that users input have unique names.
Thanks for the help !
It is a bit unclear what you want hence why my and #npocmaka answers differ a bit :)
Regardless, This will prompt a user for a foldername, then do a search for that folder and open the folder or all instance of the folder on G:\ it can be amended to do all drives as well or limited to specific directories.
The point that stood out is the fact that you say each folder is unique, meaning each should only exist once on G:\ so the first scenario should work.
#echo off
set /p promptfolder="Please type the folder you wish to open: "
for /R G:\ %%f in (%promptfolder%) do #IF EXIST %%f explorer.exe "%%f"
if you want to specifically limit it to only the 2017 / 2018 directories:
#echo off
set /p promptfolder="Please type the folder you wish to open: "
for /R G:\Work_A\2017 %%f in (%promptfolder%) do #IF EXIST %%f explorer.exe "%%f"
for /R G:\Work_A\2018 %%f in (%promptfolder%) do #IF EXIST %%f explorer.exe "%%f"
To add it as a loop, to keep the batch open purely use goto
#echo off
:start
cls
echo Last folder requested: %promptfolder%
set /p promptfolder="Please type the folder you wish to open: "
for /R G:\Work_A\2017 %%f in (%promptfolder%) do #IF EXIST %%f explorer.exe "%%f"
for /R G:\Work_A\2018 %%f in (%promptfolder%) do #IF EXIST %%f explorer.exe "%%f"
cls
goto start
I'm not sure what exactly you want but you can try this:
#echo off
echo --Choose folder to open--
echo G:\Work_A\2017
(echo()
dir /b /a:d G:\Work_A\2017\
echo G:\Work_A\2018\
(echo()
dir /b /a:d G:\Work_A\2018\
set /p "year=Year(2018/2017):"
set /p "folder=Folder:"
if exist "G:\Work_A\%year%\%folder%\" (
explorer "G:\Work_A\%year%\%folder%\"
)
I am trying to get this script to backup a specific folder/files and zip them, then move the zip to a diff folder.
but i keep getting an error.
#ECHO off
SETLOCAL
ECHO + Setting up environment variables.
SET BACKPATH=%ThisService_RootDirectory%temp\
SET ARCPATH=C:\Program Files\7-Zip\7z.exe
SET ARCPARAMS=a -y
SET DAYSTOKEEP=3
SET ARCHIVE_DAYSTOKEEP=30
SET SOURCEPATH=%ThisService_RootDirectory%MPMissions
SET DEST_PATH=%ThisService_RootDirectory%Backups
SET BACKUP_DEST=%date:~-7,2%-%date:~4,2%-%date:~-4,4%
IF NOT EXIST "%BACKPATH%" (
ECHO ! Backup Path not found, exiting.
GOTO END
) ELSE (
ECHO * Backup Path Found.
)
IF NOT EXIST "%ARCPATH%" (
ECHO ! Archiver not found, exiting.
GOTO END
) ELSE (
ECHO * Archiver Found.
)
ECHO * Copying Files...
xcopy "%SOURCEPATH%\Documents" "%BACKPATH%\%BACKUP_DEST%\MPMissions" /v /e /s /i /y 1>NUL 2>NUL
ECHO * Archiving files...
CD /D "%BACKPATH%"
FOR /f %%a IN ('FORFILES /P %BACKPATH% /C "cmd /c if #isdir==TRUE echo #file" /D -%DAYSTOKEEP%') DO (
IF NOT EXIST %%a.7z (
"%ARCPATH%" %ARCPARAMS% %%a.7z %BACKPATH%\%%a\*.* 1>NUL 2>NUL
copy %%a.7z %DEST_PATH% 1>NUL 2>NUL
del %%a.7z 1>NUL 2>NUL
)
)
ECHO * Cleaning folders older than %DAYSTOKEEP% days..
FORFILES /P %BACKPATH% /C "cmd /c if #isdir==TRUE rmdir /s /q #file" /D -%DAYSTOKEEP% 1>NUL 2>NUL
ECHO * Cleaning files older than %DAYSTOKEEP% days..
FORFILES /P %BACKPATH% /M *.7z /C "cmd /c if #isdir==FALSE del #file" /D -%DAYSTOKEEP% 1>NUL 2>NUL
ECHO * Cleaning archives files older than %ARCHIVE_DAYSTOKEEP% days..
FORFILES /P %DEST_PATH% /M *.7z /C "cmd /c if #isdir==FALSE del #file" /D -%ARCHIVE_DAYSTOKEEP% 1>NUL 2>NUL
:END
ENDLOCAL
once I run the script it gives this message:
Setting up environment variables.
Backup Path Found.
Archiver Found.
Copying Files...
Archiving files...
ERROR: No files found with the specified search criteria.
Cleaning folders older than 3 days..
Cleaning files older than 3 days..
Cleaning archives files older than 30 days..
The script has executed successfully. You may close this window.
Now the variable %ThisService_RootDirectory% is part of tcadmin which is a gaming server service, so that variable where im executing the batch script would turn that variable into an actual path of the users service
example:
%ThisService_RootDirectory%
is
C:\TCAFiles\users\admin\5\
the script copies the files to the required folder, but it does not seem to zip the files and move the zip to the required folder.
can anyone give some assistance here please.
original source is at https://community.spiceworks.com/topic/482860-batch-script-to-transfer-and-compress
It's not a direct response to your question but why reinvent the wheel ?
All you need is in 7zBackup.ps1 (a powershell script which does exactly this)
I have source folder: c:\prefix\bin
I want to copy a set of specified files from source folder and its subfolders.
Lets say I want to copy:
bin
... gtsam.dll
... msvcr120.dll
... intel64\
... vc12\
... tbb.dll
To be more clearly for what I want to copy:
.\gtsam.dll .\msvcr120.dll .\intel64\vc12\tbb.dll
There are many files in the source directory and many subdirectories that I don't want to copy. And all the specified files I have to copy do not share a wild card. They are not all *.dll to copy to c:\dst
How I can do it with the most elegant way?
Using copy, or xcopy, or robocopy?
I suggest to create first a simple text file containing line by line the files to copy with relative path.
Example for FilesList.txt:
gtsam.dll
msvcr120.dll
intel64\vc12\tbb.dll
It is up to you in which directory to store this list file with the names of the files to copy.
The code below expects this file in directory C:\prefix\bin.
Then create a batch file with following code:
#echo off
pushd "C:\prefix\bin"
for /F "usebackq delims=" %%F in ("FilesList.txt") do (
%SystemRoot%\System32\xcopy.exe "%%~F" C:\dst\ /C /H /I /K /Q /R /Y >nul
)
popd
If target is specified with a backslash at end as done here and option /I is used, console application xcopy expects that C:\dst is a directory and even creates the entire directory structure to this directory automatically if not already existing.
Or you use this script with command copy and making sure the destination directory exists before copying the files.
#echo off
if not exist "C:\dst" (
md "C:\dst"
if errorlevel 1 (
echo.
echo Failed to create directory C:\dst
echo.
pause
goto :EOF
)
)
pushd "C:\prefix\bin"
for /F "usebackq delims=" %%F in ("FilesList.txt") do (
copy /B /Y "%%~F" C:\dst\ >nul
)
popd
Command md creates also entire directory tree on creating a directory with command extensions enabled as by default.
In both cases the directory C:\dst contains after batch file execution:
gtsam.dll
msvcr120.dll
tbb.dll
The main advantage of using a list file containing the names of the files to copy with relative path is easy updating in future without the need to change the batch code.
But let's say the files should be copied with duplicating the directory structure from source to destination directory resulting in directory C:\dst containing after batch file execution:
gtsam.dll
msvcr120.dll
intel64
vc12
tbb.dll
In this case the batch code with xcopy could be:
#echo off
setlocal EnableExtensions EnableDelayedExpansion
pushd "C:\prefix\bin"
if "%CD:~-1%" == "\" ( set "BasePath=%CD%" ) else ( set "BasePath=%CD%\" )
for /F "usebackq delims=" %%F in ("FilesList.txt") do (
set "SourcePath=%%~dpF"
set "RelativePath=!SourcePath:%BasePath%=!"
%SystemRoot%\System32\xcopy.exe "%%~F" "C:\dst\!RelativePath!" /C /H /I /K /Q /R /Y >nul
)
popd
endlocal
And the batch code using copy could be:
#echo off
if not exist "C:\dst" (
md "C:\dst"
if errorlevel 1 (
echo.
echo Failed to create directory C:\dst
echo.
pause
goto :EOF
)
)
setlocal EnableDelayedExpansion
pushd "C:\prefix\bin"
if "%CD:~-1%" == "\" ( set "BasePath=%CD%" ) else ( set "BasePath=%CD%\" )
for /F "usebackq delims=" %%F in ("FilesList.txt") do (
set "SourcePath=%%~dpF"
set "RelativePath=!SourcePath:%BasePath%=!"
md "C:\dst\!RelativePath!" 2>nul
copy /B /Y "%%~F" "C:\dst\!RelativePath!" >nul
)
popd
endlocal
For understanding the used commands and how they work, open a command prompt window, execute there the following commands, and read entirely all help pages displayed for each command very carefully.
copy /?
echo /?
endlocal /?
if /?
for /?
goto /?
md /?
pause /?
popd /?
pushd /?
set /?
setlocal /?
xcopy /?
Not tested:
#echo off
set "basedir=c:\prefix\bin"
set "destination=c:\destination"
for /r "%basedir%" %%# in (*gtsam.dll *msvcr120.dll *tbb.dll) do (
copy "%%~f#" "%destination%"
)
I'm using the following batch script to isolate yesterday's files from the source folder.
The script works up till the part where yesterday's and today's files are moved to the currnewer folder.
I can't find out why I'm unable to isolate yesterday's files from the currnewer folder and move them to curr folder.
I'd be very grateful if someone can tell me what's wrong with this script.
Thanks in advance!
REM yesterdays date
#echo off
set day=-1
echo >"%temp%\%~n0.vbs" s=DateAdd("d",%day%,now) : d=weekday(s)
echo>>"%temp%\%~n0.vbs" WScript.Echo year(s)^& right(100+month(s),2)^& right(100+day(s),2)
for /f %%a in ('cscript /nologo "%temp%\%~n0.vbs"') do set "result=%%a"
del "%temp%\%~n0.vbs"
set "YYYY=%result:~0,4%"
set "MM=%result:~4,2%"
set "DD=%result:~6,2%"
set "yesterday=%yyyy%%mm%%dd%"
mkdir C:\Users\ajay.shaan.shanmugam\Documents\currnewer
robocopy C:\Users\ajay.shaan.shanmugam\Documents\Source\ C:\Users\ajay.shaan.shanmugam\Documents\currnewer\ /move /maxage:%yesterday%
mkdir C:\Users\ajay.shaan.shanmugam\Documents\curr
robocopy C:\Users\ajay.shaan.shanmugam\Documents\currnewer\ C:\Users\ajay.shaan.shanmugam\Documents\curr\ /move /minage:%yesterday%
robocopy C:\Users\ajay.shaan.shanmugam\Documents\currnewer\ C:\Users\ajay.shaan.shanmugam\Documents\Source\ /move
rmdir /S /Q C:\Users\ajay.shaan.shanmugam\Documents\currnewer
user forfiles instead of the vbscript to work with file from the date you want and use it in combo with robocopy...
forfiles /p "C:\Users\ajay.shaan.shanmugam\Documents\Source" /D -1 /C "cmd /c robocopy #path C:\Users\ajay.shaan.shanmugam\Documents\currnewer\ /move"
you can specify if you want forfiles to look into subdirectory with /s and also specify a filter using /m .
Based on your question, and the example given, the script after optimization should look like this:
#echo off
set "yesterday=1"
set "Documents=%USERPROFILE%\Documents"
REM Creating directories currnewer and curr
mkdir "%Documents%\currnewer" "%Documents%\curr"
REM Moving source to currnewer (A to B)
robocopy "%Documents%\Source\" ^
"%Documents%\currnewer\" /move /maxage:%yesterday% /L
REM Moving currnewer to curr (B to C)
robocopy "%Documents%\currnewer\" ^
"%Documents%\curr\" /move /minage:%yesterday% /L
REM Moving currnewer to source (B to A)
robocopy "%Documents%\currnewer\" ^
"%Documents%\Source\" /move /L
REM Remove folder currnewer (B)
rmdir /S /Q "%Documents%\currnewer"
exit /b 0
note: I added /L to robocopy to list only (simulation)
I have to move my latest files alone into a folder named 'archive' using cmd .how should i do it?
for ex: i have files which are created in may,june,july,aug and sep in my_folder.
now i need to move all the files which are created in may,june,july and aug into my_archive and sep files alone have to stay in my_folder.
i used move c:\my_folder*.* c:\my_archive. but this moves all the files. what should be the correct script for doing this?
second thing is , i am thinking of using task scheduler to run the batch file which has this move script? will this help?
Thanks,
ResKing
Assuming that your date format is MM/dd/yyyy this might work:
#echo off
set month=%date:~0,2%
set year=%date:~-4%
mkdir temp_folder
rem move this month's files out of the way
forfiles /d +%month%/01/%year% /c "%comspec% /c move \"#path\" temp_folder"
rem move all other files to my_archive
forfiles /d -%month%/01/%year% /c "%comspec% /c move \"#path\" my_archive"
rem restore this month's files
move temp_folder\*.* .
rmdir temp_folder
This batch will cycle through all old files (not from current month) and move them one by one to my_archive directory.
I'm creating a new file just in case there are no new files in this directory (I'm using a new file as a break rule for the loop). This file is deleted in the end of the batch file.
The dir parameters are to show only files and to sort them by date. you can additionally specify the timestamp you prefer:
/T:C - Creation
/T:A - Last Access
/T:W - Last Written
#echo off
SETLOCAL ENABLEDELAYEDEXPANSION
SET SRC_PATH=c:\my_folder
SET DST_PATH=c:\my_archive
SET MONTH=%date:~4,2%
SET YEAR=%date:~-4%
echo blah > !SRC_PATH!\to_delete.txt
for /f "skip=4 tokens=1,5 delims= " %%g in ('dir /A:-D /O:D !SRC_PATH!') do (
set date=%%g
set m=!date:~0,2!
set y=!date:~-4!
if !YEAR! gtr !y! (move !SRC_PATH!\%%h !DST_PATH!\%%h) else (
if !MONTH! gtr !m! (move !SRC_PATH!\%%h !DST_PATH!\%%h) else goto :end
) )
:end
del !SRC_PATH!\to_delete.txt