Why does my batch script become unresponsive attempting to delete inexistant files? - windows

The first part of my script:
#echo off
mode 34,18
color f0
del /s /q /f unsorted_frt.txt >nul 2>nul
del /s /q /f output_frt.txt >nul 2>nul
del /s /q /f frt.txt >nul 2>nul
del /s /q /f datas_futuros_rede_tan.txt >nul 2>nul
del /s /q /f datas_futuros_rede_tan.csv >nul 2>nul
del /s /q /f c:\datas_futuros_rede_tan.csv >nul 2>nul
(etc.)
Using Windows 7 it succeeds. But if the files to delete don't exist, the .bat file ignored this and continued anyway. I upgraded to Windows 10 and now, if the files to delete don't exist, the script fails entirely.
I need the script to continue despite this. How can I make that work on Windows 10? This is what it currently looks like:
It stops at this screen. If I delete this first part of the script, the rest runs normally.

Answer previously posted on question:
The problem is occurring in this line del /s /q /f c:\datas_futuros_rede_tan.csv >nul 2>nul So i changed it to del /s /q /f "c:\datas_futuros_rede_tan.csv" >nul 2>nul So, after more or less 2 minutes stopped in the same screen (like the image above), it starts to keep going.

It is usually a good idea to test whether or not the file exists before deleting it.
IF EXIST "unsorted_frt.txt" DO (DEL "unsorted.txt")
A FOR loop can help in deleting a large list of files. This list would presume that all files except the last one are in the current working directory. If the file names contain SPACE or other special characters, then they need to be quoted.
SET FILE_LIST=^
unsorted_frt.txt ^
output_frt.txt ^
frt.txt ^
datas_futuros_rede_tan.txt ^
datas_futuros_rede_tan.csv ^
c:\datas_futuros_rede_tan.csv
FOR %%f IN (%FILE_LIST%) DO (
IF EXIST "%%~f" DO (DEL "%%~f" >NUL 2>NUL)
)

Related

Delete Folders on Remote PC with Certain names

This is a sample code that allows me to delete all folders with the name ".RemoveAsap" attached to them
#echo on
set dir="\\TestPC2\c$\Users"
FOR /D /R %dir% %%X IN (*.RemoveAsap) DO RMDIR /S /Q "%%X"
pause
exit
Simply running the code as is runs perfectly but when I try to make the code more interactive, I get the error
#echo on
cd C:\Users\User1\Desktop\Test\
TYPE con >> LowDASD.txt
For /F %%A in (LowDASD.txt) do echo "\\%%A\c$\users\" >> LowDASD2.txt
set "LwDs"="LowDASD2.txt"
FOR /D /R "%LwDs%" %%X IN (*.RemoveAsap) DO RMDIR /S /Q "%%X"
pause
LowDASD2.txt would be the address/ directory location where the directories will be deleted, IE \\TestPC2\c$\Users
The code does not delete anything or give an error that "the path is too long" at least it was doing that with the previous variations that I was trying. If someone can help me with this, i would greatly appreciate it.
Try using FORFILES, instead of the command FOR, this way you can make it work like this:
:: forfiles /p "folder_location" 'args' '/c "cmd /c del /f /q #path"'
:: So...
cd C:\Users\User1\Desktop\Test\
TYPE con >> LowDASD.txt
For /F %%A in (LowDASD.txt) do echo "\\%%A\c$\users\" >> LowDASD2.txt
set "LwDs=LowDASD2.txt"
forfiles /p %LwDs% /s /c "cmd /c del /f /q #path"
:: You can use '/d -90' to delete files older than 90 days in the folder
FOR /D /R "%LwDs%" %%X IN (*.RemoveAsap) DO RMDIR /S /Q "%%X"
Simply will not work, as %LwDs% is a filename.
FOR /F "usebackq delims=" %%j in ("%LwDs%") do FOR /D /R "%%j" %%X IN (*.RemoveAsap) DO RMDIR /S /Q "%%X"
You would think might work - %%j being assigned to each entry in the %LwDs% file in turn; usebackq used because the filename is "quoted" (see for /? from the prompt for documentation)
But it doesn't - the for /d /r syntax doesn't accept metavariables...
So - try
FOR /F "usebackq delims=" %%j in ("%LwDs%") do set "target=%%j"&call :expunge
Where expunge is an internal subroutine. The colon is required
:expunge
echo target="%target%"
FOR /d /r "%target%" %%X IN (*.RemoveAsap) DO echo RMDIR /S /Q "%%X"
echo ====================
goto :eof
An internal subroutine should be placed after an unconditional goto, which should in your case follow the pause
pause
goto :eof
Where :eof (compulsory colon again) is defined as the physical end-of-file and should not be used as a user-label. Reaching physical end-of-file returns from a call.
Always verify against a test directory before applying to real data.
Note that the rmdir is merely being echoed for testing purposes - remove the echo keyword after testing to activate.
=== Extension
The full code should thus be
#echo on
set dir="\\TestPC2\c$\Users"
FOR /F "usebackq delims=" %%j in ("%LwDs%") do set "target=%%j"&call :expunge
pause
exit
:expunge
echo target="%target%"
FOR /d /r "%target%" %%X IN (*.RemoveAsap) DO echo RMDIR /S /Q "%%X"
echo ====================
goto :eof

Delete all files in directory except .bat

I want to delete a specific directory on Windows. I use the code below. It works fine. I want to put the .bat file I made for this process in that directory. Naturally, the .bat file is also deleted. I want the .bat file to be excluded from this deletion. What should I do with the code?
Echo batch file delete folder
#RD /S /Q "D:\testfolder"
All you have to do is to lock your batch file, by opening a dummy read handle to it.
echo The batch file wont be deleted because it is locked by a dummy input redirection.
rd /s /q "D:\testfolder" 9<"%~f0"
Naturally, an error message will be shown by the rd command, because there is at-least one file in the target directory (your own batch file) which can not be deleted. You can hide that message by redirecting the standard error stream to the nul device:
rd /s /q "D:\testfolder" 9<"%~f0" 2>nul
There are several ways to achieve your task.
The method, (especially as you generally remove directories with one command, and delete files with another), is to identify the files and subdirectories separately. First identify the subdirectories and remove those, using the RD command, then delete all files except for the batch file itself, %0:
You could do that in one line using the ForFiles utility:
#%SystemRoot%\System32\forfiles.exe /P "%~dp0." /C "%SystemRoot%\System32\cmd.exe /C If #IsDir==TRUE (RD /S /Q #File) Else If /I Not #file == \"%~nx0\" Del /A /F #File"
Or you could use a For loop, with the Dir command:
#For /F Delims^=^ EOL^= %%G In ('Dir /B /A "%~dp0"') Do #If "%%~aG" GEq "d" (RD /S /Q "%%G") Else If /I Not "%%G" == "%~nx0" Del /A /F "%%G"
Please note that you can only remove/delete items for which you have the required permissions.
I would walk through the items in the folder whose contents you want to delete, remove one after another, except its name equals the name of the batch file:
rem // Change into target directory:
pushd "%~dp0." && (
rem /* Loop through immediate children of the target directory, regarding even
rem hidden and system items; to ignore such (replace `/A` by `/A:-H-S`): */
for /F "delims= eol=|" %%I in ('dir /B /A "*"') do (
rem // Check name of current item against name of this batch script:
if /I not "%%~nxI"=="%~nx0" (
rem /* Assume the current item is a sub-directory first (remove by `rd`);
rem when removal fails, try to delete it as a file (done by `del`): */
rd /S /Q "%%I" 2> nul || del /A /F "%%I"
)
)
rem // Return from target directory:
popd
)

a batch file to delete a folder which is inside another folder

On windows 7, How can i write a batch file to delete a folder which is inside another folder.
this 'another folder' name varies.
Eg: C:\Users\%USERNAME%\AppData\Local\Mozilla\Firefox\Profiles\xxxxxx\cache2
the 'xxxxxx' might change. Now, i want to delete the 'cache2' folder and all its contents.
i tried this:
:: Batch script to clear browsing history, download history, and empty the cache for Mozila Firefox.
:: Script can be run via GFI MAX RM
#echo off
TASKKILL /T /F /IM Firefox.exe
set DataDir=C:\Users\%USERNAME%\AppData\Local\Mozilla\Firefox\Profiles
del /q /s /f "%DataDir%"
rd /s /q "%DataDir%"
for /d %%x in (C:\Users\%USERNAME%\AppData\Roaming\Mozilla\Firefox\Profiles\*) do del /q /s /f %%x\*sqlite
start "" "C:\Program Files (x86)\Mozilla Firefox\firefox.exe"
cls
IF %ERRORLEVEL%==0 (
#echo "Success Message"
timeout 10
) ELSE (
#echo "Error Message"
timeout 10
exit 1001
)
but this is deleting the entire profiles folder.
can anyone out there please help me out with this.
for /d %%a in (
"C:\Users\%USERNAME%\AppData\Local\Mozilla\Firefox\Profiles\*"
) do if exist "%%~fa\cache2\" echo rmdir /s /q "%%~fa\cache2"
For each folder under the Profiles folder, if it contains a cache2 subfolder, remove it.
rmdir commands are only echoed to console. If the output is correct, remove the echo command
simple:
rmdir /s C:\Users\%USERNAME%\AppData\Local\Mozilla\Firefox\Profiles\xxxxxx\cache2
If you know that your cache2 folder is a direct child of xxxxxx, then MC ND has a good answer. But if cache2 can appear at any level, then the following works, providing that cache2 is not a direct decendent of "...\Profiles\".
#echo off
for /f "delims=" %%F in ('dir /b /ad /s "C:\Users\%USERNAME%\AppData\Local\Mozilla\Firefox\Profiles\cache2"') do echo rd /s /q "%%F"
If cache2 can be a direct child of "...\Profiles", then the following should work as long as there are no folders with a name like "cache2.x".
#echo off
for /f "delims=" %%F in ('dir /b /ad /s "C:\Users\%USERNAME%\AppData\Local\Mozilla\Firefox\Profiles\cache2.?"') do echo rd /s /q "%%F"
Both commands above simply ECHO the RD statements. Simply remove ECHO when all looks correct.

Running many commands via batch, running into annoying issue

What I am trying to do is automate moving downloads, deleting unneeded files and then running a program through command line as well as who knows what else I think up of going through this quest. Here I have the beginning of my first .bat file:
move.bat:
#echo off
for /f "tokens=*" %%f in ('dir /a:-D /s /b') do move "%%f" .
for /f "tokens=*" %%f in ('dir /a:D /s /b') do rd "%%f"
del "C:\Downloads\*.jpg"
del "C:\Downloads\*.png"
del "C:\Downloads\*.gif"
del "C:\Downloads\*.txt"
del "C:\Downloads\*.nfo"
move "C:\Downloads\*.mp4" "C:\Movies\"
cd "C:\program files\filebot\"
filebot -rename c:\movies
copy "E:\Documents\Tweaks&commands\folderize.bat" "C:\Movies"
cd "C:\Movies\"
folderize.bat
ping 192.0.2.2 -n 1 -w 4000 > nul
rmdir /S /Q "C:\movies\folderize"
Now, everything works just fine up to running folderize.bat.
Folderize.bat:
#echo off
setlocal enabledelayedexpansion
for %%a in (*.eng*) do (
set file=%%a
ren "!file!" "!file:_=!"
)
#echo off
for %%a in (*.*) do (
md "%%~na" 2>nul
move "%%a" "%%~na"
)
Folderize.bat does exactly what it's supposed to do, but if I run that it will include itself, creating a folder named folderize and then moving itself into there. Ok, not a problem, I'll just do a rmdir and all is well.
That doesn't work.
In Move.bat I originally tried without the ping command line. I added that line as I thought it just needed a couple seconds before attempting delete (honestly just a stab in the dark). That was a no go as well.
I opened a cmd prompt and ran it manually to see what would happen. After folderize is initialized, it is then moved into it's own folder prompting that the batch file could not be found and terminates.
Is there a way that I can edit folderize.bat to exclude .bat files from being placed in a folder or really just exclude itself?
Seems like it's moving everything in your folderize.bat batch file. Maybe you should put a filter on it like so:
#echo off
setlocal enabledelayedexpansion
for %%a in (*.eng*) do (
set file=%%a
ren "!file!" "!file:_=!"
)
#echo off
for /f "tokens=1* delims=" %%a in ('dir /b /o:-n ^| findstr /iv "folderize.bat"') do (
md "%%~na" 2>nul
move "%%a" "%%~na"
)
Also, you use use the call command in your move.bat, like so:
call folderize.bat

Find and delete desktop.ini files in every folder on a drive using a batch script

I want to find and delete every desktop.ini and Recycle Bin.BIN file on a network drive, H:, using a windows batch file. I currently have this:
#echo About to delete all desktop.ini and Recycle Bin.BIN files from H:, press Ctrl+C to cancel or Enter to continue.
#pause
#H:
#for /f "usebackq" %%i in (`dir /s /b /x /A:H ^| find "desktop.ini"`) do del /A:H "%%i"
#for /f "usebackq" %%i in (`dir /s /b /x /A:RH ^| find "Recycle Bin.BIN"`) do del /A:RH "%%i"
#echo Deleting complete, press any key to exit.
#pause
Which works but for any file in a sub-folder with a space in the name it fails with a "cannot find file" error. Any suggestions how to fix that?
solution that worked for me was
create bat file "delete_all_desktop_ini.bat"
with
del /s /q /f /a ".\desktop.ini"
put it in a folder and run it
it will delete all desktop inis in current directory and sub directories of this file.
i put it in a project folder that is in google drive
Give this a test:
I've altered the recycle bin name to what I see here in Windows 8.
The name changes with different versions of Windows.
#echo off
del /s /q /f /a "h:\desktop.ini"
del /s /q /f /a "h:\$Recycle.Bin\*.*"
The problem occurs because by default space is a delimiter for the for command, but you can change this using the delims option. If you pick a character that won't ever appear in a file path then it should work fine:
#echo About to delete all desktop.ini and Recycle Bin.BIN files from H:, press Ctrl+C to cancel or Enter to continue.
#pause
#H:
#for /f "usebackq delims=|" %%i in (`dir /s /b /x /A:H ^| find "desktop.ini"`) do del /A:H "%%i"
#for /f "usebackq delims=|" %%i in (`dir /s /b /x /A:RH ^| find "Recycle Bin.BIN"`) do del /A:RH "%%i"
#echo Deleting complete, press any key to exit.
#pause
for /r "H:\" %%a in (desktop.ini $Recycle.Bin) do if exist "%%~fa" echo del /f "%%~fa"
Try it, to make it working remove echo from the script.
del /s /q /f /a ".\desktop.ini"
it should works as charm
save file .bat
put it in any folder
it will delete ini files in folders and sub folders

Resources