|
Answer» Hi, I have a template file test_template.txt, and want to multiply it 36 times as: test00.txt, test01.txt, etc. Also, inside test00.txt, I want to replace the occurence _NN_ with 00, inside test01.txt replace _NN_ with 01 and so on. Could someone help me? I'm using win xp, but want to do it from a .bat file. I figured out how I can multiply the file, but I'm stuck with replacement of _NN_. Thanks, Bogdancan you please post your code you have made so far?using a 3rd party .com, it is possible: Change.com(zip) extract to windows folder... then
replace 36 for how many copies you want, and i couldn't get it double, 01 02. it goes 1,2,3, etc
Code: [Select]@echo off for /l %%a in (01,01,36) do (copy test_template.txt test%%a.txt change.com test%%a.txt "_NN_" "%%a")Umm...
It could be done with a for loop.
@echo off Setlocal enabledelayedexpansion For /f "delims=" %%a in ('type test_template.txt') do ( Set line=%%a Set line=!line:_NN_=00! Echo !line!>test00.txt ) That would work but I can't remember how to replace a string in a VARIABLE with a different variable (which woud save you having to copy and paste the code for each file.
you could try the following:
Code: [Select]@echo off if exist newfile.txt del newfile.txt for /f "tokens=*" %%a in (test_template.txt) do call :AddText "%%a del myfile.txt rename newfile.txt test_template.txt pause exit /b
:AddText %1 echo %1 set Text=%~1% IF NOT "%Text%"=="_NN_" echo %Text% >> newfile.txt if "%Text%"=="_NN_" ECHO 00>> newfile.txt exit /b
you would need to change the following line: if "%Text%"=="_NN_" ECHO 00>> newfile.txt to the variable for the file number from the rename.my code needed a little work. I have updated the code to work correctly.
Code: [Select]@echo off :start if exist newfile.txt del newfile.txt
set /a count+=1 echo %count% set name=test%count%.txt copy "test_template.txt" %name%
for /f "tokens=*" %%a in (%name%) do call :AddText "%%a if exist %name% del %name% rename newfiles.txt %name%
IF NOT %count%==36 GOTO start ECHO JOB COMPLETE..... PAUSE exit /b
:AddText %1 set Text=%~1% IF NOT "%Text%"=="_NN_" ECHO %Text% >> newfiles.txt if "%Text%"=="_NN_" ECHO %count% >> newfiles.txt exit /b If you have any questions or issues please let me know.Thanks for your replies. wbrost, I tried your script, but on my pc the result is that it creates the files, but with single DIGIT in the name. Eg. 1,2,etc instead of 2 digits, like 01, 02. Also, the _NN_ pattern does not get replaced. I think the problem with this is that it is not delimited by any white characters. Also, empty lines get removed, and they should be like the template. Thanks for your help.
An small example of the template file is:
load 0x1D000000 mpeg_slice.bin
load 0x1C000000 decoderinfo_NN_.bin load 0x1C0002FC SomeMatrix.bin load 0x1C0003FC SliceCode_NN_.bin also, for file multiplication I tried, and worked:
for %%D in (00, 01, 02, 03, 04, 05, 06, 07, 08, 09, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35) DO copy test_template.txt test%%D.txt
As you see, I'm a complete beginner at this kind of things. Quote from: bozergu on August 07, 2009, 09:23:51 AM also, for file multiplication I tried, and worked:
for %%D in (00, 01, 02, 03, 04, 05, 06, 07, 08, 09, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35) DO copy test_template.txt test%%D.txt
As you see, I'm a complete beginner at this kind of things.
Maybe this could work.
For %%a in (NUMBERS) do for /f "delims=" %%b in ('type test_template.txt') do ( set b=%%b Set b=%b:_NN_=%%a% Echo %b% >> test%%a.txt ) I made that on my iPod, so it is untested. you can use this vbscript Code: [Select]Set objFS=CreateObject("Scripting.FileSystemObject") strTemplate = "file" For num=0 To 36 Set objFile = objFS.OpenTextFile(strTemplate) strnum = Pad(num,2) Set objOutFile = objFS.CreateTextFile("test" & strnum ,True ) Do Until objFile.AtEndOfStream strLine = objFile.ReadLine If InStr(strLine,"_NN_")> 0 Then strLine = Replace(strLine,"_NN_",Pad(num,2)) End If objOutFile.Write(strLine & vbCrLf ) Loop objOutFile.Close objFile.Close Next
Function Pad(input, howmany) 'Pad leading zeroes to a string till length of howmany Do Until Len(input) = howmany input = "0" & input Loop Pad = input End Function
output Code: [Select] C:\test>more file this is first line blah sfj _NN_ laksjdf _NN_ last line
C:\test>cscript /nologo test.vbs
C:\test>ls -1 test00 test01 test02 test03 test04 test05 test06 test07 test08 test09 test10 test11 test12 test13 test14 test15 test16 test17 test18 test19 test20 test21 test22 test23 test24 test25 test26 test27 test28 test29 test30 test31 test32 test33 test34 test35 test36
C:\test>more test25 this is first line blah sfj 25 laksjdf 25 last line
C:\test>more test01 this is first line blah sfj 01 laksjdf 01 last line
Can someone test my code? I would like to know if it WORKS or not, as I have never used a for loop within another for loop.Thanks to all of you for your help,
Helpmeh: I tried your code, but it was writing something like this to every file: Echo ENABLE is on.
Anyway the first code that you posted was writing what was needed to the files, so I managed to combine them and come with the following version, which does what it has to(see it at the end of the post).
Yet, there's one more issue, more a cosmetical one - the batch removes all empty lines from the template, and I'd like it not to. Can someone give a hint regarding this?
Setlocal enabledelayedexpansion for %%a in (00, 01, 02, 03, 04, 05, 06, 07, 08, 09, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35) do ( if exist test%%a.txt del test%%a.txt for /f "delims=" %%b in ('type test_template.txt') do ( set line=%%b Set line=!line:_NN_=%%a! Echo !line! >> test%%a.txt ) ) Quote from: bozergu on August 10, 2009, 01:56:15 AMThanks to all of you for your help,
Helpmeh: I tried your code, but it was writing something like this to every file: Echo enable is on.
Anyway the first code that you posted was writing what was needed to the files, so I managed to combine them and come with the following version, which does what it has to(see it at the end of the post).
Yet, there's one more issue, more a cosmetical one - the batch removes all empty lines from the template, and I'd like it not to. Can someone give a hint regarding this?
Setlocal enabledelayedexpansion for %%a in (00, 01, 02, 03, 04, 05, 06, 07, 08, 09, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35) do ( if exist test%%a.txt del test%%a.txt for /f "delims=" %%b in ('type test_template.txt') do ( set line=%%b Set line=!line:_NN_=%%a! Echo !line! >> test%%a.txt ) )
I just looked at my code and was about to add the delayed expansion part until I saw your code. You could add this line after the set commands (replace the echo command).
If "%%b"=="" (echo. >> test%%a.txt) else (echo !b! >> test%%a.txt)
That could work. If not then it may be a problem with how FOR handles blank lines (which I am fairly sure it is).
|