batch FOR command - string together output of 2 dos commands - windows

Trying to get this to work -
FOR /f "tokens=*" %%i in ('dir /s /b *.rar ^| findstr /i ^"par.*1^"' 'dir /s /b *.rar ^| findstr /v /i ^"part^"') DO (
winrar x "%%~i" "v:\unpack\"
)
It works fine with only the first 'dir /s /b *.rar', but I can't seem to chain them together. I know there are other syntaxes I can use, like *.rar and *.zip only but I have to search the dir output as shown. I tried adding a comma between the two dir commands but still makes no difference.
So...is this possible?

Ok, got it working. Here is the cmd line -
'dir /s /b *.rar ^| findstr /i ^"par.*1^" ^& dir /s /b *.rar ^| findstr /v /i ^"part^"'

Related

Windows command to delete all files except the specified list of files which may contain spaces in their names

I have a folder contains a list of files. I am using following command to delete all the files except the required files. If there is a file with spaces in the name then following command is failing. Say "File Name with space.txt" or "File 1.txt"
for /f %F in ('dir /b /a-d ^| findstr /vile "file1 file2 file3"') do del "%F"
I tried putting the file names in "" but no success.
You have two options with the FINDSTR command to accomplish this.
The first is to list each file individually with the /C option.
for /f "delims=" %F in ('dir /b /a-d ^| findstr /V /I /L /E /C:"file1" /C:"file2" /C:"file3"') do del "%F"
The other option is put all your search strings in a file, one on each line and use the /G option.
for /f "delims=" %F in ('dir /b /a-d ^| findstr /V /I /L /E /G:"search.txt"') do del "%F"

How to delete empty folders with some exceptional folders in shell script?

For example:
I have folder keep1, sub folder keep2 and sub folder of folder keep2 : keep3:
Keep1(not empty)
Keep2(not empty)
Keep3(empty)
Currently I'm using:
for /f "delims=" %%i in ('dir /s /b /ad ^| sort /r') do rd "%%i"
which deletes all empty folders but I don't want to delete some folders.In above example, I want to keep folder Keep3which is empty.
Could you not do use find?
for /f "delims=" %%i in ('dir /s /b /ad ^| sort /r ^| find /V "Keep3"') do rd "%%i"
?
EDIT
I've seen you might have multiple places you need to skip, so you could use http://www.computerhope.com/findstr.htm findstr
ie if you have to ignore Keep3 and Keep4 then its
for /f "delims=" %%i in ('dir /s /b /ad ^| sort /r ^| findstr /V "Keep3 Keep4"') do rd "%%i"
It also as (basic) regex capabilities

How can I Delete Files That Do Not Contain Certain Characters Using Windows Batch File?

How can I delete files that do not end with the pattern .PDF_*.pdf ?
AA00A6E2.PDF
AA00A6E3.PDF
AA00A6E3.PDF_01.pdf
AA00A6E3.PDF_02.pdf
AA00A6E3.PDF_03.pdf
I have been trying all sorts of variations around the following syntax:
FOR %%F IN (%varFolderSource%\*.*) DO IF NOT "%%~xF" == "*_*" DEL /F /S "%%F"
But I can't seem to crack it.
%varFolderSource% is a folder path: C:\Temp etc.
I am running this in a Windows 7 batch file.
For /f "delims=" %A in ('dir /b /a-d ^|findstr /i /v /e /r "\.PDF_[a-z0-9]*\.pdf"') do echo %A
Type
for /?
findstr /?
In a batch file use %%A rather than %A at command prompt.

Looping over files in subfolders matching a pattern in Windows CMD prompt

I am trying to find all files matching the wildcard *\dev\*\*\*\Results_local.xml in a cmd script, but I failed. I tried:
for %%f in (*\dev\*\*\*\Results_local.xml) do (
echo %%f
)
but it doesn't return any results. What should I do instead?
dir /s/b /a-d Results_local.xml |findstr /i /b /r ".*\\dev\\.*\\.*\\.*\\"
should give you a results list.
for /f "delims=" %%f in (
'dir /s/b /a-d Results_local.xml ^|findstr /i /b /r ".*\\dev\\.*\\.*\\.*\\"'
) do echo %%f
should apply the results to %%f one-by-one.

how do you delete all files NOT a flag in windows command prompt

I am trying to delete all items NOT .aspx, but i wasn't sure how to do it. Right now i was just going through everything with
del /F *.html
del /F *.csproj
del /F *.ico
etc.
Is there a simpler command to just say: DELETE EVERYTHING in DIRECTORY NOT a FOLDER OR has an extension of .aspx
I cant seem to find a solution on the Internet which matches my question. I didn't know if DEL accepted Regex to be like /^.*(^\.aspx)$ or something
bash file says:
chdir C:\Users\william.francis\Desktop\Deploy\tmp
for /f "delims=ÿ" %a in ('dir /b /a-d ^| find /v ".aspx"') do del /s /q /f %a
chdir ..
Edit: would like a reason as to the downvotes so i could fix it. I find this question to be valid enough to not get penalized. shrug
You can do it this way:
for /f "delims=ÿ" %a in ('dir /b /ad ^| find /v ".aspx"') do rd /s /q %a
for /f "delims=ÿ" %a in ('dir /b /a-d ^| find /v ".aspx"') do del /s /q /f %a
Please run it on a test directory first.
Another form:
for /f "delims=ÿ" %a in ('dir /b /a-d') do if not "%~xa"==".aspx" del /q %a
As a batch file:
#echo off
for /f "delims=ÿ" %%a in ('dir /b /a-d ^| find /v ".aspx"') do del /s /q /f %%a
Or
#echo off
for /f "delims=ÿ" %%a in ('dir /b /a-d') do if not "%%~xa"==".aspx" del /q %%a
Save it to a file like file.bat, or file.cmd then run x:\path\to\file.bat or x:\path\to\file.cmd.

Resources