Windows Batch folder exist and folder not exist - windows

Have a need where we need to delete zip files that exist under a specific folder at 100 days old. All other zip files in the other folders, we can delete that are 30 days old. This would be easy if the files were a specific name, unfortunately we can only do this by folder name.
In the end result we will be using one of these techniques with FORFILES as we don't have to do a lot of logic programming in script.
Searched and did find how to do this and works OK, but the processing of the script is very slow.. Pretty much at this point, everything in the "NOT" somefolder condition is what takes quite a while to complete. Have done this with VBScript and PowerShell, but really want to get back to Batch Scripting for this.
BAT script to search directory for folders that match an input name
and as dbenham admits, again this is very slow. Also, did not like the fact that it does not show all folders in the "not" condition for the ones it does not find
He also states, that if you want to do extensive file/folder searching redirecting to a output file, maybe the best solution
This does work, but does not show the "not found" or the folders it would list..
dir /s /a-d c:\windows\*system32* >nul && (echo found it) || (echo not found)
This works to find all folders with system32 on a drive
dir /b /ad /s "c:\system32"
This works to look for folders that DO HAVE system32 within the results which are pretty quick too
FOR /f "tokens=*" %%G IN ('dir /b /ad /s c:\ ^| findstr /I /C:"system32"') DO echo %%G
We want to show all folders that DO NOT HAVE system32 within and in testing and redirecting to result.txt file, it created a 11.9MB file and will take quite a while to complete especially on a whole drive
FOR /f "tokens=*" %%G IN ('dir /b /ad /s c:\ ^| findstr /I /V /C:"system32"') DO echo %%G
How do I search for the directory of a file given its name using a batch script?
Not quite sure about this one
for /r c:\ %%F in (system32) if exist "%%F" echo %%~dpF
Playing around we get
For /F %%A IN('dir /s /a-d "c:\windows\*system32*") do && (echo found it) || (echo not found)
Are there any other ideas or Suggestions out there?

This will search in c:\files and below, and delete zip files by the following criteria:
In the folder called Special folder it will delete them if they are 100 days or older
otherwise it will delete them if they are 30 days or older.
Any zip file in the folders below the Special folder will be deleted at 30 days and older.
Test it before use.
#echo off
for /r "c:\files" %%a in (*.zip) do (
for %%b in ("%%~dpa\.") do (
if /i "%%~nxb"=="Special folder" (
forfiles /p "%%~dpa." /m "%%~nxa" /d -100 /c "cmd /c del #path"
) else (
forfiles /p "%%~dpa." /m "%%~nxa" /d -30 /c "cmd /c del #path"
)
)
)
pause

Related

Windows Bat - The System Cannot Find the File Specified

I am using the following code in a .bat to cleanup a directory. It is to delete any directory with a time stamp older than 14 days. The thing is, this script works and deletes the appropriate directories. However it returns the error:
ERROR: The system cannot find the file specified I am unable to decipher the cause of this, and would like to get to the bottom of it.
FORFILES /S /D -14 /p %cd% /M "*" /C "cmd /c IF #isdir == TRUE rmdir #path /s /q"
As to the follow up question you asked:
Using a Windows batch file, find directories that do not contain any letters in their name. They can contain special characters and spaces. Delete the directories and their sub-folders w/o confirmation.
Put 1.bat in the directory you want to cleanup. Open a cmd window and run 1.bat.
Find all directories that do not contain any letters in their name and output their names to 1.txt. They can contain special characters and spaces.
Echo the directories to be removed. Do not remove them.
Remove comment tag to remove directories and sub-directories w/o confirmation.
1.bat
for /f "usebackq delims=|" %%a in ('DIR /b /ad ^| findstr /v /r "[a-Z]"') do echo "%cd%\%%a" will be removed without confirmation.
:: for /f "usebackq delims=|" %%a in ('DIR /b /ad ^| findstr /v /r "[a-Z]"') do rd /s /q "%cd%\%%a"

Batch file that deletes all but the last 10 per file name

I have multiple folders with multiple files with the same name and then a timestamp. Example:
foo_2017.01.16.png
foo_2017.01.15.png
foo_2017.01.14.png
bar_2017.01.16.png
bar_2017.01.15.png
bar_2017.01.14.png
file_2017.01.16.png
file_2017.01.15.png
file_2017.01.14.png
What I'm needing to do is create a batch file that will parse out only the name before the _timestamp and keep the last 10 of each file name. I'm not very well versed in batch and could really use some help here. The Date Modified for every file is the same, so I cannot just say keep only the last 10 days, otherwise I could handle that.
EDIT:
I have a batch file that deletes files in a specific folder older than 28 days that looks like the following, I just do not know how to modify it to iterate through for each file name specifically and to not go by Date Modified:
/c echo #FILE
forfiles -p "C:\workspace\SeleniumTestResults" -s -m *.* /D -28 /C "cmd /c del #path"
Again, this batch is definitely not my area
#echo off
setlocal
REM create some files for testing:
break>foo_2017.01.16.png
break>foo_2017.01.15.png
break>foo_2017.01.14.png
break>bar_2017.01.16.png
break>bar_2017.01.15.png
break>bar_2017.01.14.png
break>file_2017.01.16.png
break>file_2017.01.15.png
break>file_2017.01.14.png
dir *.png
set keep=2
for /f "delims=_" %%a in ('dir /b *.png') do (
for /f "skip=%keep% delims=" %%b in ('dir /b /o-n "%%~a*.png"') do del "%%b"
)
dir *.png

Windows batch script for searching files based on file name,checks if the files are older than N days, if true, it deletes those files

I am a newbie to windows batch scripting. I have researched through the web and the site and tried out the solutions but none seem to give me the desired results.
This is what I want to achieve:
Search for files in a folder using a specific filename
show found files
Check the found files if they are older than 1 day
If true,delete those files
Else return message(Found files not older than 1 day)
From my research I was able to write a batch code that searches for file using a string, but unable to do step 2,3,4 and 5.
Kindly assist.
Here is my batch code:
#echo off & setlocal
set "MySearchString=Scheduled_error"
for /r %%a in (*) do for /f "delims=" %%i in ('echo("%%~na" ^| findstr /i "%MySearchString%"') do echo del "%%~fa"
Seems like a perfect task for FORFILES!
forfiles /p c:\SomePath\ /s /m *.* /c "cmd /c echo #path" will show you all files older than one day. You can modify the filter replacing *.* with the file name you are looking for.
To delete these files simply replace echo #path with del /y #path and add the /d -1 parameter:
forfiles /p c:\SomePath /s /m *.* /d -1 /c "cmd /c del /y #path"
The age of the files to delete is specified with the /d -1 switch where -1 means 1 day and older.

Batch file to recursively delete files in a folder older than N number of days

I'm using a batch file now to delete all files ending in .snp that are older than 180 days. The code below works to delete all files ending in .snp under the root folder
C:\Program Files\Snapshots
But I recently discovered that within the Snapshots folder there are folders organized by date
"1-10-2014, 12-20-2014, 10-15-2014 etc.."
and that the below line of code doesn't work to recursively search through each directory and is therefore not deleting.
What changes should I make to this code to have it recursively search through folders within a root folder and delete files that are greater than 180 days?
forfiles /M *.snp /P "C:\Program Files\Snapshots" /S /D -180 /C "cmd /c del /F /Q #path"
Without the /D (Date) it workes for sub-folders
forfiles /M *.txt /P "C:\hlpme" /S /C "cmd /c del /f /q #path
but you obviously want the date to be there
then in CMD
forfiles /D -180 /M *.txt /P "C:\hlpme" /S /C "cmd /c del /f /q #path
The /D before the Pathname selects all files that have been changed more than 180 days ago
The best option for highest reliability is to combine the strengths of the For command with the FORFILES commands to allow each one to do what they do best.
Set str_Ext=*.snp
Set int_Age=-180
For /R "%~dp0" %%D IN (.) DO (
For /F "usebackq tokens=*" %%F IN (`FORFILES /P "%%~D" /m %str_Ext% /D %int_Age% 2^>nul`) DO (
Call :s_Del_File "%%~D" "%%~F"
)
)
Goto :EOF
:s_Del_File
Set "str_DIR=%~1"
Set "str_FIL=%~2"
Set "str_DIR=%str_DIR:~0,-1%"
DEL /F/Q/A "%str_DIR%%str_FIL%"
Goto :EOF
Within the second FOR command, the backquote (~ key) contains the FORFILES command and uses the console output to call a batch subroutine to delete the specified file.
Spaces in folder and file names will not slow this beast down, and the double quotes ["] around the Set commands will allow the process to work with folders and files that have parentheses in them or other exotic, but allowable characters.

Windows batch - match folders with a dot (.) in the folder name

I'm trying to figure out how to match folders with a dot in the file name (e.g., ".svn") in a Windows batch script.
Here's the basic script:
setlocal
pushd c:\myDir
#echo off
FOR /D /r %%G in ("*\.whatever") DO (
echo %%G
REM do stuff
)
#echo on
popd
endlocal
This works just fine for most folder names (e.g., "*bin"), but I can't figure out the method to specify a folder with the dot. "*.whatever" and "*\.whatever" return no results. I'm guessing I'm missing some escape character or something equally simple, but I haven't been able to find any documentation on it.
(Before anyone asks, no I'm not trying to recursively delete subversion folders; "*.svn" is just an example.)
Maybe I am missing something, but as you say it seems simple
for /r /d %%a in (.*) do echo %%~fa
But if the folders are hidden, the normal for will not be able to see them, so we need to execute a dir command an process its output with a for /f
for /f "delims=" %%a in ('dir /ad /s /b .*') do echo %%~fa

Resources