简体   繁体   中英

Need a batch file that can find folders based on filenames

I have a need for a batch file or utility that would be able to find any "un-compressed archive folders" that are no longer needed and can now be deleted because the original archive file is still present.

The key is that the "un-compressed folder" and the "original archive file" always have the same name except for the file extension of the archive file. I do not want to automatically delete anything; I just want to create a list of folders that I can manually check out. So the sequence would be a 4 step process:

1) Search for all archive files using wildcards such as *.zip, *.rar, *.iso 2) Create a list of all of the filenames that are found - minus the file extensions 3) Use the list created in step two to search for any folders with those names 4) Create a text file with any folders found in step three.

I tried modifying a batch file that I found in these posts but it didn't work and I would like to start from scratch. Any help would be greatly appreciated.

Thanks

Ok, I'll do this step by step:

Step 1:

set dir="C:\...[path to root directory]" 
where /r %dir% *.zip *.iso *.rar >> log.txt

Note the where utility should be on your computer if using windows 7.

Step 2:

ren log.txt log.tmp
for /f "delims=." %%a in (log.tmp) do (Echo %%a >> log.txt)
del log.tmp

The above code will not handle files names with periods in it

Step 3:

for /f "tokens=*" %%a in (log.txt) do (
where /r %dir% %%a* >> files.txt
)

Not 100% sure if above will work, tell me if it doesn't.

Step 4:

Rem This code will handle file paths to directories
Ren files.txt files.tmp
for /f "tokens=*" %%a in (files.tmp) do (
Echo %%~pa >> files.txt
)
del files.tmp

Rem The below code will ged rid of repeated direcotries
ren files.txt files.tmp
Echo. > files.txt

setlocal enabledelayedexpansion
for /f "tokens=*" %%a in (files.tmp) do (
set var=1
for /f "tokens=*" %%b in (files.txt) do (
if "%%~a" equ "%%~b" set var=0
)
if !var!==1 Echo %%a >> files.txt
)
del files.tmp

And I'm rather confident that should work. Of course I haven't tested this, but run all of this with @Echo on and a pause command between each sect (or as seperate batch files) so that if an eror does occur I can try helping you.

Hope this was helpful, Mona.

@echo off

    setlocal enableextensions

    set "rootDir=d:\_data_"
    set "fileList=%~dp0\%~n0.files.list"
    set "folderList=%~dp0\%~n0.folders.list"

    rem generate list of compressed files names
    break > "%fileList%"
    for /F "tokens=*" %%f in ('where /r "%rootDir%" *.zip *.rar *.iso *.7z 2^>nul') do (
        >> "%fileList%" echo %%~nf
    )

    rem check compressed file list against directory list
    break > "%folderList%"
    for /F "tokens=*" %%f in ('dir "%rootDir%" /s /b /ad ^| findstr /e /g:"%fileList%" ') do (
        >> "%folderList%" echo %%f
    )

    type "%folderList%"

    endlocal

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM