Meet the Other Phone. Child-safe in minutes.

Meet the Other Phone.
Child-safe in minutes.

Buy now

Please or to access all these features

Geeky stuff

Windows command file help needed please

11 replies

UnderZealous · 07/11/2024 17:27

Please can someone help me write a batch file?

This is what I want it to do

For each line in File1:
Read the next line of File1 and put value in OldName
Read the next line of File2 and put value in NewName
Find and replace all occurrences of OldName in files in directory and all subdirectories with NewName.
If there is no match, write the OldName value in Unused.txt

OP posts:
DanFmDorking · 07/11/2024 21:57

@UnderZealous - I've had a quick look through the DOS (or'.bat') file commands but I can't see anything useful, sorry.

However, I rather these probs!
I would consider loading 'File 1' and 'File 2' into excel and with a Formula (or two or more) put the appropriate names into new columns 'OldName' and 'NewName'.

Then, have a cup of tea and think about what to do next.

UnderZealous · 08/11/2024 08:58

Thanks. They are in Excel, so I could do it with 1 .csv file instead of 2 text files, but that isn't my problem.

The bits I'm struggling with are the
For each line get str1 and str2 then do [command using str1 and str2]

OP posts:
LardyCakeLover · 08/11/2024 22:04

Something like this should work (I haven't tested it fully)

@echo off
setlocal enabledelayedexpansion

REM Set file paths
set "file1=File1.txt"
set "file2=File2.txt"
set "unused=Unused.txt"
set "directory=."

REM Clear or create the Unused.txt file
> "%unused%" echo Unused entries:

REM Initialize counters for reading lines
set /a lineCount=0

REM Read File1 and File2 line by line
for /f "usebackq delims=" %%A in ("%file1%") do (
set "oldName=%%A"

REM Read corresponding line from File2
set "newName="
set /a lineCount+=1
for /f "skip=!lineCount! delims=" %%B in ("%file2%") do (
    set "newName=%%B"
    goto foundNewName
)

:foundNewName
if defined newName (
    REM Find and replace OldName with NewName in the specified directory
    set "foundMatch=0"
    for /r "%directory%" %%F in (*) do (
        findstr /m /c:"!oldName!" "%%F" >nul
        if !errorlevel! equ 0 (
            REM Replace OldName with NewName in file
            set "foundMatch=1"
            powershell -Command "(Get-Content -Path '%%F') -replace '!oldName!', '!newName!' | Set-Content -Path '%%F'"
        )
    )
    REM If no matches were found, log OldName in Unused.txt
    if !foundMatch! equ 0 (
        echo !oldName! >> "%unused%"
    )
)

)

endlocal

LardyCakeLover · 08/11/2024 22:07

If you've never used it, try Chatgpt - it's great for these sort of programming scripts. I use it for small Perl scripts all the time

The code above was the result of putting your post into Chatgpt

DanFmDorking · 08/11/2024 23:59

@LardyCakeLover - wow - impressed - never thought about Chatgpt - thanks.

UnderZealous · 09/11/2024 08:55

I tried searching, but the suggestions didn't work properly. It wasn't Chatgpt I used. I'll try your suggestion. Thanks. It would save me weeks of tedious work.

Each entry should have at least one match now, as I went through looking for and deleting any that didn't. It took hours. Smile
Fortunately it was a quiet day with no interruptions, so it filled the time.

OP posts:
wavingfuriously · 30/11/2024 01:44

LardyCakeLover · 08/11/2024 22:07

If you've never used it, try Chatgpt - it's great for these sort of programming scripts. I use it for small Perl scripts all the time

The code above was the result of putting your post into Chatgpt

Dang...thought u were a programmer!🤭

UnderZealous · 30/11/2024 10:20

I found a free app to do it. It does the job.

OP posts:
DanFmDorking · 30/11/2024 22:03

@UnderZealous Could you give me the details of the app please?

UnderZealous · 02/12/2024 20:37

fnr
Overview

OP posts:
DanFmDorking · 03/12/2024 01:19

@UnderZealous Thank you.

New posts on this thread. Refresh page