nerds and pro´s.
I am trying to get my head around a simple thing like this:
#echo off
MKDIR "C:\Program Files (x86)\Start-proTM" (
IF %ERRORLEVEL0%(
GOTO :ok
)
IF %ERRORLEVEL1%(
GOTO :no
)
)
IF EXIST "C:\Program Files (x86)\Start-proTM" (
GOTO :ok
) ELSE (
MKDIR "C:\Program Files (x86)\Start-proTM"
)
IF %ERRORLEVEL%=0 (
GOTO :ok
)
IF %ERRORLEVEL%=1 (
GOTO :no
)
#ECHO off
:ok
ECHO Ok...
pause
:no
ECHO NO...
Pause
don´t mind the rand. PAUSE,
just to simplyfy things.
:ok REM would be rewritten to take care of the install process.
:no REM is the %ERRORLEVEL% return in txt for the user.
Just want a simple .exe to MKDIR, check if such has been created. Then install(extract) the files in set directory. And let the user know the current status of the install process, through promt in cmd.exe
Anyone? any suggestions?
I might be totally blinded, by the simplicity of the structure?
This is how I've done something like this before:
set "TARGETPATH=C:\Program Files (x86)\Start-proTM"
echo Extracting files to %TARGETPATH%
if exist "%TARGETPATH%" (rmdir "%TARGETPATH%" /S /Q || exit /b 1)
mkdir "%TARGETPATH%" || exit /b 1
Note: If you try to set and use variables inside a parenthetical scope, you will need to do setlocal EnableDelayedExpansion and surround the variable names with exclamation (!) points instead of percent (%) signs.
Related
I wanted to make a batch file with multiple else conditions and already searched a lot but mine is not working. I want the file to check if two files exists and if they do then open one of that files. If one of the two files do not exist the batch should compare the next two files. My file looks like that:
IF EXIST "file1.txt" IF EXIST "file2.txt" Goto V1
IF EXIST "file3.txt" IF EXIST "file4.txt" Goto V2
IF EXIST "file5.txt" AND IF EXIST "file6.txt" Goto V3
:V1
cd "folder"
start sample.exe
goto commonexit
:V2
cd "folder2"
start sample2.exe
goto commonexit
:V3
cd "folder3"
start sample3.exe
goto commonexit
:commonexit
Like this the cmd opens and immediately closes. When I comment out the ":commonexit" it opens and works throug the code but it seems like in the double IF conditions (IF ... IF...) it only cares about the second IF. Putting an AND operator between them does not help.
Has anyone of you a guess, what could be wrong?
EDIT: the :commonexit is working. I just did not know that a breakline after :commonexit would make the code corrupt.
Here is one way:
#echo off
if exist "file1.txt" if exist "file2.txt" (
cd "folder"
start sample.exe
goto :EOF
)
if exist "file3.txt" if exist "file4.txt" (
cd "folder2"
start sample2.exe
goto :EOF
)
if exist "file5.txt" if exist "file6.txt" (
cd "folder3"
start sample3.exe
goto :EOF
)
Here is the logic:
file1 is checked for existence, if exist check if file2 exists, if it does, cd, execute sample and then goto end of file. If however file1 or file2 does not exist, it will skip the current code block and goto the next line of if's
Simpler and closer to the original code:
IF EXIST "file1.txt" IF EXIST "file2.txt" cd "folder" & start sample.exe & goto commonexit
IF EXIST "file3.txt" IF EXIST "file4.txt" cd "folder2" & start sample2.exe & goto commonexit
IF EXIST "file5.txt" IF EXIST "file6.txt" cd "folder3" & start sample3.exe & goto commonexit
rem Put here code that will execute when none of the previous paths execute...
:commonexit
EDIT: New method added
The new method below allows to write an efficient "AND" operation over several "EXIST filename" tests, and to chain several of these tests in a way similar to several "ELSE IF" commands:
(for /F "skip=1" %%a in ('dir /B file1.txt file2.txt') do break) && (
echo Both file1.txt and file2.txt exists
echo Process they here
) || (for /F "skip=1" %%a in ('dir /B file3.txt file4.txt') do break) && (
echo Both file3.txt and file4.txt exists
echo Process they here
) || (for /F "skip=1" %%a in ('dir /B file5.txt file6.txt') do break) && (
echo Both file5.txt and file6.txt exists
echo Process they here
) || echo Last else
This method is based on the "ExitCode" value returned by for /F command as explained at this answer (below Exit Code management section). The method can be easily extended to more files by just inserting the additional file names and adjusting the "skip=1" value to the number of files minus one. For example:
(for /F "skip=2" %%a in ('dir /B file1.txt file2.txt file3.txt') do break) && (
echo All file1.txt, file2.txt and file3.txt exists
echo Process they here
) || (for /F "skip=2" %%a in ('dir /B file4.txt file5.txt file6.txt') do break) && (
echo All file4.txt, file5.txt and file6.txt exists
echo Process they here
) || echo Last else
Here's an alternative one line solution, (split over 3 to maintain max 80 character line lengths), which uses the Where command:
(Where/Q "file1.txt"&&(Where/Q "file2.txt"&&Start /D "folder" "sample.exe"))||(
Where/Q "file3.txt"&&(Where/Q "file4.txt"&&Start /D "folder2" "sample2.exe"
))||Where/Q "file5.txt"&&Where/Q "file6.txt"&&Start /D "folder3" "sample3.exe"
I'm working on the following batch script and I'm not sure how I can achieve the results I'm looking for. I searched here as well as online, but I didn't see anything that looked like what I'm looking for. Any help is appreciated.
I need to check if two files exist as well as if they don't. Currently I've got the code below working, however if one file exists or is missing it goes in a 3rd direction (I've updated the code to account for the 3rd direction so now it works fine, but I know there is much room for improvement!). I know it's not pretty, but it works and the pretty code doesn't.
Please note that there are other files in the same directory with the same extensions, so searching by extension will not work.
Working Code:
#echo off
echo checking file structure...
if exist "file1.exe" (
if exist "file2.zip" (
goto ok
)
)
if not exist "file1.exe" (
if not exist "file2.zip" (
goto download
)
)
if not exist "file1.exe" (
goto download
)
)
if not exist "file2.zip" (
goto download
)
)
:download
echo downloading missing files.
:ok
echo Install successful
What I would like to do:
(The following code isn't expected to work, it's my thoughts written out)
#echo off
set file1=setup.exe
set file2=package.zip
if exist $file1 && $file2 then goto ok
else if not exist $file1 || $file2 goto download
Example of why checking for the two files alone will not work
echo checking file structure...
if exist "setup.exe" if exist "package.zip" goto TEST1
if not exist "setup.exe" if not exist "package.zip" goto TEST2
ECHO [Error!] - 1 File is present, the other is missing!
PAUSE
:TEST1
ECHO [Success!] - Found both files, YAY!
PAUSE
:TEST2
ECHO [Error!] - No matching files found.
PAUSE
The parentheses are neccessary for the else clause.
#echo off
echo checking file structure...
if exist "file1.exe" (
if exist "file2.zip" (
goto :ok
) else goto :download
) else goto :download
:download
echo downloading missing files.
:ok
echo Install successful
But the else isn't required at all because the program flow falls through to the download label
#echo off
echo checking file structure...
if exist "file1.exe" if exist "file2.zip" goto :ok
:download
echo downloading missing files.
:ok
echo Install successful
Why perenthesize? Simply try.
#echo off
echo checking file structure...
if exist "file1.exe" if exist "file2.zip" echo Install successful
if not exist "file1.exe" echo downloading missing "file1.exe"
if not exist "file2.zip" echo downloading missing "file2.zip"
or in a loop:
#echo off
for %%i in ("file1.exe" "file2.zip") do if not exist "%%i" echo %%i Must be downloaded & goto :EOF
echo Successful Installation
:again
for %%a in ("file1.exe" "file2.zip") do if not exist "%%~a" call :download "%%~a" &goto again
And then write an internal procedure to download the missing file supplied as %1
Possibly you'd want to install a counter and report in the :download procedure if the download is being executed, and if so of which file - and count the number of iterations to stop the process if the download fails too often.
An advantage here is that there's no real limit to the number of filenames that can be included in the parentheses
Found the solution! Added some if exist parameters to account for empty folders.
for /D %%I in ("%~dp0*") do (
cd /D %%I
if exist *pm.wav (
if exist *.xml (
tar -cvzf %%~nxI.tar.gz *pm.wav *.xml
)
)
)
cd..
Download & execute
#echo off
echo checking file structure...
if exist "C:\Users\Himel\Desktop\5MB.zip" (
if exist "C:\Users\Himel\Desktop\5MB.zip" (
goto ok
)
)
if not exist "C:\Users\Himel\Desktop\5MB.zip" (
if not exist "C:\Users\Himel\Desktop\5MB.zip" (
goto download
)
)
if not exist "C:\Users\Himel\Desktop\5MB.zip" (
goto download
)
)
if not exist "C:\Users\Himel\Desktop\5MB.zip" (
goto download
)
)
:download
echo downloading missing files.
SETLOCAL
rem FOR DOWNLOADING FILES, THIS SCRIPT IS USING THE "BITSADMIN.EXE" SYSTEM FILE.
rem IT IS PRESENT ON MOST WINDOWS VERSION, PROBABLY FROM WINDOWS XP TO WINDOWS 10.
:SETUP
rem URL (5MB TEST FILE):
SET "FILE_URL=http://ipv4.download.thinkbroadband.com/5MB.zip"
rem SAVE IN CUSTOM LOCATION:
rem SET "SAVING_TO=C:\Folder\5MB.zip"
rem SAVE IN THE CURRENT DIRECTORY
SET "SAVING_TO=5MB.zip"
SET "SAVING_TO=%~dp0%SAVING_TO%"
:MAIN
ECHO.
ECHO DOWNLOAD SCRIPT EXAMPLE
ECHO.
ECHO FILE URL: "%FILE_URL%"
ECHO SAVING TO: "%SAVING_TO%"
ECHO.
rem UNCOMENT AND MODIFY THE NEXT LINE IF YOU NEED TO USE A PROXY SERVER:
rem CALL :DOWNLOAD_PROXY_ON "PROXY-SERVER.COM:8080"
rem THE MAIN DOWNLOAD COMMAND:
CALL :DOWNLOAD_FILE "%FILE_URL%" "%SAVING_TO%"
rem UNCOMMENT NEXT LINE FOR DISABLING THE PROXY (IF YOU USED IT):
rem CALL :DOWNLOAD_PROXY_OFF
:RESULT
ECHO.
IF EXIST "%SAVING_TO%" ECHO YOUR FILE HAS BEEN SUCCESSFULLY DOWNLOADED.
IF NOT EXIST "%SAVING_TO%" ECHO ERROR, YOUR FILE COULDN'T BE DOWNLOADED.
ECHO.
:EXIT_SCRIPT
PAUSE
EXIT /B
rem FUNCTIONS SECTION
:DOWNLOAD_FILE
rem BITSADMIN COMMAND FOR DOWNLOADING FILES:
bitsadmin /transfer mydownloadjob /download /priority FOREGROUND %1 %2
GOTO :EOF
:DOWNLOAD_PROXY_ON
rem FUNCTION FOR USING A PROXY SERVER:
bitsadmin /setproxysettings mydownloadjob OVERRIDE %1 "<local>"
GOTO :EOF
:DOWNLOAD_PROXY_OFF
rem FUNCTION FOR STOP USING A PROXY SERVER:
bitsadmin /setproxysettings mydownloadjob NO_PROXY
GOTO :EOF
share
:ok
echo Install successful
This part of my script is supposed to go through directories looking for *.wim files and if it finds them on the "images" folder, proceed. However, it is going straight to the "else" part of the code :/
ECHO. Checking for WIMs
ECHO ............................................
ECHO.
for /F "tokens=*" %%f in ('dir /B /S "%w%:\data\images"') do (
if exist "%%~ff\*.wim" (
ECHO found me some wims! let's continue.
GOTO actionmenu
) else (
ECHO This script requires a wim repository folder to proceed. make sure you have
ECHO properly populated the "\Images" folder before re-running this script
goto end
)
)
You don't even need a for loop. Dir gives an %errorlevel% of 1, if it doesn't find something:
dir /B /S "%w%:\data\images\*.wim" >nul && (
echo found at least one wim file.
) || (
echo no wim files found.
)
&& works as "if previous command was successful (dir found at least one file), then"
|| is the opposite: " if previous command failed (dir didn't find a matching file) then"
for /F "tokens=*" %%f in ('dir /B /S /AD "%w%:\data\images"') do (
if exist "%%f\*.wim" (
ECHO found me some wims! let's continue.
GOTO actionmenu
) else (
ECHO This script requires a wim repository folder to proceed. make sure you have
ECHO properly populated the "\Images" folder before re-running this script
goto end
)
)
What I would like to do is search a directory for any subfolder containing .NA.
Example
Folder1
Folder2
Folder3.NA
As long as there is a folder with .NA do nothing. When there is no longer any folders with .NA delete a file in a different folder.
Each time this code runs, I never get to the :NotFound section to execute deleting the file.
Maybe someone has a different solution than what I'm thinking of.
Here is my Code - this will run at startup:
#echo off
setlocal enabledelayedexpansion
Set Source=C:\Temp\Users
FOR /d %%a in ("%Source%\*.NA") DO (
IF NOT !ErrorLevel! == 0 GOTO NotFound
IF !ErrorLevel! == 0 GOTO Found REM - There is still a user folder with .NA
)
Goto EOF
:Found
#echo There is a folder with .NA Do Nothing
Goto EOF
:NotFound
#echo Simulate delete start-up file
Goto EOF
:EOF
PAUSE
To put aschipfl's and my suggestions into code:
#echo off
setlocal enabledelayedexpansion
Set Source=C:\Temp\Users
FOR /d %%a in ("%Source%\*.NA") DO Goto :Found
::fall through with no find
#echo Simulate delete start-up file
Goto :End
:Found
#echo There is a folder with .NA Do Nothing
:End
PAUSE
If you you need an own exit point with a pause command don't call it eof, that is an internal automatic label.
Looking for ideas to help me with this.
I have a lot of music and most of it is in order of [artist folder] [album folder] [cd folder] [files] and I want to quickly be able to bring all music files under [artist] level.
I'm not expecting miracles so I'm thinking if I can set it up as a right click command much like WinRar does with Extract Here, that should bring everything from that folder up a level?
So I need to be able to
call my batch , and pass it the target folder.
Have the batch select all files/folders within and move to the
same level as target folder.
Trying to keep it simple even if I need to r/click several times for one artist!
Thanks in advance for any help, pointers or further reading!
Here is a script to move all files under a root directory to the root.
"Moveit.bat C:\my\music"
will find everything under that directory and move them into C:\my\music.
Line 27 is commented out ... so the script will only tell you what it would do.
Uncomment line 27 for it to actually work.
#echo off
#rem USAGE: MoveIt root
setlocal
pushd "%~1"
set root=%CD%
if "%1"=="" goto :Usage
for /f "delims=;" %%a in ('dir /a-d /b /s ') do call :MoveIt "%%a"
goto :EOF
:MoveIt
rem Exclude files directly under "from"
set target=%~d1%~p1
if "%target:~2%"==":\" (
if "%target%"=="%root%" (
goto :EOF
)
) else if "%target:~0,-1%"=="%root%" (
goto :EOF
)
echo move "%~1" "%root%"
rem move "%~1" "%root%" > nul || echo Failed to move "%~1" to "%root%" & goto :EOF
goto :EOF
:Usage
echo Usage: moveit.bat RootDirectory
goto :EOF