batch file to count files in .zip files - batch-file

There are multiple zipped files say zip1,zip2 etc. Each zip file has multiple files in it. How do we create a batch file that only shows the number of files in the specific folder as
zip1 : 524 files
zip2 : 322 files
the code I made runs on other folders but not on zip files
#ECHO OFF
FOR /D %%D IN ("folderpath\*") DO (
FOR /F %%K IN ('DIR /A-D "%%D" 2^>NUL ^| FIND "File(s)" ^|^| ECHO 0') DO (
ECHO %%D: %%K
)
)

Here's a subroutine that uses zipjs.bat and counts the files within a zip file (it does not require external binaries):
#echo off
:countFilesInZip file.zip [out]
setlocal
set "count=0"
for /f "skip=1 tokens=* delims=" %%a in ('
call zipjs.bat list -flat yes -source "%~f1"^|
findstr /e /v "\\"
') do (
set /a count=count+1
)
::echo %count%
endlocal & if "%~2" EQU "" (
echo %count%
) else (
set "%~2=%count%"
)
goto :eof
to use it:
call :countFilesInZip "c:\zipfile.zip" count
echo %count%

Related

Link a wild card to a script in Batch

Hello I was told to ask another question
I would like to see how to add this line to my current script
FOR /f "delims=" %%q IN ('dir /b /s /a-d "%source%\(1)*.txt"') DO (
Or if I can get the help updating "File" and "Filename" to this (1) *.txt this would great as well
#ECHO OFF
SETLOCAL
ECHO STARTING 1
For %%G In ("%~dp0..\New Folder 1") Do Set "source=%%~fG"
For %%G In ("%~dp0..\New Folder 2") Do Set "target=%%~fG"
For %%G In ("%~dp0..\New Folder 3") Do Set "destdir=%%~fG"
set "FILE=(1) Homes in Texas.txt"
set "FILENAME=(1) Homes in Texas.txt"
for /f "tokens=1 delims=[]" %%a in ('find /n "appi"^<"%source%\%file%"') do set /a start=%%a
for /f "tokens=1 delims=[]" %%a in ('find /n "opcn"^<"%source%\%file%"') do set /a end=%%a
(
for /f "tokens=1* delims=[]" %%a in ('find /n /v ""^<"%source%\%file%"') do (
IF %%a geq %start% IF %%a leq %end% ECHO(%%b
)
)>"%target%\(A)_sets.txt"
for /f "tokens=1 delims=[]" %%a in ('find /n "paid"^<"%source%\%file%" ') do set /a start=%%a
for /f "tokens=1 delims=[]" %%a in ('find /n "whbn"^<"%source%\%file%" ') do set /a end=%%a
(
for /f "tokens=1* delims=[]" %%a in ('find /n /v ""^<"%source%\%file%" ') do (
IF %%a geq %start% IF %%a leq %end% ECHO(%%b
)
)>"%target%\(B)_sets.txt"
If Exist "%target%\*.txt" If Exist "%destdir%\" (
Copy /Y /B "%target%\(A)_Sets.txt" + "%target%\(B)_Sets.txt" "%destdir%\(1).txt"
)
ren "%destdir%\(1).txt" "%filename%"
If Exist "%source%\(2) *.txt" (
call "%~dp0(2) Extraction tool.bat"
)
GOTO :EOF
Now I would like to keep this script the speed on my script is very fast it can edit my files at 1 sec per file
I'm OK with using a partial Filename
Because of my setup it makes for no errors, it's super fast speed and all files have these in the filename (?)
With the way I have my setup my workstation, I would have to edit my scripts over and over to match the file name, so I'm trying to use these (1) *.txt, because all my txt file look like this
Sub Houston Folder
(1) Houston.txt
(2) Houston.txt
(3) Houston.txt
Sub Auston Folder
(1) Austin.txt
(2) Austin.txt
Sub Dallas Folder
(1) Dallas.txt
(2) Dallas.txt
(3) Dallas.txt
I have over 5000 files easy
They all have (?) in the file name so looking for (1) *.txt, but don't know if this will be an issue with ren "%destdir%\(1).txt" "%filename%" using (1) *.txt
If there is a way to fix my File and Filename to work on (1) *.txt and to keep the original file name as end result that would be very appreciated
Any Help will be great
Is this correct, when I set this up this way or any other way I get blank files [corrected code below. Stephan]
REM not here... set "FILE=%~1"
set "FILENAME=(1) Homes in Texas.txt"
FOR /f "delims=" %%q IN ('dir /b /s /a-d "%source%\(1)*.txt"') DO call :label "%%q"
goto :eof
:Label
REM here "%~1" gets the value "%%q" from the CALL command, a FQDN (filename including drive and path because of 'dir /s /b')
for /f "tokens=1 delims=[]" %%a in ('find /n "appi"^<"%~1"') do set /a start=%%a
My Setup Folders
Main Folder
|Extraction Batch Folder
|New Folder 1
|all original txt files
|New Folder 2
|all extracted txt files
|New Folder 3
|all renamed and merged files
It's now almost ready We got File fixed with that line
still working on Filename
If Exist "%target%\*.txt" If Exist "%destdir%\" (
Copy /Y /B "%target%\(A)_Sets.txt" + "%target%\(B)_Sets.txt" "%destdir%\(1).txt"
)
ren "%destdir%\(1).txt" "%filename%"
I know that wildcards does work with ren I tested this to confirm *
If Exist "%target%\*.txt" If Exist "%destdir%\" (
Copy /Y /B "%target%\(A)_Sets.txt" + "%target%\(B)_Sets.txt" "%destdir%\(1).txt"
)
ren "%destdir%\(1).txt" "(*)A.txt"
and in %destdir% it rename to (1)A.txt so the wild card sees the (1)
how can I set this up to take the matching (1) on the filename located in the source folder and use that to rename the (1).txt found in `%destdir% folder

How to delete only text files except few files

In a certain path I have some different kinds of file type. Eg., .txt, .bas, .cls, etc.
I need to delete only the text files in that path except few files.
For eg, if the path has a.txt, b.txt, c.txt, aa.bas, bb.cls, it should delete only a.txt. It should not delete b.txt and c.txt (Also it should not delete the other extension files).
To delete all ?.txt files in the root folder, excluding b.txt and c.txt
#echo off
for %%i in (?.txt) do (
if not "%%~nxi"=="c.txt" if not "%%~nxi"=="b.txt" echo del "%%i"
)
To do this in the root and subdirectories:
#echo off
for /R %%i in (?.txt) do (
if not "%%~nxi"=="c.txt" if not "%%~nxi"=="b.txt" echo del "%%i"
)
If the files are to be all *.txt files and not just single digit as per your example (add /R to recurse:
#echo off
for %%i in (*.txt) do (
if not "%%~nxi"=="c.txt" if not "%%~nxi"=="b.txt" echo del "%%i"
)
Similarly, but using findstr to only exclude:
#echo off
for /f %%i in ('dir /b /a-d ^|findstr /vi "b.txt" ^|findstr /vi "c.txt"') do (
echo del "%%i"
)
and to search only include:
#echo off
for /f %%i in ('dir /b /a-d ^|findstr /i "a.txt"') do (
echo del "%%i"
)
and to include and search subdirectories:
#echo off
for /f %%i in ('dir /b /s /a-d ^|findstr /i "a.txt"') do (
echo del "%%i"
)
On all of the above examples, remove echo to actually perform the delete, echo is used as a safety measure and will only display the del result to console.
Edit
Seeing as you specifically have a list of files (as per one of you comments) to exclude, you can use something like this. You have to create a file called exclusion.txt and add the files to exclude in list form:
b.txt
c.txt
file with space.txt
d.txt
Then create the batch file and add the code below. When ran, it will prompt for the file extention to filter on, where you can type an extension. i.e txt or simply press enter to perform a delete on all files, except the excluded ones. Just to be safe, I added an additional for loop to simply echo the files and prompt you if you are sure you want to delete the files.
#echo off
set cnt=0 & set excl= & set ext=
echo(
if not exist exclusion.txt echo You have not created an "exclusion.txt" file. & echo( & echo You need to create it first, then rerun the script & echo( & pause & goto :eof
echo Ensure you have listed all files to be excluded in "exclusion.txt" file
echo(
set /p "ext=Add File extention to search on (txt, pdf, etc), or press enter for all files: "
if not defined ext goto cont
if not "%ext:~0,1%"=="." set "ext=.%ext%"
set "ext=*%ext%"
:cont
setlocal enabledelayedexpansion
for /f "delims=" %%a in (exclusion.txt) do (
set /a cnt+=1
set "nlr!cnt!=%%a"
)
for /l %%i in (1,1,%cnt%) do (
if not defined excl (
set "excl=!nlr%%i!"
) else (
set "excl=!excl! !nlr%%i!"
)
)
echo(
echo WARNING: You are about to delete the following files!!
echo(
for /f "delims=" %%i in ('dir /b /a-d %ext% ^|findstr /VIE "%excl%"') do (
if /i not "%%i"=="exclusion.txt" if not "%%i"=="%~0" echo %%i
)
echo(
Choice /c YN /m "Are you sure you want to delete these files?"
if %errorlevel% equ 2 goto :eof
for /f "delims=" %%i in ('dir /b /a-d %ext% ^|findstr /VIE "%excl%"') do (
if /i not "%%i"=="exclusion.txt" if not "%%i"=="%~0" del %%i
)

Windows Command to identify folders with only one file

I need a command to run from the Windows CLI to identify any folders (or sub folders) that contain only one file. If the folder contains two files, it should not be included. In the end, I need to output this list to a text file. It should contain the full folder path.
Ex: OutputLog.txt
C:\fold1
C:\fold1\sub
C:\fold3
C:\fold4
This should work to identify folders with one file.
#echo off
for /d /r "d:\base\folder" %%a in (*) do (
dir /b /a-d "%%a" 2>nul |find /c /v "" |findstr "^1$" >nul && >>file.txt echo %%a
)
#echo off
setlocal EnableDelayedExpansion
(for /D /R %%a in (*) do (
set count=0
for %%b in ("%%a\*.*") do set /A count+=1
if !count! equ 1 echo %%a
)) > OutputLog.txt
#echo off
set "parentfolder=c:\test"
for /f "tokens=* delims=" %%F in ('dir /s /a:d /b "%parentfolder%"') do (
dir "%%F"|findstr /b "1 File(s)" >nul 2>&1 && echo %%F
)
This will list all subfolders with only one file in a parent folder .As it checks the string of the dir command output it should be altered if language settings/windows version provide different DIR command output.

Windows batch file force return error level 1 when it finds a missing file

I am new to command scripting.
I am able to find and list out the missing files from a folder into an output file using the below code.
Please let me know how can i force to get an error code 1 when there is a missing file.
Thanks
#echooff
setlocal enabledelayedexpansion
pushd "N:\opasdata\d110001\medias\images"
set found=false
for /f "tokens=* delims=" %%a in (listimagescopy.txt) do (
for /r %%x in (%%a) do (
if exist "%%a" set found=true
)
if "!found!"=="false" echo %%a >>"V:\Current Library\notfound.txt"
set found=false
)
ECHO Files are Available
EXIT /B 0
:END
ECHO Files are not Available
EXIT /B 1
#echooff
setlocal enableextensions disabledelayedexpansion
pushd "N:\opasdata\d110001\medias\images"
set "missingFiles=0"
for /f "delims=" %%a in (listimagescopy.txt) do (
dir /s /b /a-d "%%a" >nul 2>nul || (
echo %%a >> "V:\Current Library\notfound.txt"
set "missingFiles=1"
)
)
if %missingFiles%==0 (
echo Files are available
) else (
echo Files are not available
)
popd & endlocal & exit /b %missingFiles%

Batch file for counting the files with same initial name and moving them

Is it possible to do this?
In a folder i have files with same initial names
Example:
Main folder
-Quest2323231.txt
Quest2343434.txt
Quest2343435.txt
Fund103.txt
Fund102.txt
I have a config file (abc.config) in which i have name of file on which i need to count and move them . If the count is more than 2 then i need to move them.
In this case for e g I need to find files which have name as 'Quest'
Appreciate you help on this.
#echo off
setlocal enableextensions
set "number="
for /f "tokens=1" %%a in (
'dir /a-d /-c "c:\mainfolder\quest*" 2^>nul^|findstr /b /c:" "'
) do if not defined number set "number=%%a"
if not defined number set "number=0"
echo %number%
Untested: This expects text in the first line of abc.config which is the file information such as Quest in the example and if there are more than 2 matching files in the source folder then it will echo a move command to move them to the target folder.
Change *%file%* to %file%* in two places if you want to match only the start of the filename.
Remove the echo to actually perform the move commands.
#echo off
set "source=c:\mainfolder"
set "target=d:\target\folder"
set /p "file=" < "abc.config"
for /f %%a in ('dir /b /a-d "%source%\*%file%*" ^|find /i "%file%" 2^>nul^|find /c /v "" ') do set "number=%%a"
if %number% GTR 2 for /f "delims=" %%a in ('dir /b /a-d "%source%\*%file%*" ^|find /i "%file%" ') do (
echo move "%source%\%%a" "%target%"
)
pause

Resources