A
A
Alexander2022-02-12 14:05:20
cmd/bat
Alexander, 2022-02-12 14:05:20

How to find and remove all keywords from a certain list in file and folder names?

Hello!

The task is this. There are archives in which the names of folders and files contain certain keywords, for example:


[KEYWORD] Folder #1
-- [KEYWORD] Subfolder #1
-- [KEYWORD] Subfolder #2
[KEYWORD] Folder #2
[KEYWORD] File #1.txt
[KEYWORD] File #2.txt
File #3.txt
File to delete.docx

You need to find [KEYWORD] in all folders and files and delete it, as well as add a new key [DOMAIN.RU] at the end of the name . In addition, you need to remove certain files from the archive from the list specified in txt.

Result example:

Folder #1 [DOMAIN.RU]
--Subfolder #1 [DOMAIN.RU]
--Subfolder #2 [DOMAIN.RU]
Folder #2 [DOMAIN.RU]
File #1 [DOMAIN.RU].txt
File #2 [ DOMAIN.RU].txt
File №3.txt

After reading the manuals and searching for information on the Internet, I was able to sketch the following script:
spoiler
chcp 1251
@ECHO ON

:: Удаляемое ключевое слово
SET SearchKeyword=[SITE]
:: Добавляемое ключевое слово
SET ReplaceKeyword=[NEWSITE.RU]
:: Рабочая папка
SET SaveDir=RESULT
:: Папка с файлами
SET FilesDir=FILES

:: Список ключей для замены
SET ListNames=LIST_NAMES.txt
:: Списком удаляемых файлов
SET ListFiles=LIST_FILES.txt

:: Допустимые типы архивов
SET Mask="*.zip" "*.rar" 
:: Путь к архиватору
SET Archiver="C:\Program Files\WinRAR\WinRAR.exe"

:: Определение пути
SET WorkPath=%CD%

:: Удаление папки
RD /S /Q "%WorkPath%\%SaveDir%\"
:: Создание папки
MD %SaveDir%

FOR %%f IN (%Mask%) DO (

    :: Распаковка архива
    %Archiver% X "%%f" "%SaveDir%\"  

    :: Удаление исключенных файлов
    Call :deleteFiles "%WorkPath%\%FilesDir%\%ListFiles%"

    :: Переименовывание файлов
    Call :funcRename "%WorkPath%\%SaveDir%\*%SearchKeyword%*" %SearchKeyword%

    :: Добавление файлов в архив
    %Archiver% A -M5 -Y -EP1 -R "%WorkPath%\%SaveDir%\%%~nf %ReplaceKeyword%.zip" "%WorkPath%\%SaveDir%\"
)


    pause
    Exit


::Переименовывание файлов
:funcRename
    FOR /F "usebackq delims=" %%d IN (`2^>nul Dir %1 /S /B /A:D ^|Sort /R`) DO (
            Set "N=%%~nd" &Call Ren "%%d" "%%N:%SearchKeyword%=%%%%~xd %ReplaceKeyword%"
        )
    Exit /B

::Удаление файлов
:deleteFiles
    FOR /F "usebackq delims=" %%a IN (%WorkPath%\%FilesDir%\%ListFiles%) DO (
        DEL /F /Q "%WorkPath%\%SaveDir%\%%a"
    )

It may have turned out to be shit code, but this is due to my "no" experience in working with BAT. In general, by the way, the script works, but crookedly. I drop the archive into the folder with the script, run the batch file, the archive is unpacked, unnecessary files are deleted, then it is renamed and again packed into the archive. The problems are as follows:

1) Bypass is performed only by folders, files are not affected. I also failed to make a bypass on the files.

2) If there is a period in the search keyword, then the replacement does not work. For example, everything will work fine with the [SITE] key , but there are already problems with [SITE.RU] . I didn't figure out how to fix this.

3) After removal from the key name, spaces may remain at the beginning. Suppose there was a name
" [SITE] Folder #1 "
became
" Folder #1 [NEWSITE.RU] "

To solve the problem, I tried adding a space at the end to the search key itself, and in general this solves the problem, but there is a possibility that the original name may be [SITE] Folder # 1 and then the key will not be found. In short, you need to somehow remove the space at the beginning of the name.

4) Considering that the archive is searched for automatically in the folder, is it possible to implement a warning and stop the script if there is more than one archive in the folder?

5) Now the script works to search for only one key specified in SearchKeyword . How can I implement multiple searches? For example, also how it is implemented with deleting files. All files to be deleted are stored in LIST_FILES.txtBut, here it turns out already, and so the cycle in the cycle, so I'm a little confused) I

implemented everything I could, and then nothing more ... Therefore, I will be very grateful for the help. If necessary, I will pay)

For convenience, I posted the source codes on Yandex disk

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Alexander, 2022-02-12
@PR0Z0N

PS - to solve the 5th question, I thought to move the code with deletion, name change and further archiving, outside the cycle, I mean this code:

:: Удаление исключенных файлов
    Call :deleteFiles "%WorkPath%\%FilesDir%\%ListFiles%"

    :: Переименовывание файлов
    Call :funcRename "%WorkPath%\%SaveDir%\*%SearchKeyword%*" %SearchKeyword%

    :: Добавление файлов в архив
    %Archiver% A -M5 -Y -EP1 -R "%WorkPath%\%SaveDir%\%%~nf %ReplaceKeyword%.zip" "%WorkPath%\%SaveDir%\"

and only then add a loop with keys for names and put there:
:: Переименовывание файлов
    Call :funcRename "%WorkPath%\%SaveDir%\*%SearchKeyword%*" %SearchKeyword%

But, in this case, the original name of the archive is lost. In general, I think this is exactly what you need to do, you just need to understand how to save the archive name)

A
Anton, 2022-02-15
@KJhas

Hm. A very inconvenient language for this task... Is it critical at all?
For me, such things are easily solved by the BASIC from word'a ... ('97).
Set she=CreateObject("Scripting.fileSystemObject")...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question