Batch delete files in variable - batch-file

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

Related

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

Keep 2 specific folders using RMDIR

As part of my job I need to remove files from computers that are being shipped over seas, this task is done several times a week and can be scripted, however I am terrible with script, I am in no means a coder of any sort.
My issue is that I need to clear out the C:\Users folder whilst keeping the Public and Default folders. I know how to remove a directory and all subfolders however I don't know how to keep just those two folders.
The code I have so far is:
#Echo off
color F
:Choice
Echo.
Echo.
set choice=
set /p choice="Type Y to proceed or N to close the tool and then press ENTER:----"
IF '%Choice%' =='Y' GOTO DELETE
IF '%Choice%' =='y' GOTO DELETE
IF '%Choice%' =='N' GOTO END
IF '%Choice%' =='n' GOTO END
:DELETE
rmdir /S /Q C:\***PATH***\***FOLDERNAME***
rmdir /S /Q C:\***PATH***\***FOLDERNAME***
rmdir /S /Q C:\***PATH***\***FOLDERNAME***
rmdir /S /Q C:\***PATH***\***FOLDERNAME***
Echo.
Echo.
#Echo All Files Removed
GOTO END
:END
echo.
echo.
Echo *****...Closing programme... Please wait...*****
Timeout /t 3
exit
Do you know if this is possible?
The following method, ran As Administrator, may work for you:
#Echo Off
Color 0F
Echo=&Echo=
Choice /N /M "Type Y to proceed or N to close the tool"
If ErrorLevel 2 Exit /B
For /F Tokens^=2^Delims^=^" %%A In ('WMIC Path Win32_UserProfile Where^
"Special!='True'" Assoc /AssocClass:Win32_UserAccount 2^>Nul'
) Do WMIC UserAccount Where "SID='%%A' And LocalAccount='TRUE'" Delete
Echo=&Echo=&Echo All Files Removed&Echo=&Echo=
Echo *****...Closing programme... Please wait...*****
Timeout 3 /NoBreak>Nul
Exit /B
Special!='True' will ignore special accounts, which include Administrator, Public and Default.

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

Looping Commands in Batch Scripts

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

How to write a batch file to check for specifed folders in a directory

How can I write a batch file to check for specified subfolders in a directory and then generate a log file showing the results of the findings (found it or not found). I am new to scripting and below is what I have so far:
#echo off
ECHO
SET /P QUESTION="Perform file check (Y/N)?"
if QUESTION == y goto :START_SCRIPT
:START_SCRIPT
if exist "C:\Folder\ABC" (echo found it) else echo not found;
if exist "C:\Folder\DEF" (echo found it) else echo not found;
if exist "C:\Folder\GHI" (echo found it) else echo not found;
test.bat >> out.txt
One more
#echo off
set "folder=c:\folder"
( for %%d in ("ABC" "DEF" "GHI") do if exist "%folder%\%%~d" (
echo "%%~d" found
) else (
echo "%%~d" not found
)
) >> file.log
Very simple;
#echo off
choice /m "Perform Check?"
if errorlevel 2 Exit
:loop
set /p query="Folder Name: "
if "%query%"=="" goto :end
if exist "C:\Folder\%query%" (Echo %query% Exists&Echo %query% Found >> out.txt
) Else (
Echo %query% Doesn't Exist&Echo %query% Not Found >> out.txt
)
goto :loop
:end
Echo End of Batch File&sleep 5
Exit
And that should do what you want (note it requires user input) which if is not the case, can use a log file redirected as input.
Mona
Here is how I'd do it:
#echo off
setlocal
cd /d %~dp0
call :FolderExists abc && Echo Yes || Echo No
exit /b
:FolderExists folder
for /f %%a in ('dir /b /ad') do (
if /i %%a EQU %1 exit /b 0)
exit /b 1

Resources