I have the following batch file HERE
It searches for the line that contains...
[/Script/MyGame.Mode]
Then it creates a new line and adds...
RedirectReferences=(PackageName="%Package%",PackageURLProtocol="%PackageURLProtocol%",PackageURL="%WebAddress%/%Package%%Ext%",PackageChecksum="")
It works unless the original file contains spaces after each line. If there are no spaces it works perfect.
Is there an easy way to clear out all the spaces in the original file before it searches, copies and writes the new line in a new file?
Or is there a better way to do this overall. Sorry but I'm not knowledgeable at batch files yet. Thanks for any help given.
I figured it out. I was able to clear the spaces from the original file with the code below before running the copy and adding the new line.
I changed some file names then ran the code below before I'm checking the file and writing new line.
I had to show some code from my other bat to show you the file name changes. Once you combine everything together in one bat it works fine.
:CLEARWHITESPACE
for /F "eol=; tokens=1 delims=; " %%I in (Game.txt) do (
set /a count+=1
if !count! leq 10 echo %%I>>game.temp.txt
)
:SHOWLINE
set NewURL=RedirectReferences=(PackageName="%Package%",PackageURLProtocol="%PackageURLProtocol%",PackageURL="%WebAddress%/%Package%%Ext%",PackageChecksum="")
pause
:WRITENEW
set inputfile=game.temp.txt
set outputfile=game.temp2.txt
(for /f usebackq^ delims^=^ eol^= %%a in ("%inputfile%") do (
echo %%a
if "%%~a"=="[/Script/MyGame.Mode]" call echo %%NewURL%%
))>>"%outputfile%"
Related
I want to make a program that takes the content of the second line of a text file and puts it on the first. (It doesn't matter if the second doesn't get edited)
for /f "tokens=1" %%t in (file.txt) do set string1=%%t
for /f "tokens=2" %%t in (file.txt) do set string2=%%t
echo %string1%%string2%>file.txt
I have two issues hat I can't seem to be able to fix.
One: the loops only include the first word of each line in the variables.
Two: Echo doesn't replace the first line of the file with the variables given and instead writes ECHO command deactivated (I have the French version of Windows 10 and simply translated what got written in the file, the text in English Windows version might be slightly different, but you get the idea)
If you have any suggestions, I would appreciate if you explain what the code you provide does (I always like to learn)
Your question is not clear and can be understood in several different ways. Anyway, this management is simpler with no for command:
#echo off
setlocal EnableDelayedExpansion
< file.txt (
rem Takes the content of the first line
set /P "line1="
rem Takes the content of the second line and puts it on the first
set /P "line2="
echo !line1!!line2!
rem It doesn't matter if the second line doesn't get edited
echo !line2!
rem Copy the rest of lines
findstr "^"
) > output.txt
move /Y output.txt file.txt
The FOR command uses a space as a delimiter by default. So you have to tell it to not use any delimiters with the DELIMS option. Also, you should be able to do this with a single FOR /F command. Just hold the previous line in a variable.
#ECHO OFF
setlocal enabledelayedexpansion
set "line1="
(for /f "delims=" %%G in (file.txt) do (
IF NOT DEFINED line1 (
set "line1=%%G"
) else (
echo !line1!%%G
set "line1="
)
)
REM If there are an odd amount of lines, line1 will still be defined.
IF DEFINED line1 echo !line1!
)>File2.txt
EDIT: I think I completely misunderstood your question. Once you clarify your question I will repost a code solution if needed.
Use skip to omit the first line and write the 2nd line twice. In general an edit of a file implies a rewrite to a new file and possibly a rename to retain the old file name.
:: Q:\Test\2018\07\25\SO_51508268.cmd
#Echo off
Set "flag="
( for /f "usebackq skip=1 delims=" %%A in ("file1.txt") Do (
If not defined flag (
Echo=%%A
Set flag=true
)
Echo=%%A
)
) >file2.txt
Del file1.txt
Ren file2.txt file1.txt
After running the batch a file1.txt with initially numbered lines 1..5 looks like this:
> type file1.txt
2
2
3
4
5
Okay I have several lines in a text file. I want to get the first line and save it in another file. For example this is the text file:
put returns between paragraphs
for linebreak add 2 spaces at end
for linebreak add 2 spaces at end2
for linebreak add 2 spaces at end3
I want put returns between paragraphs to be saved into another file.
I used
for /f "tokens=" %%A in ('findstr /r "^[0-9][0-9]*$" <"C:\Users\Sherlock\Desktop\AbcImport\123.txt"') do echo 123>>1234.txt
pause
But it doesn't work at all.
How to get just the first line of a text file written into a new text file using a batch file?
Option 1 - SET /P : This is the simplest and fastest pure batch solution, provided the line does not exceed 1021 bytes, and it does not end with control characters that must be preserved. The size of the file does not matter - it will always read and write the first line very quickly.
#echo off
setlocal enableDelayedExpansion
set "ln="
<"input.txt" set /p "ln="
>"output.txt" (echo(!ln!)
Option 2 - FOR /F : This will work with lines up to ~8191 bytes long, but it can be slow if the file is really large because the FOR /F loop must read the entire file before it processes the first line. This solution is basically the same as the Mofi answer, except it disables the EOL option, so it never ignores the first line, regardless what the first character is. It does have a limitation that it will skip empty lines, so technically it does not give the correct result if the first line is empty:
#echo off
for /f usebackq^ delims^=^ eol^= %%A in ("input.txt") do echo(%%A>"output.txt"&goto :break
:break
There is a way to preserve the first line if it is empty using pure batch, but I would not bother. I would move on to ...
Option 3 - JREPL.BAT, or some other non-batch solution : Batch is quite poor at manipulating text files. You are much better off using some other scripting language like VBScript, JScript, or Powershell. Or a Windows port of any number of unix utilities.
I would use JREPL.BAT - a hybrid JScrpit/batch regular expression text processing utility that runs natively on any Windows machine from XP onward. It is way overkill for such a simple task, but it is an extremely handy, powerful, and efficient tool to have in your arsenal. Once you have it, then it can be used for many text processing tasks. Full documentation is embedded within the script.
jrepl "^.*" "$&" /jendln "quit=true" /f "input.txt" /o "output.txt"
Use CALL JREPL if you put the command within a batch script.
Here is the batch code to write just first non blank/empty line of a text file into another text file.
#echo off
for /F "usebackq delims=" %%I in ("InputTextFile.txt") do (
echo %%I>"OutputTextFile.txt"
goto ContinueAfterLoop
)
:ContinueAfterLoop
InputTextFile.txt is the file in current directory containing the first line to copy.
OutputTextFile.txt is the file created in current directory with first line from input file copied into this output file.
The command GOTO is used to exit the loop after first line is processed and continue the batch file below the loop.
For understanding the used commands and how they work, open a command prompt window, execute there the following commands, and read entirely all help pages displayed for each command very carefully.
echo /?
for /?
goto /?
Read also the Microsoft article about Using Command Redirection Operators.
You can use use this command:
SetLocal EnableDelayedExpansion
for /f "tokens=* delims=;" %%m in ("C:\Users\Sherlock\Desktop\AbcImport\123.txt") do (
set /p FirstLine=<%%m
echo !FirstLine!>>1234.txt
)
and for multiple file:
SetLocal EnableDelayedExpansion
for %%a in ("*") do (
for /f "tokens=* delims=;" %%m in ("%%a") do (
set /p FirstLine=<%%m
echo !FirstLine!>>1234.txt
)
)
rem Get the first line of a text file:
set /P "line=" < "C:\Users\Sherlock\Desktop\AbcImport\123.txt"
rem Write it into a new text file:
echo %line%> 1234.txt
So im working on a batch file that can Extract certain lines of text from a massive number of txt files. I had it working fine inside 1 folder but I needed it to be recursive so I changed it up a bit, and It no longer Outputs anything to a text file
for /r %%Z in (*.as) do (
SET /a count=0
set /a non_eng=0
echo z = %%Z
pause
for /F "eol=; tokens=1,2 usebackq delims=(" %%A in ("%%Z") do (
set /a count+=1
if /i %%A==texte echo !count!=%%A(%%B
) > "%%Z.txt"
Echo Writting To File %%Z.txt
pause
if exist "%%Z" echo LC_!count! >> "%%Z.txt")
This Line No longer works
) > "%%Z.txt"
But if I change it to >> it works fine... Problem Is it doubles up the copied text each time...
Working version (just doesn't delve into directories)
PS: I Worked around this issue but Im still unsure as to what caused the problem in the first place at the very least I believe I should have been geting at least one line of correct output to the file.
Your working code has the redirection placed at the for loop that iterate the files, not the for /f that reads them. In this way there is one open/write/close on the log file for each file being processed, with all the data echoed inside redirected to the output file.
But in the non working version, you have the redirection at the for /f that reads the files. Now you have a open/write/close operation for each of the lines of the file being processed. And, as the redirection is >, only the last execution of the for /f is saved in the file.
The simplest way to avoid it is to enclose the inner for loop in parenthesis and redirect this block
(
for /f .... %%A in (...) do (
....
)
) > "%%Z.txt"
I'm new to batch scripting and could use some assistance.
I'm trying to write a batch script that will open a .txt file, remove the first 4 lines, and delete some other things leaving just some numbers. I need some help deleting the first four lines.
#echo off
title test1
start users_6042014.txt
for /f "skip=4 delims=*" %%a in (C:\users_6042014.txt) do (
echo %%a >>C:\newfiletest.txt
)
xcopy C:\newfiletest.txt C:\users_6042014.txt /y
I thought this would delete the first lines and make a copy of the new file but it's not working. Any help would be greatly appreciated!
You don't tell us what isn't "working" about the batch.
The start users_6042014.txt will open the file with the default editor (presumably notepad) which is probably not what you want to do. Try commenting this line out, or remove it completely.
Note that
echo %%a >>C:\newfiletest.txt
will append to any existing C:\newfiletest.txt an probably also append a space to the end of each line.
Try
(for /f "skip=4 delims=*" %%a in (C:\users_6042014.txt) do echo %%a)>C:\newfiletest.txt
to create the file anew.
Finally, move in place of xcopy would replace the existing file; C:\newfiletest.txt would no longer exist.
Test this:
This will ignore lines starting with ; and blank lines.
#echo off
title test1
for /f "usebackq skip=4 delims=" %%a in ("C:\users_6042014.txt") do (
>>"C:\newfiletest.txt" echo %%a
)
move "C:\newfiletest.txt" "C:\users_6042014.txt"
I've searched a thousand of example and tried, but none of them actually works for me. My requirement is pretty straight forward, I have a file - config.txt, it has one of lines:
sqlServer=localhost
I'm trying to update this line to:
sqlServer=myMachine\sql2012
I looked examples online, some of them are just working with set variables, or some are not replacing but inserting. There are some examples are writing into a new file, but the new file has line number in front of each line. I don't find a useful instruction how to write batch scripts, and none of the update file batch scripts works for me.
It will be very helpful if you leave some comments.
Thanks in advance
EDITED - to adapt to comments
#echo off
setlocal enableextensions disabledelayedexpansion
set "config=.\config.txt"
set "dbServer=localhost\sql2012"
for /f "tokens=*" %%l in ('type "%config%"^&cd.^>"%config%"'
) do for /f "tokens=1 delims== " %%a in ("%%~l"
) do if /i "%%~a"=="sqlServer" (
>>"%config%" echo(sqlServer=%dbServer%
) else (
>>"%config%" echo(%%l
)
type "%config%"
endlocal
Input file is read line by line (for /f %%l), then each line is split (for /f %%a) and if the first token in the line is "sqlserver" then the line is replaced, else the original line is sent to file.
The command used to retrieve the information in the first for loop includes an cd.>"%config%" that will remove the contents of the file, so the final resulting lines (that have been read in memory by the for command before removing them) are sent directly to the same file.
You can do this:
FINDSTR /I /V /B "sqlServer=" config.txt > config.new
ECHO sqlServer=myMachine\sql2012 >> config.new
DEL config.txt
REN config.new config.txt
The FINDSTR will remove all lines that start with sqlServer= and create a new file called newfile.
The ECHO will add a line at the end with sqlServer=MyMachine\sql2012.
The last two lines delete your existing config.txt and replace it with the output of the first two lines.