Answer the question
In order to leave comments, you need to log in
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
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
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"
)
" [SITE] Folder #1 "
became
" Folder #1 [NEWSITE.RU] "
Answer the question
In order to leave comments, you need to log in
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%\"
:: Переименовывание файлов
Call :funcRename "%WorkPath%\%SaveDir%\*%SearchKeyword%*" %SearchKeyword%
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question