How do I check if a bunch of files exist in my script?
I want my script to perform some add the files if its not present or just ignore and proceed with the rest of the code.
This is what I have writtes:
if not exist qapi/qapi*.h (
#ECHO OFF
CD %~dp0\..\..
ECHO %CD%
SET TOP_IOE_SW= %CD%
ECHO %TOP_IOE_SW%
SET BUILD_DIR=%TOP_IOE_SW%/build/ms
icacls * /q /c /t /grant Users:F
mkdir include\qapi
xcopy /Y qapi_export\qapi*.h include\qapi
xcopy /Y qapi\common\qapi*.h include\qapi
xcopy /Y core\api\qapi*.h include\qapi
xcopy /Y qapi\build include\build\
)
else (
call :next)
:next
////the rest of the code comes here///
When I run the script, it keeps prompting me to overwrite these files.
How can I have this done without the prompt?
Thanks
Related
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)
Following
Move all files except some (file pattern) from a DOS command
I want to copy all files from my release dir to the deployment dir with that batch file:
echo off
set "source=.\EasyRun\bin\Release\"
set "destination=C:\temp\EasyRun\"
IF exist %destination% rd %destination% /s /Q
IF exist %source% ( echo "OK release") ELSE ( echo "NO Realease DIR "%source% && pause && exit )
dir /b /a-d %source%|findstr /b "vshost" > excludeList.txt
xcopy /s %source%"*.exe" %destination% /exclude:excludeList.txt
xcopy /s %source%"*.dll" %destination%
echo "OK!!"
Pause
So here are two problems:
the excludeList is not filled and the file is empty
even when manually I put .\EasyRun\bin\Release*.vshost.exe it the xcopy doesn't exclude it from copying.
thanks
It might be caused by the empty excludeList.txt. -> Try to enter a dummy entry
I had some issues with xcopy and network shares.
(I got errors due to latency, access restrictions, ...)
I recommend robocopy with the /xf option. e.g. /xf vshost
Also the /MT[:N] multi-threaded copies should speed-up the copy process.
I am trying to run a simple xcopy batch script but I am facing some difficoulties.
Here is the script
SET SRC=C:\FOLDER1\FOLDER2
SET DEST=V:\FOLDER1
FOR /D %%d in (%SRC%\*) do xcopy /S /I /y /exclude:%SRC%\exclude.txt %%d V:\FOLDER1\%%~nxd
Basically this script should copy some of the subfolders of C:\FOLDER1\FOLDER2 (excluded.txt contains a list of directories to exclude) and its content to the destination. However when I run the scripts, altough no errors are thrown, NO FILES OR FOLDERS get copied. What am I doing wrong?
Interestingly if I run the following script INSIDE FOLDER2, everything procedes as expected.
FOR /D %%A in (*) DO xcopy /S /I /y /exclude:exclude.txt %%A V:\FOLDER1\%%A
xcopy "C:\FOLDER1\FOLDER2" "V:\FOLDER1" /e /i /y
You mention nothing about excluding any folders.
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.
I'm trying to have a bat inside the CD that will copy the Folder 'src' to desktop.
How can I get the files to copy to a directory, like the desktop, and then run the Java command?
xcopy /s/e /y c:\Desktop\Game \src
cd c:\Desktop\Game
java com/brackeen/javagamebook/tilegame/GameManager
#SET $source=*Your source path*
#SET $destination=*Your destination path*
#xcopy "%$source%" "%$destination%" /E /I /R /Y /Z
#IF ERRORLEVEL 1 ECHO Copy failed. && GOTO :EOF
#CD /D "%$destination%"
#*Start your java command*
Xcopy parameters used :
/E Copy folders and subfolders, including Empty folders
/I Assume the destination is a folder when the destination does not exist
/R Overwrite read-only files.
/Y Suppress prompt to confirm overwriting a file.
/Z Copy files in restartable mode.
More at SS64.com