Batch program will not continue - batch-file

I have a batch script that deletes a folder but if the folder cannot be found, then the script stops.
I am using the follow command: rd Folder
Anything underneath that command is not executed if the folder doesn't exist. How can I fix this?
My script:
net share Users /delete
taskkill /F /IM status.exe
cd C:\Users\Normal\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Startup
del status.exe
cd C:\Users\Normal\AppData\Roaming
rd Others /s /q
REM ANYTHING BELOW WILL NOT CONTINUE:
start /b "" cmd /c del "%~f0"&exit /b
exit
pause > nul

Add a conditional statement to check if the folder does indeed exist before deleting. Doing so, your batch-file will not cease to run.
if exists c:\my_folder_to_delete\
REM Delete my folder now that I know it exists!!

replace the line rd Others /s /q with if exist Others (rd Others /s /q).
See http://ss64.com/nt/if.html for more complex conditional commands.

Related

Task Scheduler Having Issues Running CMD file that works when its run manually

I have a script to delete all files in my Epson Scans Folder. If I run the script manually it works perfectly. But for some reason if I set up a task in task scheduler it does not delete the files? Does anyone know why this would happen?
Note: pclist.txt is just a list of pc names
#echo off
for /F %%G in (pclist.txt) do (
pushd "\\%%G\C$\Epson Scans" || exit /B 1
for /D %%I in ("*") do (
rd /S /Q "%%~I"
)
del /Q "*"
popd
)
Are you sure that the file pclist.txt is located in 'probably' the \Windows\system32 directory?
When run as a Scheduled task the current directory will not necessarily be that used when run as a user.
The easiest way to achieve that is is to put that full path within those parentheses or to add the following line just beneath the #echo off.
IF "%CD%\" NEQ "%~dp0" PUSHD "%~dp0"

What does the code mean? (Deleting files and sub folders in a folder with a batch file)

del /s /q "C\:test\*.*"
for /d %%p in (C:\test\*.*) do rmdir "%%p" /s /q
This is a code I have which deletes files and sub folders in a folder. And it works, but I do not get what each of the command works. So my question is, what does the second line mean? Like, what is the %%p part, and what is the rmdir "%%p" part?
This is a batch file.
for /d is to iterate directories.%%p is a special kind of variable used in for loops - in this case it will change its value to each directory name in c:\tests . The part after do means execute rmdir (deletes directory) for each value of %%p. More info here
When in doubt, you should see the help of each command by typing YourCommand /? :
Del /?
RD /? or rmdir /?
For /?
I added an echo to see what happen when you execute your batch file :
#echo off
Echo This command to delete all files located on your "C\:test\" folder
Echo del /s /q "C\:test\*.*"
pause
echo this command is for looping thru your directory "C\:test\" to remove any subfolders on there
for /d %%p in (C:\test\*.*) do echo rmdir "%%p" /s /q
pause

Can't delete folder after using RoboCopy

I was trying to copy a file out of a folder and then afterward delete the folder. I'm using RoboCopy on windows 10.
Now I realize I could use the /MOVE switch but I ran into an error doing it the first way I was trying and I'd like to figure it out just so I have the knowledge of why it's happening.
I have a folder structure like so:
ParentFolder
---->SubFolder
-------->test.txt
---->test.bat
From within ParentFolder I run test.bat which contains the following script
echo off
setlocal
set dir="path\to\ParentFolder"
cd %dir%
robocopy %dir% %dir%\.. test.txt
if %ERRORLEVEL% LSS 8 (
rmdir /s /q "%dir%"
)
endlocal
The copy works fine, without the rmdir, the file copies and the old file is left within SubFolder. When I add the rmdir line, the copy still works fine, the old file is actually deleted, but then when it tries to remove the SubFolder directory I get the error that the process cannot access the file because it is being used by another process.
So the final structure looks like this instead of having SubFolder removed:
ParentFolder
---->SubFolder
---->test.bat
---->test.txt
Try this:
echo off
setlocal
set dir="path\to\ParentFolder"
cd %dir%
robocopy %dir% %dir%\.. test.txt
if %ERRORLEVEL% LSS 8 (
cd\
rmdir /s /q "%dir%"
)
endlocal
I think the problem is that you're trying to delete the folder while inside it.
See this example
cd c:\temp\test
rmdir /s /q c:\temp\test
cd c:\temp
rmdir /s /q c:\temp\test
As you can see the first rmdir failed but the second succeeded.

Deleting and copying files using batch file

Im trying to copy files from one drive to another using a batch file, Which works! but we keep Changing file names on our main file which creates addition copys with diffrent names everytime its run. I dont want to delete the Copy file entirely bacause of the length of time the copy takes just to copy. I would like to Compare the 2 files and delete the files that are no longer on the main drive here is the test that im working on. Thanks for any help you can give me.
#echo
cls
If not exist "C:\Users\Jeremy\Desktop\Test Main\*.*" "Del "C:\Users\Jeremy\Desktop\Test Clone\*.*"
xcopy "C:\Users\Jeremy\Desktop\Test Main\*.*" "C:\Users\Jeremy\Desktop\Test Clone\*.*" /D /C /E /S /I /Y /V /H /R /F /d:01-01-1998
pause
:abort
echo You pressed CTRL+C to end the copy operation.
goto exit
you might want to look into robocopy, specifically with the /mir switch, which mirrors (copy all new files and delete all no longer existing files) the source folder to the target.
Thanks
This does work
#echo
cls
robocopy /MIR "C:\Users\Jeremy\Desktop\Test Main" "C:\Users\Jeremy\Desktop\Test Clone"
pause
:abort
echo You pressed CTRL+C to end the copy operation.
goto exit
But I would still like to understand if anyone can or wants to take the time to correct my original question
Try:
#echo off
cls
If not exist "C:\Users\Jeremy\Desktop\Test Main\*.*" "Del "C:\Users\Jeremy\Desktop\Test Clone\*.*"
xcopy "C:\Users\Jeremy\Desktop\Test Main\*.*" "C:\Users\Jeremy\Desktop\Test Clone\*.*" /D /C /E /S /I /Y /V /H /R /F /d:01-01-1998
If %errorlevel% EQU 2 (
echo You pressed CTRL+C to end the copy operation.
)
pause

Delete all files of specific type (extension) recursively down a directory using a batch file

I need to delete all .jpg and .txt files (for example) in dir1 and dir2.
What I tried was:
#echo off
FOR %%p IN (C:\testFolder D:\testFolder) DO FOR %%t IN (*.jpg *.txt) DO del /s %%p\%%t
In some directories it worked; in others it didn't.
For example, this didn't do anything:
#echo off
FOR %%p IN (C:\Users\vexe\Pictures\sample) DO FOR %%t IN (*.jpg) DO del /s %%p\%%t
What I'm I missing in the second snippet? Why didn't it work?
You can use wildcards with the del command, and /S to do it recursively.
del /S *.jpg
Addendum
#BmyGuest asked why a downvoted answer (del /s c:\*.blaawbg) was any different than my answer.
There's a huge difference between running del /S *.jpg and del /S C:\*.jpg. The first command is executed from the current location, whereas the second is executed on the whole drive.
In the scenario where you delete jpg files using the second command, some applications might stop working, and you'll end up losing all your family pictures. This is utterly annoying, but your computer will still be able to run.
However, if you are working on some project, and want to delete all your dll files in myProject\dll, and run the following batch file:
#echo off
REM This short script will only remove dlls from my project... or will it?
cd \myProject\dll
del /S /Q C:\*.dll
Then you end up removing all dll files form your C:\ drive. All of your applications stop working, your computer becomes useless, and at the next reboot you are teleported in the fourth dimension where you will be stuck for eternity.
The lesson here is not to run such command directly at the root of a drive (or in any other location that might be dangerous, such as %windir%) if you can avoid it. Always run them as locally as possible.
Addendum 2
The wildcard method will try to match all file names, in their 8.3 format, and their "long name" format. For example, *.dll will match project.dll and project.dllold, which can be surprising. See this answer on SU for more detailed information.
You can use this to delete ALL Files Inside a Folder and Subfolders:
DEL "C:\Folder\*.*" /S /Q
Or use this to Delete Certain File Types Only:
DEL "C:\Folder\*.mp4" /S /Q
DEL "C:\Folder\*.dat" /S /Q
I wrote a batch script a while ago that allows you to pick a file extension to delete. The script will look in the folder it is in and all subfolders for any file with that extension and delete it.
#ECHO OFF
CLS
SET found=0
ECHO Enter the file extension you want to delete...
SET /p ext="> "
IF EXIST *.%ext% ( rem Check if there are any in the current folder :)
DEL *.%ext%
SET found=1
)
FOR /D /R %%G IN ("*") DO ( rem Iterate through all subfolders
IF EXIST %%G CD %%G
IF EXIST *.%ext% (
DEL *.%ext%
SET found=1
)
)
IF %found%==1 (
ECHO.
ECHO Deleted all .%ext% files.
ECHO.
) ELSE (
ECHO.
ECHO There were no .%ext% files.
ECHO Nothing has been deleted.
ECHO.
)
PAUSE
EXIT
Hope this comes in useful to anyone who wants it :)
I don't have enough reputation to add comment, so I posted this as an answer.
But for original issue with this command:
#echo off
FOR %%p IN (C:\Users\vexe\Pictures\sample) DO FOR %%t IN (*.jpg) DO del /s %%p\%%t
The first For is lacking recursive syntax, it should be:
#echo off
FOR /R %%p IN (C:\Users\vexe\Pictures\sample) DO FOR %%t IN (*.jpg) DO del /s %%p\%%t
You can just do:
FOR %%p IN (C:\Users\0300092544\Downloads\Ces_Sce_600) DO #ECHO %%p
to show the actual output.
this is it:
#echo off
:: del_ext
call :del_ext "*.txt"
call :del_ext "*.png"
call :del_ext "*.jpg"
:: funcion del_ext
#echo off
pause
goto:eof
:del_ext
set del_ext=%1
del /f /q "folder_path\%del_ext%"
goto:eof
pd: replace folder_path with your folder
Step 1:
Navigate to the folder in question using the cd command
For example:
cd C:\Users\tremanleo\Desktop\HoldLEOCMS
Step 2
Delete the the file type.
For Example:
DEL *.bak
If you are trying to delete certain .extensions in the C: drive use this cmd:
del /s c:\*.blaawbg
I had a customer that got a encryption virus and i needed to find all junk files and delete them.

Resources