Jump to content

[Solved] Replace string in multiple file names


Recommended Posts

For hashes I tend to use the powershell module, however I also still use Microsoft's own fciv.exe, downloaded in this self extractor.

 

With that I'd just use this kind of structure:

"PATH TO\FCIV.EXE" %CD% -SHA1 -TYPE *.EXE -WP

Or:

FOR /F "EOL=/ TOKENS=*" %A IN ('"PATH TO\FCIV.EXE" %CD% -SHA1 -TYPE *.EXE -WP') DO >>OUTPUT.LOG @ECHO(%A

to create a log file containing only the SHA1 hashes of all of the EXE files in the current directory.

 

I would suggest you look for a more suitable checksum tool instead of having to 'fix' the output from every use of your current one.

 

Also I have to ask again, Why ?SHA1 and not ?SHA1*, I'm fairly sure from what you've posted that the check-summed files were not named beginning with an asterisk.

I skipped fciv. I remove only ?SHA1 to look like checksum files generated by HashCheck Shell Extension.

Thanks!

Link to comment
Share on other sites


And, just to bring some added uncertainty :w00t: to this thread, why exactly one would use SHA1? :unsure:

 

I mean, what is the actual scope (or final goal) of the hashing?

 

Possibly (please) refraining from the usual nonsense about MD5 collisions and the algorithm being compromised.

 

jaclaz

Link to comment
Share on other sites

And, just to bring some added uncertainty :w00t: to this thread, why exactly one would use SHA1? :unsure:

 

I mean, what is the actual scope (or final goal) of the hashing?

 

Possibly (please) refraining from the usual nonsense about MD5 collisions and the algorithm being compromised.

 

jaclaz

SHA-1 is compromised too, so we should use SHA-512, but that will require a completely new batch script. :D

Link to comment
Share on other sites

No. :no:

SHA-1 is NOT compromised, an algorithm to reduce the needed calculations from 280 to a mere 269 has been developed.

This in layman terms translates "from one zillion to a few fantastillions", a normal human mind cannot even start to appreciate how many items are in a zillion or in a fantastillion, everything is fine, don't worry, don't panic.

Practical consequences: NONE

Practical consequences for anyone but (maybe) NSA and other three-or-more-letter agancies: NONE42

Practical consequences for anyone using the hash for file verification: NONE84

By convention NONE to the power of 42 is used to describe a very, very, very, very, little amount, and NONE to the power of 84 is lot less than that.

jaclaz

Link to comment
Share on other sites

Computing SHA-1 sums is a little bit paranoid. md5 is enough secure. I compute SHA-1, because my machine is loaded up to 16% during hash calculation, which is fine. The HDD is loaded at its maximum read speed during hash computing which means that if I calculate md5 sums instead of sha-1, it will require the same amount of time. In this case a faster storage is needed to finish calculation faster. Almost all hash command line utilities, except for exactfile can max out the read speed of a HDD. AutoIt _Crypt_HashFile function is on the limit, with 175 MB/s (on a RAID0 SSD array), but it's not exactly a command line tool, though it can be used like one.

Can I ask jaclaz for a modified script that can replace that string recursively in subfolders?

I used:

FOR /F "tokens=* delims=" %%A IN ('DIR /B /S *.sha1') DO echo Processing %%A...&CALL :do_replace "%%A"

but it's not enough. Also, an example from stackoverflow doesn't work with your script.

setlocal EnableDelayedExpansionfor /r "D:\\New folder" %%f in (*.txt) do (  (for /f "tokens=*" %%l in (%%f) do (    set "line=%%l"    echo !line:  )) >"%%~ff.new"  del /q "%%~ff"  ren "%%~ff.new" "%%~nxf")

Thanks!

Edited by radix
Link to comment
Share on other sites

Can I ask jaclaz for a modified script that can replace that string recursively in subfolders?

Sure you can ask :).

Unfortunately :( jaclaz won't fulfill your request because, as already evidenced TWICE by him, the whole idea of using batch to replace a string in a file is foolish (besides being highly prone to errors/issues and definitely much slower then the suggested use of gsar or similar), unless there are valid REASONS for this choice (which are nowhere to be found in this specific case).

Yzöwl has pointed out also how it is easier/better/advised/etc, to use a tool that already writes the expected output when compared to using a tool that writes in a format that you need to post-process.

You may want to check the link in jaclaz's signature "But ... then, why?", you are of course very welcome  :thumbup  to insist on doing this particular foolish thing or any other you may think of, but without  jaclaz's support, at least for this particular one.

 

Hint ;):

What does "%~nx1" expand to?

What does "%~dpnx1" expand to?

jaclaz

Edited by jaclaz
Link to comment
Share on other sites

Thanks for the tip!

For recursive find and replace string in multiple files:

SETLOCAL ENABLEEXTENSIONS ENABLEDELAYEDEXPANSIONSET New_string=new stringSET Old_string=old stringFOR /F "tokens=* delims=" %%A IN ('DIR /B /S *.txt') DO echo Processing %%A...&CALL :do_replace "%%A"GOTO :EOF:do_replaceIF EXIST "%~dpnx1.new" DEL "%~dpnx1.new"SET tomod=0FOR /F "tokens=* delims=" %%B IN ('TYPE "%~dpnx1"^|FIND "%Old_string%"') DO SET tomod=1&GOTO :outofloop:outofloopIF %tomod%==0 ECHO No match...&ECHO.&GOTO :EOFFOR /F "tokens=* delims=" %%B IN ('TYPE "%~dpnx1"') DO (SET Line=%%BSET ModLine=!Line:%Old_string%=%New_string%!IF NOT "!Line!"=="!Modline!" ECHO !Line!&ECHO !Modline!&ECHO.&&SET tomod=1ECHO !Modline!>>"%~dpnx1.new")FOR /F "tokens=* delims=" %%A IN ('DIR /B /S *.new') do move /y "%%A" "%~dpnx1"GOTO :EOF
Edited by radix
Link to comment
Share on other sites

Hmmm.

At least you could limit the loop on first match, since you have the "old string" on each line or so.

FOR /F "tokens=* delims=" %%B IN ('TYPE "%~dpnx1"^|FIND "%Old_string%"') DO SET tomod=1&GOTO :outofloop:outofloopIF %tomod%==0 ECHO No match...&ECHO.&GOTO :EOF

jaclaz

Link to comment
Share on other sites

@radix I'm still trying to work out what all of the code is for!

 

What happens if you run this?

@ECHO OFFSETLOCAL ENABLEEXTENSIONSSET "FS=files*.7z.*"SET "WD=D:\New folder"SET "RS=?SHA1"FOR /F "DELIMS=*" %%A IN ("%FS%") DO SET "FN=%%A">%FN%.sha1 TYPE NULFOR /F "TOKENS=*" %%A IN ('FSUM.EXE -SHA1 -D"%WD%" "%FS%"') DO (SET "OL=%%A"	>>%FN%.sha1 CALL ECHO;%%OL:%RS%=%%)

The only things you should need to change are %FS% and %WD% variables on lines 4 and 5 respectively.

  • Upvote 1
Link to comment
Share on other sites

Running

@ECHO OFFSETLOCAL ENABLEEXTENSIONSSET "FS=files*.7z.*"SET "WD=D:\New folder"SET "RS=?SHA1"FOR /F "DELIMS=*" %%A IN ("%FS%") DO SET "FN=%%A">"%WD%\%FN%.sha1" TYPE NULFOR /F "TOKENS=*" %%A IN ('FSUM.EXE -SHA1 -D"%WD%" "%FS%"') DO (SET "OL=%%A"	>>"%WD%\%FN%.sha1" CALL ECHO;%%OL:%RS%=%%)

it will create an output like this:

6ea15a475e8ab4db5769b3e37601a368834becb1 *files.7z.00159cf9224f46d5967d76a204e0d823f2d6e5f562e *files.7z.002

I've changed the output file path to folder where 7-Zip archives are located (working directory).

Thanks! :thumbup

It's possible to write this entire batch script in a single line and to work? I tried separating lines like this: command1 & command2... and not works due to loop.

Edited by radix
  • Upvote 1
Link to comment
Share on other sites

It's possible to write this entire batch script in a single line and to work? I tried separating lines like this: command1 & command2... and not works due to loop.

In order to prevent you wasting time in the hope of a solution to that request, I'll let you know now that it will not be fulfilled. I see no reason whatsoever to convert a nine line batch file into one line containing nine lines of batch code.

Link to comment
Share on other sites

Guest
This topic is now closed to further replies.
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.


×
×
  • Create New...