Home > Blockchain >  How to edit files in certain folders - excluding all other folders
How to edit files in certain folders - excluding all other folders

Time:12-21

I'm working on this script and I am trying to make this read a certain Folder

@echo off
Setlocal enabledelayedexpansion

Set "Pattern="
Set "Replace="

@For %%G In ('dir /b /a-d "Quills"') Do (

FOR /f "delims=" %%a IN ('dir /b /s /a-d "*.txt"') DO (
    Set "File=%%~na"
    Ren "%%a" "!File:%Pattern%=%Replace%!%%~xa"
  )
)

I have tried multiple setting but nothing seems to work
I added this @For %%G In ('dir /b /a-d "Quills"') Do (
I added this Set "Source="
I added this @For %%G In (Quills) Do (
I added this FOR /f "delims=" %%a IN ('dir /b /s /a-d "Quill\*.txt"') DO (

Quills is the Folder Name I want it to read, and I have many Sub folders and Quill is found in many places Any Help will be awesome

I have many folders with many different names, and I have different folders I edit files in

example:
main folder
--- Folder 1
------Quills
------Folder 1
------Folder 2
main folder 2
--- Folder 1
------Quills
------Folder 1
------Folder 2

Quill is located in many different Sub Folders, and inside of every folder I have many txt files, what I do is I use the script to edit the file name, I remove certain characters from the file name, for example upper_case.txt, here I would use the Search to find all_and Replace it with a space, like this upper case.txt, but I only want the script to do this to the files found inside the Quills Folders

OK, guys thanks to Gerhard I was able to find the problem with my original script

I will leave my fixed original script here for anyone that might need it

@echo off
Setlocal enabledelayedexpansion

Set "Pattern=What to search for"
Set "Replace=What to replace it with"

FOR /R %%a IN ("Folder Name\*.txt") DO (
    Set "File=%%~na"
    Ren "%%a" "!File:%Pattern%=%Replace%!%%~xa"
)

Thank you

CodePudding user response:

My assumption is that Quills is a single Directory and you want to recursively search files in the root and subfolders of it:

@echo off
setlocal enabledelayedexpansion
set "search=str_to_search"
set "replace=to_replace"
for /R %%i in ("Quills\*.txt") do (
    set "fname=%%~nxi"
    if "!fname:%search%=%search%!" == "!fname!" ren "%%~fi" "!fname:%search%=%replace%!"
)

If however you have mulitple Quills directories in different sub Folders and want to do it for each of them:

@echo off
setlocal enabledelayedexpansion
set "search=search_str"
set "replace=replace_str"
for /F "delims=" %%i in ('dir /b/s/a-d *.txt ^| findstr /i "Quills\\"') do (
    set "fname=%%~nxi"
    if "!fname:%search%=%search%!" == "!fname!" ren "%%~fi" "!fname:%search%=%replace%!"
)
  • Related