1.

Solve : Help w/batch merging files?

Answer» <html><body><p>Hello,<br/><br/>I have several file, 1KB each. Some are empty but few are not. If I merge all of them into one file I end up with blank lines. How can I find the empty files when I can not use the size and merge only the ones w/data? Or is there a way to remove the blank lines automatically from the <a href="https://interviewquestions.tuteehub.com/tag/new-1114486" style="font-weight:bold;" target="_blank" title="Click to know more about NEW">NEW</a> file?Test for the file size in a For loop using %%~zA and reject/accept the file for merging based on the file size using an If command.  Note that if a file contains just a CR/LF it is not 'empty', the length will be shown as 2 bytes.<br/><br/>At the command prompt enter For /? to view For help<br/><br/>Post your script. Or exploit the fact that FOR /F skips blank lines<br/> Code: <a>[Select]</a>if exist temp.txt del temp.txt<br/>for /f "delims=" %%a in (file.txt) do echo %%a &gt;&gt;temp.txt<br/>del file.txt<br/>ren temp.txt file.txt<br/>replace all <a href="https://interviewquestions.tuteehub.com/tag/instances-770152" style="font-weight:bold;" target="_blank" title="Click to know more about INSTANCES">INSTANCES</a> of file.txt with <a href="https://interviewquestions.tuteehub.com/tag/name-237998" style="font-weight:bold;" target="_blank" title="Click to know more about NAME">NAME</a> of file to remove blank lines fromThank you all for suggestions. I understand now why, even though the file 'looks' empty, it is not (size is 2 bytes). As a test, I combined 3 files (1 record each) into 1 and then removed the blank lines, at least I thought I did, but when I try to load them it shows 6 records where 3 are blank so the load process aborts. I don't think I can check for size because the file is only 2 bytes, as Dusty pointed out, it's not 'empty'.I can't get this to work. I still end up with 6 records when I only have 3. How I end up with 3 blank ones, which you can not actually see in the file, when I already removed the blank lines? Please, if anybody can help, let me know. Here are the batch files I am using.<br/><br/>rem RUN_CLEAN_TEST_FILE.BAT<br/>echo on<br/>date /t<br/>time /t<br/>call clean_test_file TEST1.txt TEST.txt<br/>exit<br/><br/>rem clean_test_file.bat<br/>echo off<br/>For /F "tokens=* delims=" %%A in (%1) Do Echo %%A &gt;&gt; %2<br/>exit Quote from: Cookie</p><blockquote>As a test, I combined 3 files (1 record each) into 1 and then removed the blank lines</blockquote> <br/>How did you "combine" the files, which software did you use?<br/> <br/>Please post a screenshot of a hex editor output of test1.txt and test.txtHi,<br/>I don't use any software to combine the file, I just use the copy command because I want everything to run in batch:<br/>copy *.txt test1.txt /b<br/>Here is where test1.txt contains blanks so I run the clean batch to remove the blanks and create the test.txt file.<br/>I am not sure if this is what you're <a href="https://interviewquestions.tuteehub.com/tag/asking-2445373" style="font-weight:bold;" target="_blank" title="Click to know more about ASKING">ASKING</a> for, but here they are<br/><br/><br/>[recovering disk space - old attachment deleted by admin]Suggest you do the lot in one script as below.  This should <a href="https://interviewquestions.tuteehub.com/tag/read-619994" style="font-weight:bold;" target="_blank" title="Click to know more about READ">READ</a> your three test files and write them to output omitting blank lines, after testing you can change the inputs/output filenames to whatever you want:<br/><br/> Code: <a>[Select]</a>echo off<br/>cls<br/><br/>for /f "tokens=*" %%1 in (inputfile1.txt inputfile2.txt inputfile3.txt) do (<br/>    echo %%1&gt;&gt;outputfile.txt<br/>)<br/><br/></body></html>


Discussion

No Comment Found