I need help converting the following Linux/Mac commands into a Windows batch script.
find / -regex ``^.*/Excel_[a-z]*.xls'' -delete 2>/dev/null
find / -regex ``^.*/presentation[0-9]*[a-z]*.ppt'' -delete 2>/dev/null
Basically using regular expressions, I need to find any of the .xls/.ppt files (in the format above) in a given Windows box and delete them.
Sorry, I'm new to Windows batch files.
You really don't explain what your hieroglypics mean.
In a batch file,
@echo off
setlocal
dir /s "c:\local root\Excel_*.xls"
would show all of the files matching starting Excel_
with extension .xls
(and if that's what you want, simply replacing dir
with del
would delete them; adding >nul
would suppress messages; adding 2>nul
suppresses error messages)
If you want files starting Excel_
then followed by any alphas-only, then
for /f "delims=" %%a in ('dir /b /s /a-d Excel_*.xls ^| findstr /E /R "\\Excel_[a-z]*\.xls" ') do echo "%%a"
The dir
produces a directory list in /b
(basic) form (ie. filename-only) /s
- with subdirectories (which attaches the full directory name) and the /ad
suppresses directorynames. This is piped to findstr
to filter out the required filenames. The result is assigned to %%a
line-by-line, and the delims=
means "don't tokenise the data"
should show you all the files matching that criterion - but it would be case-sensitive; add /i
to the findstr
switches to make it case-insensitive. (/r means "regex" within findstr
's restricted implementation; /e means "ends" - I tend to use these over $
) The \\\\
in intended to implement escaped \\
to ensure the filename match is complete (ie do't find "some_prefixExcel_whatever.xls) - I'll leave what \\.
is intended to do to your imagination...
(again, change echo
to del
to delete and add in the redirection palaver if required)
And the other regex - well, follow the bouncing ball. It would appear you want .ppt
files with names starting presentation
followed by a possible series of numerics then by a series of alphabetics. I'd suggest
findstr /e /r "\\presentation[0-9]*[a-z]*\.ppt" for that task.
Plain Batch can't do this task. But you could make use of tools like findstr which come with Windows and support Regex.
This line can be executed from CMD and deletes all files in the current folder which match the RegEx:
for /f "eol=: delims=" %F in ('findstr /r "MY_REGEX_HERE"') do del "%F"
So try to get your expected results by playing around with this command. If your fine with the output/results, you can embed this line in your batch script. (Be careful, when embedding this line in batchscript you have to double the percentage signs!)
for /f "eol=: delims=" %%F in ('findstr /r "MY_REGEX_HERE"') do del "%%F"
Use PowerShell.
get-childitem | where-object { $_.Name -match '<put a regex here>' } | remove-item
get-childitem
returns file system objects, and the where-object
filter selects only those file system objects whose name property matches a regular expression. These filtered items are then passed through the pipeline to remove-item
.
There is good information about the PowerShell pipeline in the about_pipelines
help topic, which you can read using the following command:
help about_pipelines
Here a small batch to do what you are looking for:
@echo off
set /p dirpath=Where are your files ?
:: set dirpath=%~dp0 :: if you want to use the directory where the batch file is
set /p pattern=Which pattern do you wanna search (use regex: *.xml e.g.) :
:: combinason /s /b for fullpath+filename, /b for filename
for /f %%A in ('dir /s /b "%dirpath%\%pattern%" ^| find /v /c ""') do set cnt=%%A
echo File count = %cnt%
call :MsgBox "Do you want to delete all %pattern% in %dirpath%? %cnt% files found" "VBYesNo+VBQuestion" "Click yes to delete the %pattern%"
if errorlevel 7 (
echo NO - quit the batch file
) else if errorlevel 6 (
echo YES - delete the files
:: set you regex, %%i in batch file, % in cmd
for /f "delims=" %%a in ('dir /s /b "%dirpath%\%pattern%"') do del "%%a"
)
:: avoid little window to popup
exit /b
:: VBS code for the yesNo popup
:MsgBox prompt type title
setlocal enableextensions
set "tempFile=%temp%\%~nx0.%random%%random%%random%vbs.tmp"
>"%tempFile%" echo(WScript.Quit msgBox("%~1",%~2,"%~3") & cscript //nologo //e:vbscript "%tempFile%"
set "exitCode=%errorlevel%" & del "%tempFile%" >nul 2>nul
endlocal & exit /b %exitCode%
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.