I've been trying to make a batch file that makes folders named after the people on a .txt file list, and then gives them full access to modifying their own personal folder.
The problem is that I keep getting a ' delims= " was unexpected at this time' error.
Here's my code here, I was wondering if you guys would be able to find out what I did wrong, thanks ^-^
(Btw I haven't added the permissions part yet, I just need to get this part sorted out first)
CODE: http://pastebin.com/XLi11nZa
NAMES LIST: http://pastebin.com/xbh3WTSv
#echo off
color A
echo What is the name of list file? (Do not include format)
SET /P list=
setlocal EnableDelayedExpansion
set "cmd=findstr /R /N "^^" %list%.txt | find /C ":""
for /f %%a in ('!cmd!') do set m=%%a
SET c=0
echo !m! folders to be created. Continue? (Y/N)
SET /P ANSWER=
if /i {%ANSWER%}=={y} (goto :yes)
if /i {%ANSWER%}=={yes} (goto :yes)
exit
:yes
echo Now creating %m% folders.....
for /f "eol=; tokens=1 delims=," %%i in ("%list%.txt") do (
SET /a c = !c! + 1
mkdir "%%i"
echo !c!/%m% folders created [%%i]
)
endlocal
echo Now adding permissions to %m% folders.....
pause
setlocal enabledelayedexpansion
SET c1=0
for /f "eol=; tokens=1 delims=," %%i in ("%list%.txt") do (
SET /a c1 = !c1! + 1
SET word=1
SET /a showme=c1-1
SET showme=skip=%showme%
IF !c1! equ 1 set "showme= "
FOR /F "tokens=%word% %showme% delims= " %%F IN ("%list%") DO if defined
showme set showme=%%F
SET first=%showme:~0,1%
SET word=2
SET /a showme1=c1-1
SET showme1=skip=%showme1%
IF %c1% equ 1 set "showme1= "
FOR /F "tokens=%word% %showme1% delims= " %%L IN ("%list%") DO if
defined showme1 set showme1=%%L
set B=%showme1%%first%
set _STRING=%B%
set "_UCASE=ABCDEFGHIJKLMNOPQRSTUVWXYZ"
set "_LCASE=abcdefghijklmnopqrstuvwxyz"
for /l %%a in (0,1,25) do (
call set "_FROM=%%_UCASE:~%%a,1%%
call set "_TO=%%_LCASE:~%%a,1%%
call set "_STRING=%%_STRING:!_FROM!=!_TO!%%
)
set _STRING
echo %_STRING%
echo %_STRING%>>testing.txt
endlocal
pause
)
names list
Loralee Stucky
Tomas Silberman
Marleen Rosell
Phyllis Steier
Elmo Jetter
Kristyn Spruell
Willetta Vandermeer
Hazel Alsobrook
Naida Nixon
Nadia Godfrey
Lavonna Antunez
Mac Castile
Tamela Stover
Piedad Heidrick
Hien Welsh
Carolin Gularte
Mariko Tolentino
Alia Graddy
Deadra Rehkop
Donella Pittman
Replace
for /f "eol=; tokens=1 delims=," %%i in ("%list%.txt") do (
by
for /f "usebackq eol=; tokens=1 delims=," %%i in ("%list%.txt") do (
twice.
Without the usebackq option, a double-quoted set of for /F is interpreted as a literal string rather than a file. Removing the double-quotes could work but could also lead to problems with files that have spaces in their names.
Another thing: you are dynamically creating the skip=# option for for /F, where # stands for a number. you have to make sure that this number is always positive, so 0 is not understood by for /F which could also lead to your error message.
So you could add an if check whether the number is greater than zero and do not add the skip option otherwise (by clearing your showme variables).
And last but not least: the delayed expansion is not always used properly: sometimes in the block startung at the for command in line #26 of your code and reaching to the end, you are not consistently using !! for expansion of variables showme, showme1 and c1, which are the ones that are modified within that code block.
The folders will get created proper if you remove the double quotes from the filename variable for /f "eol=; tokens=1 delims=," %%i in (%list%.txt) do ( change that line and you'll get past this part.
You may want to do the same in the permissions section.
Related
I have a file rev.ini having multiple variable to update:
s1=10
s2=20
s3=30
Here I am using separate loop for finding string in a file. there are 3 times loop are running for same file. Is it possible to find these three string in same loop?
#Echo Off
cd /d D:\xyz
setlocal enabledelayedexpansion
set s1=10
set s2=11
set s3=12
set "file=rev.ini"
for /F "tokens=1,* delims==" %%i in ('findstr "s1= " rev.ini') do (
set "versionVar=%%~i"
set "versionVal=%%~j"
set sequence=%s1%
)
for /f "tokens=1,*delims=]" %%i in ('type "%file%" ^| find /v /n "" ^& break^>%file%') do (
set "line=%%j"
if "!line!" == "!versionVar!=!versionVal!" set line=!versionVar!=!sequence!
echo(!line!>>!file!
)
for /F "tokens=1,* delims==" %%i in ('findstr "s2= " rev.ini') do (
set "versionVar=%%~i"
set "versionVal=%%~j"
set sequence=%s2%
)
for /f "tokens=1,*delims=]" %%i in ('type "%file%" ^| find /v /n "" ^& break^>%file%') do (
set "line=%%j"
if "!line!" == "!versionVar!=!versionVal!" set line=!versionVar!=!sequence!
echo(!line!>>!file!
)
for /F "tokens=1,* delims==" %%i in ('findstr "s3= " rev.ini') do (
set "versionVar=%%~i"
set "versionVal=%%~j"
set sequence=%s3%
)
for /f "tokens=1,*delims=]" %%i in ('type "%file%" ^| find /v /n "" ^& break^>%file%') do (
set "line=%%j"
if "!line!" == "!versionVar!=!versionVal!" set line=!versionVar!=!sequence!
echo(!line!>>!file!
)
Goto :EOF
#ECHO OFF
SETLOCAL ENABLEDELAYEDEXPANSION
rem The following settings for the source directory and filename are names
rem that I use for testing and deliberately include names which include spaces to make sure
rem that the process works using such names. These will need to be changed to suit your situation.
SET "sourcedir=u:\your files"
SET "filename1=%sourcedir%\q74276740.txt"
SET "outfile=outfile.txt"
set "s1=10"
set "s2=11"
set "s3=12"
REM (
FOR /f "usebackqtokens=1*delims==" %%b IN ("%filename1%") DO (
SET "#keep=Y"
FOR /f "tokens=1*delims==" %%u IN ('set') DO (IF /i "%%b"=="%%u" SET "#keep="&ECHO %%b=%%v)
IF DEFINED #keep IF "%%c"=="" (ECHO %%b) ELSE (ECHO %%b=%%c)
)
REM )>"%outfile%"
GOTO :EOF
Always verify against a test directory before applying to real data.
Note that if the filename does not contain separators like spaces, then both usebackq and the quotes around %filename1% can be omitted.
Simply read each line. If the line contains = and the part before the = contains any variablename in the environment (I made it case-insensitive with /i) then generate a line using the matching value. If #keep remains set after the for..%%u has been executed, then there is no match, so either reproduce the original x=y or the original line, if it had no =.
The output file can be generated by removing the rem before the ( and ).
The output file must be different from the input file, and should be moved over the input file when the batch finishes (not shown)
--- After comment ---
So, if you follow the instructions provided in the last two paragraphs, you get
#ECHO OFF
SETLOCAL ENABLEDELAYEDEXPANSION
rem The following settings for the source directory and filename are names
rem that I use for testing and deliberately include names which include spaces to make sure
rem that the process works using such names. These will need to be changed to suit your situation.
SET "sourcedir=u:\your files"
SET "filename1=%sourcedir%\q74276740.txt"
SET "outfile=outfile.txt"
set "s1=10"
set "s2=11"
set "s3=12"
(
FOR /f "usebackqtokens=1*delims==" %%b IN ("%filename1%") DO (
SET "#keep=Y"
FOR /f "tokens=1*delims==" %%u IN ('set') DO (IF /i "%%b"=="%%u" SET "#keep="&ECHO %%b=%%v)
IF DEFINED #keep IF "%%c"=="" (ECHO %%b) ELSE (ECHO %%b=%%c)
)
)>"%outfile%"
move "%outfile%" "%filename1%"
GOTO :EOF
== revision of processing section in light of new requirement to retain empty lines ==
REM (
FOR /f "tokens=1,2*delims=:=" %%g IN ('findstr /n ^^^^ "%filename1%"') DO IF "%%h"=="" (ECHO.) ELSE (
SET "#keep=Y"
FOR /f "tokens=1*delims==" %%u IN ('set') DO (IF /i "%%h"=="%%u" SET "#keep="&ECHO %%h=%%v)
IF DEFINED #keep IF "%%i"=="" (ECHO %%h) ELSE (ECHO %%h=%%i)
)
REM )>"%outfile%"
Once again, the rem keywords need to be removed to output to the nominated file.
The changes simply feed the file through a findstr command which produces a listing of the file with a prefix of linenumber:. The extra : in the delims causes the line number to be parsed to %%g. I've changed the metavariable letters because three are required for the processing, and I prefer to not use letters that are also modifiers.
If findstr produces only a line number then it was a blank line, so %%h will be empty and we simply produce an empty line, otherwise, processing as before.
Let's say we have a variable that contains multiple lines of text containing full file names, such as
C:\Program Files\gs\gs9.26\Resource\CMap\Adobe-Japan2-0
C:\Program Files\Inkscape\share\poppler\cMap\Adobe-Japan2\Adobe-Japan2-0
...and this multi-line text is assigned to the variable var.
In Windows/DOS command prompt (not PowerShell), without a use of any external tools, how can I remove everything after the last occurrence of the character \ in the first line, i.e. for the above example to get only
C:\Program Files\gs\gs9.26\Resource\CMap\
Here is a part of the code I use:
#echo off
setlocal EnableExtensions EnableDelayedExpansion
for /f %%x in ('copy /z "%~dpf0" nul') do set "CR=%%x"
(set LF=^
%==%
)
set TAB= "
set "TAB=%TAB:"=%
net session >nul 2>&1
if !errorLevel! NEQ 0 (
echo ERROR: Use "Run as administrator" to execute this file.!LF!
pause
exit /b 1
)
<nul set/p"=Analyzing... "
set "nil= "
for /l %%i in (2,1,80) do call set "nil=%%nil%%%nil%"
set "STDOUT="
set "STDERR="
for /f "delims=" %%E in ('
2^>^&1 ^(^
for /f "delims=" %%O in ^('^
where /R "%programfiles%" "*adobe*.*"
^^^^^^^| findstr /i /n /r "japan2"^
'^) do #^(^
echo ^^^^^^^|%%O^
^)^
^) ^| findstr /n /r "^"
') do (
set "LINE=%%E"
set "LINE=!LINE:*:=!"
if "!LINE:~,1!" == "|" (
set "STDOUT=!STDOUT!!LINE:*:=!!LF!"
) else (
set "STDERR=!STDERR!!LINE!!LF!"
)
)
The original code created trailing whitespace to each path it found. I modified the code to get rid of this.
The paths had to be double quote as they contain whitespace in the path as well. The variable !STDOUT! with therefore have the following format:
"C:\Program Files\gs\gs9.26\Resource\CMap\Adobe-Japan2-0"
"C:\Program Files\Inkscape\share\poppler\cMap\Adobe-Japan2\Adobe-Japan2-0"
You mentioned that this is only part of the code, so you need to make sure that everything else works as expected.
Note however that you need to copy this part of the code "exactly" as is as I have amended a few parts of it, if you don't, it will not work as expected.
Lastly, I have to add. I am not sure of the reason this code was designed like this, I cannot see any specific reason, based on the current scenario.
This code is a bit of an overkill for now, unless you can provide a specific reason. I do not see any reason why the variable cannot be set without the line feeds.
Anyway, here it is:
#echo off
setlocal EnableExtensions EnableDelayedExpansion
for /f %%x in ('copy /z "%~dpf0" nul') do set "CR=%%x"
(set LF=^
%==%
)
set TAB= "
set "TAB=%TAB:"=%
net session >nul 2>&1
if !errorLevel! NEQ 0 (
echo ERROR: Use "Run as administrator" to execute this file.!LF!
pause
exit /b 1
)
<nul set/p"=Analyzing... "
echo(
set "nil= "
for /l %%i in (2,1,80) do call set "nil=%%nil%%%nil%"
set "STDOUT="
set "STDERR="
for /f "delims=" %%E in ('
2^>^&1 ^(^
for /f "delims=" %%O in ^('^
where /R "%programfiles%" "*adobe*.*"
^^^^^^^| findstr /i /n /r "japan2"^
'^) do #^(^
echo ^^^^^^^|%%O^
^)^
^) ^| findstr /n /r "^"
') do (
set "LINE=%%E"
set "LINE=!LINE:*:=!"
set "LINE=!LINE: =!"
if "!LINE:~,1!" == "|" (
set STDOUT=!STDOUT!"!LINE:*:=!"!LF!
) else (
set "STDERR=!STDERR!!LINE!!LF!"
)
)
for %%i in (!STDOUT!) do set "dir=%%~dpi" & goto cont:
:cont
echo !dir!
The main changes in the code you provided are here:
set "LINE=!LINE: =!" :: Removed the two trailing whitespaces
if "!LINE:~,1!" == "|" (
set STDOUT=!STDOUT!"!LINE:*:=!"!LF! :: Double quote the line so we can format them correctly.
This will allow proper usage of the strings in a list form.
Then, my code added. This sets a pth variable using drive and path to file. We then strip the trailing \ to allow the next %%~dpa to assign the drive and path to the point where you wanted it.
for %%i in (!STDOUT!) do set "dir=%%~dpi" & goto cont:
:cont
echo !dir!
The result will then be:
C:\Program Files\gs\gs9.26\Resource\CMap\
Finally, if you want to see that my part of the code does in fact remove the parts from path as you wanted, you can do this in my code section:
for %%i in (!STDOUT!) do (
echo %%~dpi
)
Or if you want to set each of them to a dir[n] to resemble an array, then place this in my code section:
set _n=1
for %%i in (!STDOUT!) do (
set "dir[!_n!]=%%~dpi"
set /a _n+=1
)
for /l %%n in (0,1,!_n!) do echo(!dir[%%n]!
set dir
I wrote this code based on some other examples but just can't get it to work? (it's a .bat file)? The code writes the new file with all the old lines just won't edit the three lines right with the "=" character. Can someone point me in the right direction please.
This is what the INTOUCH.INI file looks like to start:
[InTouch]
AppMode=2
AppName0=test
AppName1=
AppName2=
AppName3=
AppDesc0=New InTouch application
AppDesc1=
AppDesc2=
AppDesc3=
SAOConverted=1
WinFullScreen=1
WinLeft=-4
WinTop=-4
WinWidth=1032
WinHeight=748
UseNewSendKeys=1
DebugScripts=0
UseBigBitmap=1
WindowViewerStartupIconic=0
CloseOnTransfer=0
And this is what is written:
[InTouch]
AppMode=2
AppName0=test
AppName1=
AppName2=
AppName3=
AppDesc0=New InTouch application
AppDesc1=
AppDesc2=
AppDesc3=
SAOConverted=1
1=WinFullScreen=0=1
WinLeft=-4
WinTop=-4
1032=WinWidth=1000=1032
748=WinHeight=700=748
UseNewSendKeys=1
DebugScripts=0
UseBigBitmap=1
WindowViewerStartupIconic=0
CloseOnTransfer=0
This is my .bat file code:
Set "OldString1=WinFullScreen=1"
Set "NewString1=WinFullScreen=0"
Set "OldString2=WinWidth=1032"
Set "NewString2=WinWidth=1000"
Set "OldString3=WinHeight=748"
Set "NewString3=WinHeight=700"
#ECHO OFF &SETLOCAL
cd /d F:\
for %%x in (INTOUCH.INI) do call:process "%%~x"
goto:eof
:process
set "outFile=%~n1_edited%~x1"
(for /f "skip=2 delims=:" %%a in ('find /n /v "" "INTOUCH.INI"') do (
set "ln=%%a"
Setlocal enableDelayedExpansion
set "ln=!ln:*]=!"
if defined ln (
set "ln=!ln:%OldString1%=%NewString1%!"
set "ln=!ln:%OldString2%=%NewString2%!"
set "ln=!ln:%OldString3%=%NewString3%!"
)
echo(!ln!
endlocal
))>"%outFile%"
Exit /b
If you use the file's format to your advantage you can set the values of the new variables at the top of the script and then as you are reading the variable names from the settings file you can see if those variables are defined. If they are defined then output the new value, otherwise output the original value.
The trick to this is the double variable expansion you get when you use the CALL and ECHO commands together. First the for variable is expanded the name of the variable and then in the second phase of expansion the value of the variable is then expanded. That is the reason for the extra sets of percent symbols.
#echo off
Set "WinFullScreen=0"
Set "WinWidth=1000"
Set "WinHeight=700"
REM cd /d F:\
for %%F in (INTOUCH.INI) do set "outFile=%%~nF_edited%%~xF"
REM Read first line of file
set /p line1=<INTOUCH.INI
(echo %line1%
for /f "usebackq skip=1 tokens=1,2 delims==" %%G in ("INTOUCH.INI") do (
if defined %%G (
CALL echo %%G=%%%%G%%
) else (
echo %%G=%%H
)
))>"%outFile%"
Exit /b
#echo off
Set "AppMode=x"
Set "WinFullScreen=0"
Set "WinWidth=1000"
Set "WinHeight=700"
for /f "skip=1 usebackq tokens=1,2 delims==" %%G in ("a.INI") do call :proc "%%G" %%H
exit /b
:proc
set val=%2
for /F "tokens=* eol= " %%S in ("%~1") do set trimmed=%%S
call :getoverrideval %trimmed%
if "%override%" == "" (
echo %~1=%2%
) else (
echo %~1=%override%
)
goto :EOF
:getoverrideval
call set override=%%%1%%
Output:
C:\Users\w16coreeval>cmd /c a.bat
AppMode=x
AppName0=test
AppName1=
AppName2=
AppName3=
AppDesc0=New
AppDesc1=
AppDesc2=
AppDesc3=
SAOConverted=1
WinFullScreen=0
WinLeft=-4
WinTop=-4
WinWidth=1000
WinHeight=700
UseNewSendKeys=1
DebugScripts=0
UseBigBitmap=1
WindowViewerStartupIconic=0
CloseOnTransfer=0
I have some files in the form:
filename1 1 extra1.ext
filename1 2.ext
filename1 3 extra2.ext
...
filename2 1.ext
filename2 100 extra3.ext
...
filename20 1.ext
filename20 15 extra100.ext
(etc.)
...where filename1, filename2, etc., can contain spaces, symbol ' but not numbers. And extra1, extra2, etc, can contain anything. The number in the file name enclosed by spaces does not repeat per same filename1, filename2, etc.
What i want is to remove the extra things of the files that contain it. That is, to get from filename20 15 extra100.ext to filename20 15.ext
My first attempt is this:
#echo off
setlocal EnableExtensions EnableDelayedExpansion
set "FILE=file name 11 con sosas extras 2.txt"
set "ext=txt"
set "folder=."
for /F "tokens=1,* delims=0123456789" %%A in ("!FILE!") do (set "EXTRA=%%B")
set "FIRST=!FILE:%EXTRA%=!"
set "filename=!FIRST!.!ext!"
echo !EXTRA!
echo !filename!
echo rename "!folder!\!FILE!" "!filename!"
that seems to work, but if i change it to receive parameters, it doesn't:
#echo off
setlocal EnableExtensions EnableDelayedExpansion
set "FILE=%1"
set "ext=%2"
set "folder=%3"
for /F "tokens=1,* delims=0123456789" %%A in ("!FILE!") do (set "EXTRA=%%B")
set "FIRST=!FILE:%EXTRA%=!"
set "filename=!FIRST!.!ext!"
echo !EXTRA!
echo !filename!
echo rename "!folder!\!FILE!" "!filename!"
where %1 is the filename, %2 is the extension and %3 is the folder in which the files are. Probably, the extension can be extracted inside the batch, but i don't know how to do it.
On another hand, i plan to use this batch into another one. There, there will be a for loop in (*.txt) and i don't know how to differentiate between files that have extra things (and then call this batch) from files that doesn't (and then not call this batch).
Regards,
use your method to extract the "extra-portion". In a second step, remove that extra-portion:
#echo off
setlocal enabledelayedexpansion
set "FILE=file name 11 con sosas extras 2.txt"
for /f "tokens=1,* delims=1234567890" %%a in ("%file%") do set new=!file:%%b=!%%~xb
echo %new%
%%~xb gives you the extension.
Here is a batch script that seeks the first purely numeric string portion enclosed within SPACEs, or in case it appears at the end, preceded by a SPACE, that occurs after some other text not consisting of SPACEs only. The part in front of the found number followed by a SPACE followed by the number itself are used for building the new file name.
This approach handles all valid characters for file names properly, even ^, &, %, !, ( and ).
So here is the code:
#echo off
setlocal EnableExtensions DisableDelayedExpansion
rem // Define constants here:
set "_SOURCE=.\test"
for /F "eol=| delims=" %%F in ('
dir /B "%_SOURCE%\*.ext" ^| findstr /R /I ^
/C:"^..* [0123456789][0123456789]*\.ext$" ^
/C:"^..* [0123456789][0123456789]* .*\.ext$"
') do (
set "FILE=%%F"
call :SPLIT FIRST NUM REST "%%~nF"
if defined NUM (
setlocal EnableDelayedExpansion
ECHO rename "!_SOURCE!\!FILE!" "!FIRST! !NUM!%%~xF"
endlocal
)
)
endlocal
exit /B
:SPLIT rtn_first rtn_num rtn_rest val_string
setlocal DisableDelayedExpansion
set "RMD=" & set "NUM=" & set "STR=%~4"
:LOOP
for /F "tokens=1,2,* delims= " %%I in ("%STR%") do (
if not "%%J"=="" (
(for /F "delims=0123456789" %%L in ("%%J") do rem/) && (
if not "%%K"=="" (
set "STR=%%J %%K"
goto :LOOP
)
) || (
set "NUM=%%J"
if not "%%K"=="" (
set "RMD=%%K"
)
)
)
)
set "STR=%~4"
if not defined NUM goto :QUIT
set "STR=%STR% "
call set "STR=%%STR: %NUM% =|%%"
for /F "delims=|" %%L in ("%STR:^^=^%") do set "STR=%%L"
:QUIT
(
endlocal
set "%~1=%STR%"
set "%~2=%NUM%"
set "%~3=%RMD%"
)
exit /B
After having tested the script, remove the upper-case ECHO command to actually rename any files.
I have written a batch file which I want to overwrite key strings with strings from another .txt file.
currently it copies the new File.txt file perfectly but does not replace the strings with the strings from OldFile.txt file.
example of strings in File.txt file:
...
# Password
Pword=
# AccountName
Account=
# TownName
Town=
# Postcode
Postcode=
# LocationChangedDate
LocationChanged=
example of strings in OldFile.txt file I want to replace from:
...
# Password
Pword=ABC
# AccountName
Account=123
# TownName
Town=LDN
# Postcode
Postcode=WS77TP
# LocationChangedDate
LocationChanged=01/01/2015
Can someone please point me in the right direction or explain where I have made a mistake?
#echo off
setlocal disableDelayedExpansion
::Variables
set InputFile=F:\EXCHANGE\3\Machine\File.txt
set OutputFile=F:\EXCHANGE\3\File-New.txt
set CopyFile=F:\EXCHANGE\3\OldMachine\OldFile.txt
set _strFindPword=Pword=.*
for /F "delims=" %%A in ('findstr /x "Pword=.*" %CopyFile%') do set _strInsertPword=%%A
echo.%_strInsertPword%
set _strFindAccount=Account=.*
for /F "delims=" %%B in ('findstr /x "Account=.*" %CopyFile%') do set _strInsertAccount=%%B
echo.%_strInsertAccount%
set _strFindTown=Town=.*
for /F "delims=" %%C in ('findstr /x "Town=.*" %CopyFile%') do set _strInsertTown=%%C
echo.%_strInsertTown%
set _strFindLocationChanged=LocationChanged=.*
for /F "delims=" %%D in ('findstr /x "LocationChanged=.*" %CopyFile%') do set _strInsertLocationChanged=%%D
echo.%_strInsertLocationChanged%
set _strFindPostcode=Postcode=.*
for /F "delims=" %%E in ('findstr /x "Postcode=.*" %CopyFile%') do set _strInsertPostcode=%%E
echo.%_strInsertPostcode%
(
for /F "delims=" %%L in ('findstr /n "^" "%InputFile%"') do (
set "line=%%L"
setlocal EnableDelayedExpansion
set "line=!line:*:=!"
if "%%L" equ "_strFindPword" (echo.!_strInsertPword!) else (
if "%%L" equ "%_strFindAccount%" (echo.!_strInsertAccount!) else (
if "%%L" equ "%_strFindTown%" (echo.!_strInsertTown!) else (
if "%%L" equ "%_strFindLocationChanged%" (echo.!_strInsertLocationChanged!) else (
if "%%L" equ "%_strFindPostcode%" (echo.!_strInsertPostcode!) else (echo.!line!)
)
)
)
)
endlocal
)
) > "%OutputFile%"
del %InputFile%
ren %OutputFile% File.txt
pause
I think I finally got it...
What it does:
It goes through the OldFile.txt content, searching for markers, if found they are stored into environment variables to be used in the nest step (e.g. for _PWD marker (variable) which has a value of Pword=, it will create a _PWDCONTENTS variable with the content of Pword=ABC).
It goes through File.txt content, searching for the same markers, if one marker found, the corresponding CONTENTS variable is dumped in the OutFile.txt, else the original line. Because that happens in the inner for loop, I had to add some extra logic (the _WROTE var) to avoid writing the same lines more than once.
Notes:
It is supposed (well, besides doing what it's supposed to) to be "configurable" (the code is complicated, it's heading towards meta :) if you will), meaning that if there are changes between the markers the code shouldn't change (well there would be code changes, but not in the functional part only in variable definitions). Let me detail:
If you no longer need to replace the Town= string, then all you have to do is removing _TOWN from _ALL: set _ALL=_PWD _ACCT _POST _LOC.
The reverse: if you want to add some other tag (let's call it Name), you have to create a new environment variable: set _NAME=Name= and add it to _ALL: set _ALL=_PWD _ACCT _TOWN _POST _LOC _NAME.
As an indirect consequence, I didn't focus on performance, so it might run slow. Anyway I tried to keep the disk accesses (which are painfully slow) to a minimum (one example is when having 2 for loops the one that iterates on a file contents - assuming that each iteration takes a disk access; this might not be true, and Win has IO buffering - it's the outer one).
I "commented" out the last line in the file, to avoid overwriting the original file. If that behavior is needed, simply remove the rem at the beginning.
Here's the batch code:
#echo off
setlocal enabledelayedexpansion
set _INFILE="File.txt"
set _OUTFILE="NewFile.txt"
set _OLDFILE="OldFile.txt"
set _PWD=Pword=
set _ACCT=Account=
set _TOWN=Town=
set _POST=Postcode=
set _LOC=LocationChanged=
set _ALL=_PWD _ACCT _TOWN _POST _LOC
echo Parsing old file contents...
for /f "tokens=*" %%f in ('type !_OLDFILE!') do (
for %%g in (!_ALL!) do (
echo %%f | findstr /b /c:!%%g! 1>nul
if "!errorlevel!" equ "0" (
set %%gCONTENTS=%%f
)
)
)
copy nul %_OUTFILE%
echo Merging the old file contents into the new file...
set _WROTE=0
for /f "tokens=*" %%f in ('findstr /n "^^" !_INFILE!') do (
set _TMPVAR0=%%f
set _TMPVAR0=!_TMPVAR0:*:=!
for %%g in (!_ALL!) do (
echo !_TMPVAR0! | findstr /b /c:!%%g! 1>nul
if "!errorlevel!" equ "0" (
echo.!%%gCONTENTS!>>!_OUTFILE!
set _WROTE=1
)
)
if "!_WROTE!" equ "0" (
echo.!_TMPVAR0!>>!_OUTFILE!
) else (
set _WROTE=0
)
)
rem copy /-y %_OUTFILE% %_INFILE%
#EDIT0: Using #StevoStephenson suggestion (as part of the question snippet), I replaced the (2nd) outer for loop to ('findstr /n "^^" !_INFILE!') in order to include the empty lines, so the 3rd remark no longer applies (deleting). Also did some small changes to allow files that contain SPACE s in their paths.
Maybe it works like this
set CopyFile=oldfile.txt
set InputFile=newfile.txt
set str_search="Pword"
for /f "delims=" %%i in ('findstr %str_search% %copyfile%') do set str_replace=%%i
set str_replace="%str_replace%"
echo %str_search%
echo %str_replace%
pause
CALL :far %InputFile% %str_search% %str_replace%
EXIT /B 0
:far
setlocal enableextensions disabledelayedexpansion
set "search=%2"
set "replace=%3"
::remove quotes
set search=%search:"=%
set replace=%replace:"=%
echo %search%
echo %replace%
set "textFile=%1"
for /f "delims=" %%i in ('type "%textFile%" ^& break ^> "%textFile%" ') do (
set "line=%%i"
setlocal enabledelayedexpansion
set "line=!line:%search%=%replace%!"
>>"%textFile%" echo(!line!
endlocal
)
EXIT /B 0
At for /f "delims=" %%i in ('findstr %str_search% %copyfile%') do set str_replace=%%i you write the line with the variable that has the needed info to str_replace.
After that you the program calls an embeded find-and-replace-function (:far) whitch i shemelessly stole from Batch script to find and replace a string in text file without creating an extra output file for storing the modified file
This function finds the string "Pword" and replaces it by the line find in the old file.
Attention:
This doesn't solve your problem completely since your new file has to be s.th like this.
#Password
Pword
so if you loose the = it works otherwise it doesn't. I hope this helps you with your problem.
It's not perfect but this may be okay for you:
#Echo Off
Setlocal EnableExtensions DisableDelayedExpansion
(Set InputFile=F:\EXCHANGE\3\Machine\File.txt)
(Set OutputFile=F:\EXCHANGE\3\File-New.txt)
(Set CopyFile=F:\EXCHANGE\3\OldMachine\OldFile.txt)
For /F "Delims=" %%I In (
'FindStr/B "Pword= Account= Town= LocationChanged= Postcode=" "%CopyFile%"'
) Do Set %%I
(For /F "Tokens=1-2* Delims=]=" %%I In ('Find /V /N ""^<"%InputFile%"') Do (
Echo(%%J|FindStr/B # || (If Defined %%J (Call Echo=%%J=%%%%J%%) Else (
If "%%J" NEq "" (Echo=%%J=%%K) Else (Echo=)))))>%OutputFile%
Timeout -1
EndLocal
Exit/B
I've left the delete and rename for you to add at the end.
This solution should be much faster than the other solutions.
It will also preserve empty lines and lines containing ! and ^.
It only needs one findstr call for collecting the old values for all words.
A second findstr determines all lines (by line number) in the infile which needs an update.
#echo off
setlocal EnableDelayedExpansion
set "_INFILE=File.txt"
set "_OUTFILE=NewFile.txt"
set "_OLDFILE="OldFile.txt"
set "_WORDS=Pword= Account= Town= Postcode= LocationChanged="
REM *** get all values for the key words
for /F "tokens=1,* delims==" %%L in ('findstr "!_WORDS!" "!_OLDFILE!"') do (
for /F %%S in ("%%L") do (
set "word[%%S]=%%M"
)
)
REM *** Find all lines which needs an update
set wordIdx=0
for /F "tokens=1,2,* delims=:= " %%1 in ('findstr /n "!_WORDS!" "!_INFILE!"') do (
set "lines[!wordIdx!].line=%%1"
set "lines[!wordIdx!].word=%%2"
set "replace=!word[%%2]!"
set "lines[!wordIdx!].replace=!replace!"
set /a wordIdx+=1
)
REM *** copy the infile to the outfile
REM *** Replace only the lines which are marked by line numbers
echo Parsing old file contents...
set nextWordIdx=0
set /a searchLine=lines[!nextWordIdx!].line
set lineNo=0
setlocal DisableDelayedExpansion
(
for /f "tokens=*" %%L in ('findstr /n "^" "%_INFILE%"') do (
set "line=%%L"
set /a lineNo+=1
setlocal EnableDelayedExpansion
set "line=!line:*:=!"
if !lineNo! equ !searchLine! (
(echo(!line!!lines[0].replace!)
set /a nextWordIdx+=1
for /F %%R in ("!nextWordIdx!") do (
endlocal
set /a nextWordIdx=%%R
set /a searchLine=lines[%%R].line
)
) ELSE (
(echo(!line!)
endlocal
)
)
) > "!_OUTFILE!"