Looping Commands in Batch Scripts - batch-file

Suppose I have the command:
:start
if /i "%%~fs"=="%drive%" cd %%~fs&md Favorites 2>nul
if /i "%%~fs"=="%drive%" cd %%~fs&md "Discography Info" 2>nul
...
goto start
How would I loop the command and each time put in a different name?

You can do it similar to this:
#echo off
for %%f in (*) do call :printit "%%f"
goto :end
:printit arg1
echo %~1 ---- %1
exit /b 0
:end
echo Done.
pause

Related

CMD Create Custom Commands with more than one option

I created a custom CMD Command with a Batch Script but I dont know how to make more than 1 Option...
What I have now is a Command which looks like this:
yce -update
But I want to make a Command where you can specify what is Updated which works like this:
yce -update (all, appname, eb)
This is the entire code:
#echo off
SET "parameter=%~1"
if /i "%parameter%" equ "" goto :yce_help
for %%p in (help h update fiddler ) do (
if /i "%parameter:~1%" equ "%%~p" goto :%%~p
rem if /i "%parameter%" equ "%%~p" goto :%%~p
)
goto :wrong_parameter
:wrong_parameter
echo yce: option -%parameter:~1%: is unknown
echo yce: try 'yce -help' for more information
goto :eof
:yce_help
echo yce: try 'yce -help' for more information
goto :eof
:h
goto :help
:help
echo.
echo.
echo.
echo Usage: yce [options...]
echo -update, Updates yCE
goto :eof
:update
echo [94mCreating Folders...[0m
md "%userprofile%\.yce"
md "%userprofile%\.yce\update"
echo [94mDownloading Latest Update...[0m
echo THIS SCRIPT DOWNLOADS THE UPDATE
echo [94mStarting Update...[0m
start %userprofile%\.yce\update\yce_update.bat
goto :eof

Log contents of text file before deleting

My script isn't logging the contents of run.txt to log.txt
I've tried to remove the delete command to see if it was deleting it too quickly and therefore couldn't log. But that wasn't the case.
What should I change?
#ECHO OFF &setlocal
SET File=run.txt
type %File%
for /f "tokens=*" %%A in (%File%) do (echo >> log.txt)
del "%File%" /s /f /q > nul
pause
Here is a very simple way to do the task you are requiring.
#echo off
REM Will only grab the first line of the file
set /p file=<run.txt
REM For the last line use a for loop
for /f "tokens=*" %%a in (%file%) do set last_line=%%a
(
echo %file%
)>>log.txt
del /f %file% >nul
If not %errorlevel% equ 0 (
ECHO ERROR - %errorlevel%
pause
exit /b
)
ECHO Success!
timeout /t 003
exit /b %errorlevel%
EXPLANATION
set /p is for set prompt. For more information you can use set /? in your CMD window or check out this site.
I wish I could speak more on what < does, but what it is doing here is piping the content of run.txt to our variable.
Then we echo out our variable to our log file with (ECHO This is our %file%)>>destination
>> is to append where > is to overwrite the file.
(
echo %file%
echo.
)>>%file%
Checking for an error is probably unnecessary, but I believe it is a good habit to build on which is what I am trying to do with that If not %errorlevel% statement.
No error? We Success and timeout ourselves for xxx seconds.
Try:
#ECHO OFF &setlocal
SET "File=run.txt"
type "%File%" >> "log.txt" && (del "%File%" /f > nul)
pause

Batch delete files in variable

I have a batch file which searches through a directory tree deleting generated file backups.
I want to edit the script to run the del command against the files found in the search, but I can't get it to work.
I've searched other threads and set it up similarly but without the expected results.
#echo off
pushd FILEPATH
echo Searching directories...
for /f "delims=" %%G in ('dir /b /s *.0**.rfa') do echo "%%G"
echo.
IF /I "%%G" == "" GOTO NOTFOUND
set /P delete="Delete files? [Y/N]: "
IF /I "%delete%" NEQ "Y" GOTO ENDOF
echo Deleting files...
echo.
del "%%G"
echo.
echo Done!
timeout 5
exit
:ENDOF
echo Aborted.
timeout 5
exit
:NOTFOUND
echo Found nothing.
timeout 5
exit
Result:
Deleting files...
Could Not Find FILEPATH\ %G
Done!
Do you really need the for loop? The following should work exactly as you want:
#echo off
dir /b /s "*.0*.rfa" || echo Found nothing. & goto :finish
choice /m "delete all? "
if errorlevel 2 echo Aborted. & goto :finish
echo Deleting files...
del /q /s "*.0*.rfa"
echo Done.
:finish
timeout 5
exit /b

Batch File looping until certain text is displayed

Currently have a batch file for replacing NTFS permissions using the takeown and icacls commands, i have added these commands to a loop and it works great.
Is there a way to exit the loop when a certain response is displayed? like "Failed processing 0 files" or something like that? the code i am using is below, hopefully this will help some other people also.
#echo off
setlocal enabledelayedexpansion
for /l %%x in (1,1,1000) do (
echo Taking ownsership of Folders & Files - loop %%x
for /f "delims=" %%i in ('takeown.exe /R /A /F "F:\Shares\NetBackup Clients" /D N ^| findstr /i /C:"Failed processing 0 files"') do (
set "error=%%i"
if "!errorlevel!"=="0" goto :end
)
echo Applying permissions to filestore - loop %%x
icacls.exe "F:\Shares\NetBackup Clients" /grant "Domain\Group":F /grant "Domain\Group":R /T /C
echo Finished applying permissions to filestore - loop %%x >> C:\Loopy.txt
)
goto :eof
:end
echo %error%
Many Thanks
I think you might have the error the wrong way around, so you would need to adjust it accordingly, but we use findstr and if we meet the requirement (errorlevel is 0) we exit the loop.
#echo off
setlocal enabledelayedexpansion
for /l %%x in (1,1,1000) do (
echo %%x
for /f "delims=" %%i in ('takeown.exe /R /A /F "\\fileserver\share\" /D N ^| findstr /i "Failed processing 0 files"') do (
set "error=%%i"
if "!errorlevel!"=="0" goto :end
)
echo Finished takeown >> C:\Loopy.txt
icacls.exe "\\fileserver\share\" /grant "Domain\Group":F /grant "Domain\Group":R /T /C
echo Finished icacls >> C:\Loopy.txt
echo Loop %%x >> C:\Loopy.txt
)
goto :eof
:end
echo %error%

How to do while loop in windows batch

I wrote a windows batch script to check and move files to another directory based on the list I put in a notepad file named list.txt. But I have no idea that how to set the while-loop. Only to jump out of the subroute when the condition fulfill.
In C Programming, we could write like this by setting a while-loop direcly. But seems in windows batch is quite different.
All I want is like this:
Directory A:
1. A.txt
2. B.txt
3. list.txt (By line sequential with filename want to move)
4. process.bat
Directory B:
None of files (Then move a file by order of line set in list.txt) OR
A.txt (If already existed a text file in directory, process.bat will pause before A.txt disappear)
Process.bat
#echo off
:readline
for /f "tokens=*" %%a in (list.txt) do call :processline %%a
goto :eof
:processline
if exist D:\DirectoryA\*.txt (
echo %time% >> D:\AutoLog\Log.txt
echo Previous job did not finished yet. >> D:\AutoLog\Log.txt
Timeout /t 30
echo.
)
set name=%*
if exist %name%.txt (
echo %time% >> D:\AutoLog\Log.txt
echo File found and processing %name%.txt now... >> D:\AutoLog\Log.txt
copy "%~dp0\%name%.txt" "D:\DirectoryB"
echo Transfer %name%.txt completed!! >> D:\AutoLog\Log.txt
echo. >> D:\AutoLog\Log.txt
Timeout /t 790
echo.
echo ==============================================================
)
:eof
Please guide me to finish the script by using a while-loop method. Thanks
I change some script sequence and it works now.
#echo off
:readline
for /f "tokens=*" %%a in (list.txt) do call :processline %%a
goto :eof
:processline
set name=%*
if exist C:\Test2\*.txt (
echo %date% %time% >> C:\Test2\Log.txt
echo Previous job did not finished yet. >> C:\Test2\Log.txt
Timeout /t 5
echo.
echo. >> C:\Test2\Log.txt
goto :processline
)
if exist %name%.txt (
echo %date% %time% >> C:\Test2\Log.txt
echo File found and processing %name%.txt now... >> C:\Test2\Log.txt
copy "%~dp0\%name%.txt" "C:\Test2"
echo Transfer %name%.txt completed!! >> C:\Test2\Log.txt
echo. >> C:\Test2\Log.txt
Timeout /t 10
echo.
echo ==============================================================
)
:eof
This will copy as well as count the number of lines from your text file..
# echo off
:TextPath
cls
set /p Input=#1 Enter the full path of the text file :
set /p Source=#2 Enter the full path of Source :
set /p Target=#3 Enter the full path of Destination :
:choice
set /P c=Ready to Continue[Y/N]?
if /I "%c%" EQU "Y" goto :Yes
if /I "%c%" EQU "N" goto :No
goto :choice
:Yes_Local
for /f "delims=" %%i in (%Input%) do echo f| xcopy /f /y "%Source%\%%i" "%Target%\%%i"
for /f %%C in ('Find /V /C "" ^< %Input%') do set Count=%%C
msg * No of Lines executed= %Count%
exit
:No
cls
color e
echo Redirecting to Main....
PING 127.0.0.1 -n 2 >NUL
cls
echo Please wait
PING 127.0.0.1 -n 4 >NUL
goto TextPath

Resources