InterviewSolution
| 1. | 
                                    Solve : Find and Replace text in all text files in a specific folder - NT4 OS? | 
                            
| 
                                   
Answer»  Trying to come up with a batch that will work with NT4 OS and find and replace specific lines in text files. There are about 60 of these text files that hold operational parameters in them. Up to this point on a weekly basis when they are updated on sundays at 7am, I have been just going in with notepad and changing the specific lines from N to Y to enable features and then ALT+F+S and ALT+F+X which i can get through 60 files pretty quickly manually changing specific lines to Y from N. if "%%b"=="$LASTSORT N" echo $LASTSORT Y but cool that batch doesnt need an escape character for the $ outside of a string " " area as I highlighted that your echo-ing out. Learned something new. Also reading the batch you provided it looks like its looking only in *.txt text files, so I would need to change the file extension to the extension of the target which would be one FOR loop for the *.xts and the another FOR loop for the *.mtx file extension to test when all *.xts files have been tested for the string match and replace if found. As I altered what you gave me below, or should it be best rewritten with an OR statement in a single FOR loop to test both for the string match in both *.xts or *.mtx files or even just change to a total wild card of a single FOR loop testing all files with *.*? or would a *.* cause a CRC Error due to it grabbing at the tempnewfile.tmp file which is also in the target directory of execution and has bitten me before with xcopy instructions with wild cards and writing back to source drive. Code: [Select]echo off cd "c:\folder" for %%a in (*.xts) do ( ( for /f "delims=" %%b in ('type "%%a"') do ( if "%%b"=="LS: N" echo LS: Y if "%%b"=="$LASTSORT N" echo $LASTSORT Y if not "%%b"=="LS: N" if not "%%b"=="$LASTSORT N" echo %%b ) )>"tempnewfile.tmp" del "%%a" ren "tempnewfile.tmp" "%%a" ) for %%a in (*.mtx) do ( ( for /f "delims=" %%b in ('type "%%a"') do ( if "%%b"=="LS: N" echo LS: Y if "%%b"=="$LASTSORT N" echo $LASTSORT Y if not "%%b"=="LS: N" if not "%%b"=="$LASTSORT N" echo %%b ) )>"tempnewfile.tmp" del "%%a" ren "tempnewfile.tmp" "%%a" ) ( OR ) If there is no risk of a CRC Error of it grabbing at its own temp file use a *.* to test all at the location by which this is executed, where the batch is executed from a location say at C: which then the instructions of the batch switch the target to C:\folder as you have on the 2nd line. Code: [Select]echo off cd "c:\folder" for %%a in (*.*) do ( ( for /f "delims=" %%b in ('type "%%a"') do ( if "%%b"=="LS: N" echo LS: Y if "%%b"=="$LASTSORT N" echo $LASTSORT Y if not "%%b"=="LS: N" if not "%%b"=="$LASTSORT N" echo %%b ) )>"tempnewfile.tmp" del "%%a" ren "tempnewfile.tmp" "%%a" )My original code has a problem - due to a bug in the for command - and adding an extension as I described below will also fix that issue. You can process different filetypes using a single for loop and the ren command keeps the original extension but adds an extra one - and that extra one is removed after all files are processed, by the last ren command. Code: [Select]echo off cd "c:\folder" for %%a in (*.mtx *.xts) do ( ( for /f "delims=" %%b in ('type "%%a"') do ( if "%%b"=="LS: N" echo LS: Y if "%%b"=="$LASTSORT N" echo $LASTSORT Y if not "%%b"=="LS: N" if not "%%b"=="$LASTSORT N" echo %%b ) )>"tempnewfile.tmp" del "%%a" ren "tempnewfile.tmp" "%%a.final" ) ren *.final *. Thanks Foxidrive ... I am going to try this out today on the PC of the system. Going to make a backup copy of the files first just in case, but looks like it should work as intended. Didnt know that a FOR loop could have ( *.mtx *.xts ) like this. I was thinking that for a single FOR loop it might need to be 2 separate ARGUMENTS like an OR condition such as ( *.mtx ) || ( *.xts ) otherwise it would take *.mtx *.xts as a single string vs testing for one or the other file types. Learned something new. Will let you know how it went in a reply back later. Thanks for your help on this! Just tested it and it looks like maybe there is an issue with this batch and NT4 not supporting it or some other issue. Command Shell Window goes black for about 10 seconds as its processing and then scrolls duplicate file found and then gets to the pause that I added in the batch so that the command shell window wouldnt close and I could see the messages. All but 4 config files at the C:\Machine1\Parameters\Sorts\ location for *.mtx and *. xts are changed to 0k files with contents wiped out and the 4 that weren't completely wiped out of all other parameters had LS: Y in the 1k files. The Files are normally like 37k in size and have all sorts of config parameters in them. I ended up reverting back from backup of the files so that I can try the batch again when a correction is made. In the folder where this is all happening there are 2 copies of each file with 2 different file extensions, but as stated above all but 4 files are wiped out of data and 0k in size. I have run into other difficulties with batch and NT4 so this wouldnt be the first time that a batch that should work and probably works fine on a newer NT based OS pukes on NT4. The last time I ran into issues with NT4 it was using the date and time calls which NT4 doesnt like %date% and %time%. I didnt add anything else to the batch you provided, I just changed the path as necessary for the target and added a pause at the end to see the last messages before close of the window as highlighted below. Quote echo off Thank You for your efforts on this and hopefully it doesnt make you want to pull your hair out like I have felt like doing with NT4 and batches in the past. The -NT4 OS on the subject line of this was to make it clear that its NT4 OS and is probably going to be picky with what it supports for instructions. My guess is that this batch either has a small typo that I am not seeing or needs to be tweaked to a legacy format that NT4 is fine with. EDIT: Was just thinking could there be a character in these config files that is causing the batch to go haywire such as these characters as for the config files are loaded with config info that uses special characters and HEX etc such as these characters [email protected]#$%^&*(){}[]~`';:"<>,.?/\|+=-_ Maybe I'm off the beaten path and its something else, but I have run into issues before with other scripts and the script hitting characters that cause issues, so figured I'd touch on this if its a concern or not. Maybe it doesnt matter what info is in the config files as for its only looking for the specific matches and no other characters can foul it up.Can you try this batch script? It works on W2K - and just creates a test folder and creates two files to test it with. See if any errors occur before the first pause and what they are. I don't have NT4 to test it with here. Code: [Select]echo off md "C:\Machine1\Parameters\Sorts\testdave" cd "C:\Machine1\Parameters\Sorts\testdave" ( echo $LASTSORT N echo PL120 echo LS: N echo dostips )>aaa.mtx ( echo $LASTSORT N echo PL120 echo LS: N echo dostips )>aaa.xts echo after file creation pause for %%a in (*.mtx *.xts) do ( ( for /f "delims=" %%b in ('type "%%a"') do ( if "%%b"=="LS: N" echo LS: Y if "%%b"=="$LASTSORT N" echo $LASTSORT Y if not "%%b"=="LS: N" if not "%%b"=="$LASTSORT N" echo %%b ) )>"tempnewfile.tmp" del "%%a" ren "tempnewfile.tmp" "%%a.final" ) echo before final rename ren *.final *. pause I'm not dave but I do have NT4. Not sure how useful that will be. I ran the batch file and it works without any error up to the first pause. After continuing I get a few errors: Code: [Select]Press any key to continue . . . A duplicate file name exists, or the file cannot be found. before final rename A duplicate file name exists, or the file cannot be found. A duplicate file name exists, or the file cannot be found. Press any key to continue . . . Thanks BC_Programmer, that is useful. I thought I'd traced a flaw but my brain must be out-to-lunch, as it was the same code. If you have time to test this then it may help locate the problem. Run this in an empty folder. Code: [Select]echo off :md "C:\Machine1\Parameters\Sorts\testdave" :cd "C:\Machine1\Parameters\Sorts\testdave" ( echo $LASTSORT N echo PL120 echo LS: N echo dostips )>aaa.mtx ( echo $LASTSORT N echo PL120 echo LS: N echo dostips )>aaa.xts echo after file creation dir pause for %%a in (*.mtx *.xts) do ( ( for /f "delims=" %%b in ('type "%%a"') do ( if "%%b"=="LS: N" echo LS: Y if "%%b"=="$LASTSORT N" echo $LASTSORT Y if not "%%b"=="LS: N" if not "%%b"=="$LASTSORT N" echo %%b ) )>"tempnewfile.tmp" del "%%a" ren "tempnewfile.tmp" "%%a.final" ) echo =========== dir echo before final rename ren *.final *. pause If that fails can you try this version please? Code: [Select]echo off :md "C:\Machine1\Parameters\Sorts\testdave" :cd "C:\Machine1\Parameters\Sorts\testdave" ( echo $LASTSORT N echo PL120 echo LS: N echo dostips )>aaa.mtx ( echo $LASTSORT N echo PL120 echo LS: N echo dostips )>aaa.xts del "tempnewfile.tmp" 2>nul for %%a in (*.mtx *.xts) do ( for /f "delims=" %%b in ('type "%%a"') do ( if "%%b"=="LS: N" >>"tempnewfile.tmp" echo LS: Y if "%%b"=="$LASTSORT N" >>"tempnewfile.tmp" echo $LASTSORT Y if not "%%b"=="LS: N" if not "%%b"=="$LASTSORT N" >>"tempnewfile.tmp" echo %%b ) del "%%a" ren "tempnewfile.tmp" "%%a.final" ) ren *.final *. pause First Batch Output: Code: [Select]C:\test2>test after file creation Volume in drive C has no label. Volume Serial Number is C875-5A5F Directory of C:\test2 06/26/15 03:22a <DIR> . 06/26/15 03:22a <DIR> .. 06/26/15 03:22a 36 aaa.mtx 06/26/15 03:22a 36 aaa.xts 06/26/15 03:20a 693 test.bat 5 File(s) 765 bytes 3,810,073,600 bytes free Press any key to continue . . . ECHO is off. Volume in drive C has no label. Volume Serial Number is C875-5A5F Directory of C:\test2 06/26/15 03:22a <DIR> . 06/26/15 03:22a <DIR> .. 06/26/15 03:22a 7 aaa.mtx.final 06/26/15 03:22a 7 aaa.xts.final 06/26/15 03:20a 693 test.bat 5 File(s) 707 bytes 3,810,073,600 bytes free before final rename A duplicate file name exists, or the file cannot be found. Press any key to continue . . . Second batch: Code: [Select]C:\test2>test The system cannot find the file specified. Could Not Find C:\test2\aaa.mtx The system cannot find the file specified. Could Not Find C:\test2\aaa.mtx Could Not Find C:\test2\aaa.mtx The system cannot find the file specified. The system cannot find the file specified. Could Not Find C:\test2\aaa.xts The system cannot find the file specified. Could Not Find C:\test2\aaa.xts Could Not Find C:\test2\aaa.xts The system cannot find the file specified. A duplicate file name exists, or the file cannot be found. Press any key to continue . . . After the second batch file ran there were two files in the same folder as the batch file, "aaa" and "aaa.xts.final" I did some tests myself here and compared with Windows 2000, and I think the difference (or perhaps one of many) may be that in NT4, rename appears to work differently. with two files test.xts.final and test2.xts.final, for example, on Windows 2000 (and presumably later) ren *.final *. will rename them to test.xts and test2.xts respectively. However, on NT4 it renames them to test and test2- removing the extension entirely. Thanks again BC_Programmer, it is that final rename - a change as Windows versions flew past. This may work - touch wood. It relies on the filenames NOT having a plus + sign in them. Code: [Select]echo off :md "C:\Machine1\Parameters\Sorts\testdave" :cd "C:\Machine1\Parameters\Sorts\testdave" ( echo $LASTSORT N echo PL120 echo LS: N echo dostips )>aaa.mtx ( echo $LASTSORT N echo PL120 echo LS: N echo dostips )>aaa.xts for %%a in (*.mtx *.xts) do ( ( for /f "delims=" %%b in ('type "%%a"') do ( if "%%b"=="LS: N" echo LS: Y if "%%b"=="$LASTSORT N" echo $LASTSORT Y if not "%%b"=="LS: N" if not "%%b"=="$LASTSORT N" echo %%b ) )>"%%a+tmp" del "%%a" ) echo ===== before rename & dir & echo ===== before rename & pause for %%a in ("*+tmp") do ( for /f "delims=+" %%b in ("%%~a") do ren "%%~a" "%%b" ) echo done pause Thanks BC to providing the NT4 environment testing for Foxidrive on this issue. Will be checking back here periodically on when you want me to test on the live system. Thanks for both of your help on this.No error this time: Code: [Select] ===== before rename Volume in drive C has no label. Volume Serial Number is C875-5A5F Directory of C:\testdata 06/26/15 04:41a <DIR> . 06/26/15 04:41a <DIR> .. 06/26/15 04:41a 7 aaa.mtx+tmp 06/26/15 04:41a 7 aaa.xts+tmp 06/26/15 04:39a 710 test.bat 5 File(s) 724 bytes 3,810,054,656 bytes free ===== before rename Press any key to continue . . . done Press any key to continue . . . The files don't seem to get renamed at the last step, though- both aaa.mtx+tmp and aaa.xts+tmp remain. Quote from: BC_Programmer on June 26, 2015, 06:44:55 AM No error this time: Hmm - can you please show us what this prints to the console? Code: [Select]echo off ( echo $LASTSORT N echo PL120 echo LS: N echo dostips )>aaa.mtx+tmp ( echo $LASTSORT N echo PL120 echo LS: N echo dostips )>aaa.xts+tmp for %%a in ("*+tmp") do ( echo "%%~a" for /f "tokens=1 delims=+" %%b in ("%%~a") do echo ren "%%~a" "%%b" ) echo done pause  | 
                            |