Jump to content
MSFN is made available via donations, subscriptions and advertising revenue. The use of ad-blocking software hurts the site. Please disable ad-blocking software or set an exception for MSFN. ×

[Solved] Replace string in multiple file names


Recommended Posts

Hello,

I got a similar problem with the next batch script, trying to replace a string with another one in multiple files:

@ECHO OFFSETLOCALfor %%* in (.) do set new=%%~n*SET "new=news tring"SET "old=XX XX"for %%f in (*.txt) do (    echo Processing %%f...    (    FOR /F "delims=" %%l IN (%%f) DO (        SET "line=%%l"        SETLOCAL ENABLEDELAYEDEXPANSION        set "x=!line:%old%=%new%!"        ECHO(!x!    ENDLOCAL    )    )>%%~nf.new)::for %%f in (*.new) do move /y "%%f" "%%~nf.txt"GOTO :EOF

Files that contain spaces in their names will not be edited.

I tried with these lines of code:

FOR /F "delims=" %%l IN (%%f) DO (
FOR /F "delims=" %%l IN (^"%%f^") DO (

as it's explained here: http://ss64.com/nt/syntax-dequote.html

but didn't work to escape ""

Edited by radix
Link to comment
Share on other sites


Hello radix,
 
IMO, best place to look at first is the command For /?. Look for the usebackq option.
 
Quote from For /?:

usebackq        - specifies that the new semantics are in force,
                  where a back quoted string is executed as a
                  command and a single quoted string is a
                  literal string command and allows the use of
                  double quotes to quote file names in
                  file-set.

 
To change behaviour of how quoting works, use the backquotes option.
 
Using the backquotes option allows you to use double quotes to brace the variable that stores the path that may also have some whitespace.

FOR /F "usebackq delims=" %%l IN ("%%f") DO (
  • Upvote 1
Link to comment
Share on other sites

 

To change behaviour of how quoting works, use the backquotes option.

 

Using the backquotes option allows you to use double quotes to brace the variable that stores the path that may also have some whitespace.

FOR /F "usebackq delims=" %%l IN ("%%f") DO (

 

Hello,

It's not working, runing the test in a virtual machine generate file content like this one:

ALLUSERSPROFILE=C:\Documents and Settings\All UsersAPPDATA=C:\Documents and Settings\Power User\Application DataCLIENTNAME=ConsoleCommonProgramFiles=C:\Program Files\Common FilesCOMPUTERNAME=POWERUSER-PCComSpec=C:\WINDOWS\system32\cmd.exeDEVMGR_SHOW_DETAILS=DEVMGR_SHOW_NONPRESENT_DEVICES=1FP_NO_HOST_CHECK=NOHOMEDRIVE=C:HOMEPATH=\Documents and Settings\Power UserLOGONSERVER=\\POWERUSER-PCnew=news tringNUMBER_OF_PROCESSORS=1old=XX XXOS=Windows_NTPath=C:\WINDOWS\system32;C:\WINDOWS;C:\WINDOWS\System32\WbemPATHEXT=.COM;.EXE;.BAT;.CMD;.VBS;.VBE;.JS;.JSE;.WSF;.WSHPROCESSOR_ARCHITECTURE=x86PROCESSOR_IDENTIFIER=x86 Family 6 Model 58 Stepping 9, GenuineIntelPROCESSOR_LEVEL=6PROCESSOR_REVISION=3a09ProgramFiles=C:\Program FilesPROMPT=$P$GSESSIONNAME=ConsoleSystemDrive=C:SystemRoot=C:\WINDOWSTEMP=C:\DOCUME~1\POWERU~1\LOCALS~1\TempTMP=C:\DOCUME~1\POWERU~1\LOCALS~1\TempUSERDOMAIN=POWERUSER-PCUSERNAME=Power UserUSERPROFILE=C:\Documents and Settings\Power Userwindir=C:\WINDOWSgdfgdfgdfnews tringgdfhfghfg

where gdfgdfgdfnews tringgdfhfghfg is the file name.

Edited by radix
Link to comment
Share on other sites

I am not understanding what/why you want to dequote.

The issue here is more like wrapping filenames with spaces into double quotes.

 

I would use something *like*:

@ECHO OFFSETLOCAL ENABLEEXTENSIONS ENABLEDELAYEDEXPANSIONSET New_string=newSET Old_string=oldFOR /F "tokens=* delims=" %%A IN ('DIR /B *.txt') DO echo Processing %%A...&CALL :do_replace "%%A"GOTO :EOF:do_replaceIF EXIST "%~nx1.new" DEL "%~nx1.new"SET tomod=0FOR /F "tokens=* delims=" %%B IN ('TYPE "%~nx1"^|FIND "%Old_string%"') DO SET tomod=1IF %tomod%==0 ECHO No match...&ECHO.&GOTO :EOFFOR /F "tokens=* delims=" %%B IN ('TYPE "%~nx1"') DO (SET Line=%%BSET ModLine=!Line:%Old_string%=%New_string%!IF NOT "!Line!"=="!Modline!" ECHO !Line!&ECHO !Modline!&ECHO.&&SET tomod=1ECHO !Modline!>>"%~nx1.new")GOTO :EOF

But actually it makes little sense, exactly because a line in a .txt file may contain any kind of special character, and there are handy and faster solution.

 

My advice is to use gsar.exe and call it a day, compare with this thread where the parsing of "plain text file" has been used (and abused):

http://www.msfn.org/board/topic/151785-how-to-merge-two-text-files/

 

jaclaz

Link to comment
Share on other sites

Here is a VBS example of moving a folder with spaces in the name to another with folder with spaces in it name.

Dim Fso :Set Fso = CreateObject("Scripting.FileSystemObject")  If Fso.FolderExists("C3 D4") Then   If Fso.FolderExists("A1 B2") Then    Fso.MoveFolder "A1 B2", "C3 D4\A1 B2"  End If End If 
  • Upvote 1
Link to comment
Share on other sites

@radix Please try to provide an full explanation of what you are trying to achieve, because it isn't possible to tell from the code you've attempted.

 

I have created a new Topic from your question since it has no specific ties with the one in which you'd posted.

Link to comment
Share on other sites

Thanks jaclaz for the neat script!

 

@radix Please try to provide an full explanation of what you are trying to achieve, because it isn't possible to tell from the code you've attempted.

 

I have created a new Topic from your question since it has no specific ties with the one in which you'd posted.

Thanks Yzöwl for creating the new thread!

 

I want to replace some unwanted characters from an output file that contains hashes for a group of big files.

I used fsum hash utility. Usually, the output looks like this:

; SlavaSoft Optimizing Checksum Utility - fsum 2.52.00337 <www.slavasoft.com>;; Generated on 03/29/15 at 20:45:38 ;778e7656fa79a073adbee3be578b50e8c58e7f5a ?SHA1*files.7z.001860993761f8420c84430d945b4f31379f49bc2f7 ?SHA1*files.7z.002ef996f72db0131b9983fb0f18f823a81418b34cc ?SHA1*files.7z.003

so, it's need some cleaning.

I used a command line like this:

SET WD=D:\New folderfsum.exe -sha1 -d"%WD%" "files*.7z.*">"%WD%\files.txt" & findstr /v ";" "%WD%\files.txt">"%WD%\files.sha1" & del /f /q "%WD%\files.txt"

The problem was ?SHA1 string.

 

The fastest (in terms of reading from storage) and cleaner (regarding the output) command line hash utility is hashutils (586 MB/s read speed from a RAID0 array made by two SSDs), but it calculate only crc32, md4, md5 and sha1. I discovered hashutils after exactfile (70 MB/s read speed from RAID0 SSD array), rhash (293 MB/s read speed from RAID0 SSD array), fsum (415 MB/s read speed from RAID0 SSD array), FileVerifier++ (386 MB/s read speed from RAID0 SSD array), powershell v4 (515 MB/s read speed from RAID0 SSD array), fciv (468 MB/s read speed from RAID0 SSD array), AutoIt _Crypt_HashFile function (175 MB/s read speed from RAID0 SSD array) and checksum from corz.org (440 MB/s read speed from RAID0 SSD array, but it's not exactly a command line utility, needing some ini file stored in %AppData% and if not find it, generate unwanted pop-ups, including opening the default web browser on their website).

From the above tools it's safe to ignore exactfile and checksum from corz.org.

Rhash support the largest number of hashes and can output in the command prompt window the calculation progress in percents, but can't use the advantage of a RAID0 array.

Edited by radix
Link to comment
Share on other sites

FileVerifier++ has many hash algos which you could try. It has a CLI named fvc.exe.
 

If I add your checksum output into a file named hash.txt and use this code:

@echo offsetlocal enabledelayedexpansionfor /f "tokens=1-2 delims= " %%A in (hash.txt) do (	set "hash=%%A"	set "file=%%B"		rem Remove ?SHA1* which is 6 characters trimmed from the 2nd token.	set "file=!file:~6!"		if not "!hash:~1!" == ";" (		echo !hash! !file!	))endlocalgoto :eof

I get an output of:

778e7656fa79a073adbee3be578b50e8c58e7f5a files.7z.001860993761f8420c84430d945b4f31379f49bc2f7 files.7z.002ef996f72db0131b9983fb0f18f823a81418b34cc files.7z.003

Seems clean AFAIK.

  • Upvote 1
Link to comment
Share on other sites

I want to replace some unwanted characters from an output file that contains hashes for a group of big files.

I used fsum hash utility. Usually, the output looks like this:

; SlavaSoft Optimizing Checksum Utility - fsum 2.52.00337 <www.slavasoft.com>;; Generated on 03/29/15 at 20:45:38 ;778e7656fa79a073adbee3be578b50e8c58e7f5a ?SHA1*files.7z.001860993761f8420c84430d945b4f31379f49bc2f7 ?SHA1*files.7z.002ef996f72db0131b9983fb0f18f823a81418b34cc ?SHA1*files.7z.003

so, it's need some cleaning.

I used a command line like this:

SET WD=D:\New folderfsum.exe -sha1 -d"%WD%" "files*.7z.*">"%WD%\files.txt" & findstr /v ";" "%WD%\files.txt">"%WD%\files.sha1" & del /f /q "%WD%\files.txt"

The problem was ?SHA1 string.

 

Unfortunately you have failed to show me what the problem is!

 

I'm guessing that there's something in the fsum output you're wanting to replace, it appears that you're hoping to replace ?SHA1 with something but you haven't confirmed that or what you're intending to replace that with!

Link to comment
Share on other sites

There's still confusion, did you want to remove ?SHA1, or as MHz's script does remove ?SHA1*

 

Also, note the emboldened text above, you're not really replacing anything, so why did you provide us with a script showing strings in new and old variables?

Link to comment
Share on other sites

There's still confusion, did you want to remove ?SHA1, or as MHz's script does remove ?SHA1*

 

Also, note the emboldened text above, you're not really replacing anything, so why did you provide us with a script showing strings in new and old variables?

I want to remove the ?SHA1 string, but I thought on using a second variable leaved blank, like in an example from stackoverflow.com.

The code from the top of the batch file:

SET WD=D:\New folderfsum.exe -sha1 -d"%WD%" "files*.7z.*">"%WD%\files.txt" & findstr /v ";" "%WD%\files.txt">"%WD%\files.sha1" & del /f /q "%WD%\files.txt"fsum.exe -sha1 -d"%WD%" "files1234*.7z.*">"%WD%\files1234.txt" & findstr /v ";" "%WD%\files1234.txt">"%WD%\files1234.sha1" & del /f /q "%WD%\files1234.txt"

calculate the sha-1 sum for the given sets of files and remove the unwanted lines from the output files, that contain the character ;

So, the entire batch file looks like this:

SET WD=D:\New folderfsum.exe -sha1 -d"%WD%" "files*.7z.*">"%WD%\files.txt" & findstr /v ";" "%WD%\files.txt">"%WD%\files.sha1" & del /f /q "%WD%\files.txt"fsum.exe -sha1 -d"%WD%" "files1234*.7z.*">"%WD%\files1234.txt" & findstr /v ";" "%WD%\files1234.txt">"%WD%\files1234.sha1" & del /f /q "%WD%\files1234.txt"CD /D "%WD%"SETLOCAL ENABLEEXTENSIONS ENABLEDELAYEDEXPANSIONSET New_string=SET Old_string=?SHA1FOR /F "tokens=* delims=" %%A IN ('DIR /B *.sha1') DO echo Processing %%A...&CALL :do_replace "%%A"GOTO :EOF:do_replaceIF EXIST "%~nx1.new" DEL "%~nx1.new"SET tomod=0FOR /F "tokens=* delims=" %%B IN ('TYPE "%~nx1"^|FIND "%Old_string%"') DO SET tomod=1IF %tomod%==0 ECHO No match...&ECHO.&GOTO :EOFFOR /F "tokens=* delims=" %%B IN ('TYPE "%~nx1"') DO (SET Line=%%BSET ModLine=!Line:%Old_string%=%New_string%!IF NOT "!Line!"=="!Modline!" ECHO !Line!&ECHO !Modline!&ECHO.&&SET tomod=1ECHO !Modline!>>"%~nx1.new")for %%f in (*.new) do move /y "%%f" "%~nx1"GOTO :EOF
Edited by radix
Link to comment
Share on other sites

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.

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...