Batch, use wildcard in set - batch-file

I want to put a batch file to modify a line in a file in a GPO.
The problem I have is the path is different for each user.
So, I tried to use a wildcard in my path but it doesnt work.
This bat is working:
#echo off &setlocal
setlocal enabledelayedexpansion
set "search=test"
set "replace=test2"
set "textfile=%appdata%\Thunderbird\Profiles\5xu9scdm.default\prefs.js"
set "newfile=%appdata%\Thunderbird\Profiles\5xu9scdm.default\prefs2.js"
(for /f "delims=" %%i in (%textfile%) do (
set "line=%%i"
set "line=!line:%search%=%replace%!"
echo(!line!
))>"%newfile%"
del %textfile%
rename %newfile% prefs.js
endlocal
But if I use a wildcard in the set, it doesnt work anymore.
#echo off &setlocal
setlocal enabledelayedexpansion
set "search=test"
set "replace=test2"
set "textfile=%appdata%\Thunderbird\Profiles\*.default\prefs.js"
set "newfile=%appdata%\Thunderbird\Profiles\*.default\prefs2.js"
(for /f "delims=" %%i in (%textfile%) do (
set "line=%%i"
set "line=!line:%search%=%replace%!"
echo(!line!
))>"%newfile%"
del %textfile%
rename %newfile% prefs.js
endlocal
How can I fix this ?

You approach does not work, because wild-cards can only occur in the last element of a path. In addition, for /F is not capable of handling wild-cards. So you need to wrap around a for /D loop to resolve the wild-cards, like this (supposing there is only a single matching directory *.default):
#echo off
setlocal EnableExtensions DisableDelayedExpansion
set "search=test"
set "replace=test2"
set "rootdir=%appdata%\Thunderbird\Profiles\*.default"
set "textfile=prefs.js"
set "newfile=prefs2.js"
for /D %%j in ("%rootdir%") do (
> "%%~j\%newfile%" (
for /F "usebackq delims=" %%i in ("%%~j\%textfile%") do (
set "line=%%i"
setlocal EnableDelayedExpansion
set "line=!line:%search%=%replace%!"
echo(!line!
endlocal
)
)
> nul move /Y "%%~j\%newfile%" "%%~j\%textfile%"
)
endlocal
exit /B
What I changed besides inserting the for /D loop:
delayed expansion is toggled within the loop, so exclamation marks in the text files are not lost;
all paths are properly quoted; to use a quoted file path in for /F, usebackq is required;
the replacement of the original file by the modified one is done by a single move command rather than del plus rename;

Related

Loop through files in folder, replace content and Save as new Name

I need to loop through filed end with .edi in my folder, replace a character in the content and then save the file in another folder with "_updated"on the end.
e.g.
C:/Test/FileName.edi replace ' in file with ^ and save the file into C:/Test/Output/FileName_Updated.edi
I've tried the following code and it works up until the saving the filename part, I've got confused somewhere, I don't usually write batch scripts:
#echo off
setlocal enabledelayedexpansion
for %%f in (C:\Test\*.edi) do (
set "input=C:\Test\"
SET "output=C:\Test\Output\"
for %%a in (%%f) do (
set "output=%output%%%~na_update.%%~xa"
)
(for /f "delims=" %%i in (%%f) do (
set "line=%%i"
setlocal enabledelayedexpansion
set "line=!line:'=^!"
echo(!line!
endlocal
)))>> %output%
)
Figured it out with help from aschipfl
#echo off
setlocal enabledelayedexpansion
for %%f in (C:\Users\CHRW\Desktop\EDILocalTest\*.edi) do (
SET "output=C:\Users\CHRW\Desktop\EDILocalTest\Output\"
for %%a in (%%f) do (
set "outputfile=!output!%%~na_update%%~xa"
)
(for /f "delims=" %%i in (%%f) do (
set "line=%%i"
setlocal enabledelayedexpansion
set "line=!line:'=^!"
echo(!line!
endlocal
))>> !outputfile!
)
#echo off
setlocal enabledelayedexpansion
for %%f in (C:\Test\*.edi) do (
set "input=C:\Test\"
SET "output=C:\Test\Output\"
(for /f "delims=" %%i in (%%f) do (
set "line=%%i"
rem setlocal enabledelayedexpansion
set "line=!line:'=^!"
echo(!line!
rem endlocal
)))>> "%output%%%~nf_update.%%~xf"
)
Since you were changing the value of output within the loop, the redirection should have been to !output! not %output% - the changed value of output, not the value of output at the time the outer for loop was parsed.
Since %%f contains a filename, there's no need to re-parse it. Easier to construct the output filename in-line
Since delayedexpansion has been invoked at the start of the procedure, there's no need to re-invoke it and close the new invocation. I've REMmed-out those lines as it may be that you are presenting abbreviated code.

Batch file to replace special characters in subfolder in windows

#echo off
FOR /f "delims=" %%G IN ('dir /a-d /b /s /o-n ^|sort /r') DO (
setlocal enabledelayedexpansion
pushd "%%~dpG"
SET Var=%%~nfG
SET Var=!Var: =_!
SET Var=!Var:[=_!
SET Var=!Var:]=_!
SET Var=!Var:(=_!
SET Var=!Var:)=_!
SET Var=!Var:,=_!
SET Var=!Var:'=_!
rename "%%~nfG" "!Var!"
popd
endlocal
)
Not working getting error in prompt like
_! was unexpected at this time.
Can anyone answer or correct this plz.enter code here
Also I'd compare if the name is changed before trying to rename:
:: Q:\Test\2017\08\05\SO_45521689.cmd
#echo off
FOR /f "delims=" %%G IN (
'dir /a-d /b /s /o-n ^|findstr "[,'()\[\]]"^|sort /r'
) DO (
setlocal enabledelayedexpansion
pushd "%%~dpG"
SET "Var=%%~nfG"
SET "Var=!Var: =_!"
SET "Var=!Var:[=_!"
SET "Var=!Var:]=_!"
SET "Var=!Var:(=_!"
SET "Var=!Var:)=_!"
SET "Var=!Var:,=_!"
SET "Var=!Var:'=_!"
if "%%~nfG" neq "!Var!" rename "%%~nfG" "!Var!"
popd
endlocal
)
SET Var=!Var:)=_!
rem ↑ this closing parenthesis closes the `... DO ()` code block.
Use
SET "Var=!Var:)=_!"
or
SET Var=!Var:^)=_!
Moreover, you cannot specify a drive or path for rename target. Use SET "Var=%%~nxG" instead of SET Var=%%~nfG.
Be aware of that your script ingurgitates all exclamation marks if a file name contains any.
The following script should do the job keeping delayed expansion disabled. Please note that operational rename command is merely ECHOed for debugging purposes.
#ECHO OFF
SETLOCAL EnableExtensions DisableDelayedExpansion
rem choose initial directory
pushd "D:\bat\Unusual Names"
FOR /f "delims=" %%G IN ('dir /a-d /b /s /o-n') DO (
SET "_FFN=%%~fG" File Full Name
SET "Var=%%~nxG" File Name + Extension
call :renaming
)
popd
ENDLOCAL
goto :eof
:renaming
SET "Var=%Var: =_%"
SET "Var=%Var:[=_%"
SET "Var=%Var:]=_%"
SET "Var=%Var:(=_%"
SET "Var=%Var:)=_%"
SET "Var=%Var:,=_%"
SET "Var=%Var:'=_%"
ECHO rename "%_FFN%" "%Var%"
goto :eof

bat script for replacing strings creates empty file

I'm using this bat file to modify a value in a web.config from httpTransport to httpsTransport. It works well if I direct my output to another file. If I try to overwrite my file it creates an empty file.
#echo off &setlocal
set "search=httpsTransport"
set "replace=http123Transport"
set INTEXTFILE=D:\teste_bat\Web.config
set OUTTEXTFILE=D:\teste_bat\WebTemp.config
(for /f "delims=" %%i in ('findstr "^" "%INTEXTFILE%"') do (
set "line=%%i"
setlocal enabledelayedexpansion
set "line=!line:%search%=%replace%!"
echo(!line!
endlocal
))>>"%OUTTEXTFILE%"
del %INTEXTFILE%
rename %OUTTEXTFILE% %INTEXTFILE%
Any help would e apreciated
The following code fails in case both INTEXTFILE and OUTTEXTFILE point to the same file, because the output redirection > prepares the output file at the beginning, so it creates an empty file, which is then read by findstr:
set "INTEXTFILE=D:\teste_bat\Web.config"
set "OUTTEXTFILE=D:\teste_bat\Web.config"
> "%OUTTEXTFILE%" (
for /f "delims=" %%i in ('findstr "^" "%INTEXTFILE%"') do (
set "line=%%i"
setlocal enabledelayedexpansion
set "line=!line:%search%=%replace%!"
echo(!line!
endlocal
)
)
Replacing > by >> does also not work, because this appends the new data to the original file.
To overcome this, you have got two options:
To write to a different file and to replace the original file by the new one at the end:
set "INTEXTFILE=D:\teste_bat\Web.config"
set "OUTTEXTFILE=D:\teste_bat\WebTemp.config"
> "%OUTTEXTFILE%" (
for /f "delims=" %%i in ('findstr "^" "%INTEXTFILE%"') do (
set "line=%%i"
setlocal enabledelayedexpansion
set "line=!line:%search%=%replace%!"
echo(!line!
endlocal
)
)
move /Y "%OUTTEXTFILE%" "%INTEXTFILE%"
This is the recommended variant due to better performance.
To ensure that the file is read before the output redirection is applied:
set "INTEXTFILE=D:\teste_bat\Web.config"
set "OUTTEXTFILE=D:\teste_bat\Web.config"
for /f "delims=" %%i in ('findstr "^" "%INTEXTFILE%" ^& ^> "%OUTTEXTFILE%" rem/') do (
set "line=%%i"
setlocal enabledelayedexpansion
set "line=!line:%search%=%replace%!"
>> "%OUTTEXTFILE%" echo(!line!
endlocal
)
This is worse in performance since there are multiple file access operations (appending to the file per each loop iteration due to >>), but it prevents the need of a temporary file. The portion > "%OUTTEXTFILE%" rem/ depletes the file after being read by findstr, then it is appended to later in the loop body.

How to handle ! with enabledelayedexpansion in batch

I am trying to read a file using enabledelayedexpansion .File contains special character.Because of enabledelayedexpansion ! and text after that is ignored .Please suggests some way by which this issue can be resolved .
note :- i have to use enabledelayedexpansion .
`
#echo off
set "search1='"
set "search2=""
set "search3=&"
set "search4=<"
set "search5=>"
set "search6=!"
set "replace1=&apos;"
set "replace2=""
set "replace3=&"
set "replace4=<"
set "replace5=>"
set "replace6=^!"
setlocal enabledelayedexpansion
set "textfile=!input!"
set "newfile=!input!1"
echo !textfile!
echo !newfile!
break>"!newfile!"
(for /f "delims=" %%i in (!textfile!) do (
setlocal enabledelayedexpansion
set "line=%%i"
for /f "usebackq tokens=1,2 delims=$" %%a in ('"!search3!"$"!replace3!"') do set "line=!line:%%~a=%%~b!"
for /f "usebackq tokens=1,2 delims=$" %%a in ('"!search4!"$"!replace4!"') do set "line=!line:%%~a=%%~b!"
for /f "usebackq tokens=1,2 delims=$" %%a in ('"!search5!"$"!replace5!"') do set "line=!line:%%~a=%%~b!"
for /f "usebackq tokens=1,2 delims=$" %%a in ('"!search2!"$"!replace2!"') do set "line=!line:%%~a=%%~b!"
for /f "usebackq tokens=1,2 delims=$" %%a in ('"!search1!"$"!replace1!"') do set "line=!line:%%~a=%%~b!"
setlocal enabledelayedexpansion
echo(!line!
endlocal
))>>"!newfile!"
Above is just part of script. textfile will contain file path.rest all substitution are working fine only in case of ! its causing issue
To read content from a file without destroying it you need to use the delayed toggling technic or reading it with set /p.
setlocal DisableDelayedExpansion
(
for /f "delims=" %%i in (textfile.txt) do (
set "line=%%i" -- This must be done while delayed expansion is disabled
setlocal EnableDelayedExpansion
set "line=!line:<=<!"
echo(!line!
endlocal
)
) > outfile.txt
The toggeling is necessary, because the expansion of a FOR loop variable is only safe in the disabled delayed expansion mode, else exclamation marks are removed/interpreted.
Reading a file via SET /P is another way to avoid the problems related to Delayed Expansion that may be simpler than FOR /F. I also show you a simpler way to define the set of replacements via an array
#echo off
setlocal EnableDelayedExpansion
rem Define the set of replacements
set "replacA[&]=&"
set "replace[']=&apos;"
set replace["]="
set "replace[<]=<"
set "replace[>]=>"
REM set "replace[!]=^!" // Not needed
set "textfile=!input!"
set "newfile=!input!1"
echo !textfile!
echo !newfile!
call :ProcessFile < "!textfile!" > "!newfile!"
goto :EOF
:ProcessFile
set empty=0
set "line="
:emptyLine
set /P line=
if not defined line (
set /A empty+=1
if !empty! gtr 2 exit /B
echo/
goto emptyLine
)
for /F "tokens=2,3 delims=[]=" %%a in ('set replac') do set "line=!line:%%a=%%b!"
echo !line!
goto ProcessFile
The most complex part in :ProcessFile subroutine is just used to preserve a maximum of 2 empty lines together (that may easily be increased to more lines, if needed).
Edit: completely overwritten. I could either keep an ! exclamation mark in output or replace it by ^^! (or ^^^^!, ^^^^^^! etc.) with even number of ^^ carets as follows:
#ECHO OFF >NUL
SETLOCAL enableextensions enabledelayedexpansion
set "textfile=files\31130282textfile.txt"
set "newfile=files\31130282textfileNew.txt"
set "line="
set "quote=""
setlocal DisableDelayedExpansion
set "exclamation=!"
set "exclamationNew=%~1!"
(for /f "delims=" %%i in (%textfile%) do (
set "line=%%i"
SETLOCAL enabledelayedexpansion
set "line=!line:&=&!"
set "line=!line:<=<!"
set "line=!line:>=>!"
call set "line=%%line:!quote!="%%"
set "line=!line:'=&apos;!"
call set "line=%%line:!exclamation!=!exclamationNew!%%"
echo(!line!
ENDLOCAL
))>"%newfile%"
endlocal
type "%newfile%"
Output:
==>type files\31130282textfile.txt"
<'abc'>&"cde"T
"<x!y!z!>"
==>D:\bat\SO\31130282.bat ""
<&apos;abc&apos;>&"cde"T
"<x!y!z!>"
==>D:\bat\SO\31130282.bat "^"
<&apos;abc&apos;>&"cde"T
"<x^^!y^^!z^^!>"
==>D:\bat\SO\31130282.bat "^^"
<&apos;abc&apos;>&"cde"T
"<x^^^^!y^^^^!z^^^^!>"

Batch script to find and replace a string in text file without creating an extra output file for storing the modified file

I have written a batch script to find and replace a string in a text file. Following is my script.
#echo off &setlocal
set "search=%1"
set "replace=%2"
set "textfile=Input.txt"
set "newfile=Output.txt"
(for /f "delims=" %%i in (%textfile%) do (
set "line=%%i"
setlocal enabledelayedexpansion
set "line=!line:%search%=%replace%!"
echo(!line!
endlocal
))>"%newfile%"
del %textfile%
rename %newfile% %textfile%
I am able to replace the word successfully.
But i dont want to create Output.txt and then rename it the original file..
Please help me out for editing a text file without redirecting the output to a new file..
#echo off
setlocal enableextensions disabledelayedexpansion
set "search=%1"
set "replace=%2"
set "textFile=Input.txt"
for /f "delims=" %%i in ('type "%textFile%" ^& break ^> "%textFile%" ') do (
set "line=%%i"
setlocal enabledelayedexpansion
>>"%textFile%" echo(!line:%search%=%replace%!
endlocal
)
for /f will read all the data (generated by the type comamnd) before starting to process it. In the subprocess started to execute the type, we include a redirection overwritting the file (so it is emptied). Once the do clause starts to execute (the content of the file is in memory to be processed) the output is appended to the file.

Resources