0

trying to replace drive designation inside Idrive log files using a batch. Amateur programmer trying to understand batch files for years but still don't get advanced techniques. I suspect the '\' backslash needs to be escaped but nothing I try works.

Sample Log input:

[C:\Users\rh56\Music\desktop.ini]
[D:\clients\]
[D:\Orion\]
[D:\rigel\]
[SUCCESS] [03/06/2020 5:02:19] [[Full Backup]  C:\stuff\2019-01-14 11_54_14-Cartoon Caption Contest _ The New Yorker.png][Size: 191.03 KB]]
[SUCCESS] [03/06/2020 5:02:19] [[Full Backup][C:\junk\2019-05-01 20_22_26-Greenshot.png][Size: 384.27 KB]]
[SUCCESS] [03/06/2020 5:02:20] [[Full Backup]C:\arbitrage\EuroYen30day\2019-05-01 20_23_06-Window.png][Size: 271.33 KB]]

Desired Output: (notice substituting M:\ for C:\ )

[M:\Users\rh56\Music\desktop.ini]
[D:\clients\]
[D:\Orion\]
[D:\rigel\]
[SUCCESS] [03/06/2020 5:02:19] [[Full Backup]  M:\stuff\2019-01-14 11_54_14-Cartoon Caption Contest _ The New Yorker.png][Size: 191.03 KB]]
[SUCCESS] [03/06/2020 5:02:19] [[Full Backup][M:\junk\2019-05-01 20_22_26-Greenshot.png][Size: 384.27 KB]]
[SUCCESS] [03/06/2020 5:02:20] [[Full Backup][M:\arbitrage\EuroYen30day\2019-05-01 20_23_06-Window.png][Size: 271.33 KB]]

My code:

  set vSearch=C:\\ & set vReplace=M:\\
  setlocal enableextensions disabledelayedexpansion
  ::https://stackoverflow.com/questions/23075953/batch-script-to-find-and-replace-a-string-in-text-file-without-creating-an-extra/23076141
echo. & echo DEBUG DEBUG DEBUG & pause & echo.
    set "vtextFile=C:\idriveLogChecker\molly.log"
    for /f "delims=" %%i in ('type "%vtextFile%" ^& break ^> "%vtextFile%" ') do (
        set "line=%%i"
        setlocal enabledelayedexpansion
        >>"%vtextFile%" echo(!line:%vSearch%=%vReplace%!
        endlocal
    )
echo. & echo DEBUG DEBUG DEBUG & pause & net use & set v & echo on & echo. & cmd /K

This all seems very convoluted—is there some easier highly automated way to do this with minimal learning curve? Thank you for your help

Compo
  • 36,585
  • 5
  • 27
  • 39
wschloss
  • 15
  • 1
  • 5
  • 1
    https://stackoverflow.com/q/60034/62576 – Ken White May 07 '20 at 18:30
  • 1
    If you are willing to use a third party utility: [jrepl.bat](https://www.dostips.com/forum/viewtopic.php?t=6044) – Stephan May 07 '20 at 18:38
  • `type "%vtextFile%" |jrepl "C:\\" "M:\\" >newFile.txt` - jrepl uses REGEX, so \ is an escape char, which has to be escaped (with the escape char \ of course). Or a bit more elegant: `jrepl "C:\" "M:\" /f "%vtextFile%" /l /o -`, which overwrites the file with the changed test. – Stephan May 08 '20 at 07:38

1 Answers1

2

Your code works fine for me - when I correct just one line:

set "vSearch=C:\" & set "vReplace=M:\"

The \ hasn't to be escaped (another \ would be wrong anyway. The escape char is ^) and more important: Your original line sets the first variable to C:\\<SPACE>, so there is nothing to be replaced.

Note the position of the quotes - they don't become part of the variable name or value, but is the recommended syntax and avoids stray spaces and is safe against some poisonous characters like <>&|.

aschipfl
  • 33,626
  • 12
  • 54
  • 99
Stephan
  • 53,940
  • 10
  • 58
  • 91
  • Thanks all for being so quick. Stephan, when I edit as you suggest, the string 'C:\' is replaced with nothing; '', so for instance 'C:\junk' becomes simply 'junk' . Can ANY code be quoted and the quotes are ignored? That doesn't make sense to me but at this point I just want it to work. Frustrating! I had a feeling the \\ was incorrect but got that from this site which seems pretty good; https://www.robvanderwoude.com/escapechars.php. Nothing has yet worked. jrepl.bat seems similarly quirky. I will try FART, though I admit to being a bit of a snob and I don't like it. ::) – wschloss May 08 '20 at 00:06
  • Woops !! I made a typo earlier today when correcting my code based on Stephan's comment/suggestion and it is now corrected and working! Finally! Thank you Stephan, will mark as solved. I appreciate both the solution and the explanation. – wschloss May 08 '20 at 07:06
  • Quotes around a string are never ignored in `cmd`. ( `set` is sort of an exception - they are not ignored, but also not part of the string with this syntax). If you want to learn more, get yourself a quiet room and a big can of coffee (you'll need it) and read [this](https://stackoverflow.com/questions/4094699/how-does-the-windows-command-interpreter-cmd-exe-parse-scripts). – Stephan May 08 '20 at 07:28
  • Thanks Stephan, I got through 4 cans in two paragraphs then started daydreaming about something more interesting like Organic Chemistry as it applies to Cost Accounting as utilized by speakers of Ancient Greek. Seriously thanks for your help! – wschloss May 15 '20 at 17:59