How to delete the first line of a file using Batch?
I really need help... I've tried everything but it didn't work. :(
I've tried this :
#echo off
Type test.txt | findstr /I /V /C:"So, lets remove this line" >>Test2.txt
exit
Some tasks are clumsy with pure batch techniques (and mangles characters) - this is a robust solution and uses a helper batch file called findrepl.bat that uses built in jscript-ing from - http://www.dostips.com/forum/viewtopic.php?f=3&t=4697
Place findrepl.bat in the same folder as the batch file.
type "file.txt"|findrepl /v /o:1:1 >"newfile.txt"
Another method is to use more.exe but it has limitations with the number of lines at 64K and it mangles TAB characters.
more +2 <file.txt >newfile.txt
(echo off
for /f "skip=1 tokens=1* delims=" %A in (c:\windows\win.ini) do echo %A
echo on)
You'll need to loop through your file line by line and output each line, except the first one.
From your example it looks like you might be looking for a specific string.
try this question/answer .. it might help you get you on your way.
Related
I'm wondering if there is a way to remove semi-duplicate files (name based) using a batch file or any other means (freeware utility) in Windows?
To give an example I have following files in a directory:
fileNameXXX(aaa).ext
fileNameXXX(bbb).ext
In this case, I only want to keep the fileNameXXX(bbb).ext
it's a single line in batch:
for /f "delims=" %%f in ('dir /b "*(*).ext" ^| find /v "(ddd)"') do ECHO del "%%f"
For every file matching the filemask excluding files with (ddd) do: delete it.
Remove the ECHO if the output fits your needs.
Note: if you want to use it directly on command line (instead in a batch file), replace every %%f with %f.
Tip: think about using some more code to check, if there is a Dutch version, and if not, keep the English one (or whatever you prefer).
If I have a directory with 2 files in it like these:
123456789_File1.pdf
123456789_File2.pdf
I need to run a batch file that lets me read the name of the file only and not it's path. Create another file with the same name and a different extension with a contents that is only the first 9 characters of the name. Below is what I have tried and I cannot figure out why this is not correct? I have done several hours of research over something that seems like it should be simple and I feel like I am just missing something.
FOR /R C:\Test\Files %%F in (*.*) do (set content=%%~nF & echo %content:~0,9%>> %%~nF.txt)
This works for me:
#echo off
Setlocal EnableDelayedExpansion
FOR /R . %%F in (*.*) do (
set content=%%~nF
echo !content:~0,9! %%~nF.txt
)
For future reference, please note that "this is not correct" is not an accurate description of the problem as far as StackOverflow questions and answers are concerned. The precise description of your problem is that in each iteration of the loop, %content% appears to contain the exact same content as it had in the first iteration of the loop, regardless of the value of %%F.
The solution is to enable "delayed expansion" and to use the special ! delimiter instead of % so as to enjoy the benefits of delayed expansion.
I've looked everywhere for an answer to this. Basically I need to find a " "(a space) in batch.
So, what i'm doing is making a custom command import/export batch script. Here is what i start by doing:
echo Enter commands file name without the .bat
set/p "file=>"
Now, after that set/p... line, I want to test if the "file" string contains a space.
How I can achieve this without using a different coding language? I'm keeping this strictly batch.
This works for me:
for /f "tokens=2" %%a in ("%file%") do Echo Contains Space
Which will only Echo Contains Space if there is a whitespace in file
I have a text file, call it path.txt in C:\path.txt and it will only have one line of text at a time. That line of text will be a file path, call it C:\projects\test.txt.
What's the best way to first read the text from C:\path.txt and then second use the sort command in a batch file to alphabetize the file whose path is defined by the text string in path.txt ?
Lastly, I want to erase the line of text from that C:\path.txt file.
Please let me know if this is too vague or needs a better explanation and thanks in advance.
The batch file I have now reads:
FOR /F "tokens=*" %%i IN (C:\DONOTMODIFY.txt) DO #ECHO %%i
set "filename=%%i"
SORT filename /O filename
Why the artificial limitation of only one line? Anyway yes this is simple enough. Anyway this will process any number of lines in target.txt. Code off my head so test before real use.
#echo off
for /f %%i in (target.txt) do (
sort %%i /o %%i
)
echo. > target.txt
EDIT:
Instead of echo. > target.txt you could use copy /y nul target.txt > nul that actually creates a totally empty file unlike echo. that makes a blank line.
PS: a tip for future questions: Show that you actually tried something. This is not a do my program for you site.
Okay, so basically, I have a whole list of links in a plain text Notepad file, each link on a seperate line. All I am wanting to do is to add a bit of text before each link, specifically: 127.0.0.1 and a couple of spaces.
So this...
somelink.com
becomes this...
127.0.0.1 somelink.com
By now you've probably already guessed that I am trying to edit the contents of a text file to make it useable as a HOSTS file in Windows.
So I am wanting some batch file code, executable in a .bat file, which basically reads a Notepad text file, and then add "127.0.0.1 " at the beginning of each line with text on it. I am guessing this is probably a very simple piece of code for someone with some knowledge of MS DOS and batch file code, but that most certainly isn't me, and the only batch files I have ever written have been with help like now.
Thanks for any and all help in advance with this, it really is much appreciated.
read HELP FOR and then try this in a command prompt
FOR /F "delims=" %a in (input.txt) do #echo 127.0.0.1 %a >>output.txt
here is some explanation and some considerations to extend it with a little more complete functionality and put it in a BAT file
FOR is the command to iterate over the lines of your input text file. Read microsoft documentation at http://www.microsoft.com/resources/documentation/windows/xp/all/proddocs/en-us/for.mspx
input.txt is the text file that contains your list of domain names, it must reside in the current directory
output.txt will be the result file that will contain the list of domain names prefixed with 127.0.0.1, it will be created in the current directory
If you want to create a BAT file, you need to move the FOR command and edit it a little bit, changing the %a loop variable names to be %%a.
You can then place the BAT file either in the current directory, where your input resided and where the output will be created.
Alternativelly, you may place your BAT file, elsewhere. In that case, you need to invoke it with its full path.
Or you may even place it in a special directory (I have my own C:\Program Files\CMD) and add it in the PATH system variable. See here www.microsoft.com/resources/documentation/windows/xp/all/proddocs/en-us/path.mspx?mfr=true how you can change your PATH for your current session. And here ss64.com/nt/path.html you may find some explanation on how to make the PATH change permanent.
Also, you might be tempted to add some flexibility to your BAT file, instead of having constant input.txt and output.txt filename, replace them with %1 and %2 which represent the first and second command line parameters of your BAT file.
the user might then want to use files which contain blanks in their filenames. They might specify them surrounding the names with quotes ". In that case, you need to add some incantation usebackq in the FOR command for it to not break havoc when the user uses quotes.
Finally, you will need to decide what to do in case the output text file already exists, you migh want to consider prevent overwriting.
So putting all this pieces together, here is a short BAT file to get you started...
#echo off
if .%2==. goto help
if not exist %1 goto helpno1
if exist %2 goto helpalready2
FOR /F "usebackq delims=" %%a in (%1) do #echo 127.0.0.1 %%a >>%2
goto :eof
:help
echo you need to specify input and output text files
goto :eof
:helpno1
echo %1 not found
goto :eof
:helpalready2
echo %2 already exist
goto :eof
welcome to BAT programming and enjoy!
here we go!
(
Set /p line1=
Set /p line2=
Set /p line3=
Set /p line4=
)<Filename.txt
echo 127.0.0.1 %line1%>Filename.txt
echo 127.0.0.1 %line2%>>Filename.txt
echo 127.0.0.1 %line3%>>Filename.txt
echo 127.0.0.1 %line4%>>Filename.txt
This will Read the first four lines of the text file, and then put in your stuff and each line back into the line it came from.
Have Fun!
In addition to PA.'s answer, if you require a specific number of spaces, you can throw them into a variable and add it to the command as well.
SET spaces= # to the left is 10 spaces
FOR /F "delims=" %a in (input.txt) do #echo 127.0.0.1%spaces%%a>>output.txt
So the output will be
127.0.0.1 somelink.com
Batch-File flavor:
SET spaces= # to the left is 10 spaces
FOR /F "delims=" %%a in (input.txt) do #echo 127.0.0.1%spaces%%%a>>output.txt