Batch Remove Trailing Whitespace in a Text File - batch-file

Remove trailing spaces from a file using Windows batch? and How to remove trailing and leading whitespace for user-provided input in a batch file? seem to be similar questions, but I can't get any of the answers to work.
I have a text file, say C:\Users\%username%\Desktop\sometext.txt that has a long list of numbers, some with trailing whitespace. How can I loop through and remove the whitespace at the end of each line? I'd like to do this all in batch. It'll be in a file with some other batch commands in it.

Regrettably, you don't show us an example from your file, so we're left to assume from your desription.
Assuming your file is something like
1
22
3
64
where some of the lines have trailing spaces, then
#ECHO OFF
SETLOCAL ENABLEDELAYEDEXPANSION
(
FOR /f "usebackq" %%a IN (q30594509.txt) DO (
SET /a num=%%a
ECHO(!num!
)
)>u:\newfile.txt
GOTO :EOF
I used a file named q30594509.txt containing the above data for my testing.
Produces u:\newfile.txt

Related

How to insert line breaks on existing text file?

I have an existing text file that is one long string. I would like to create a .bat script to insert a carriage return and line-feed after it finds ~.
For example, the original text file is:
This is a long string~which should be many lines~and yet it is not
The wanted output is:
This is a long string~
which should be many lines~
and yet it is not
I am not really sure how to do tilde replacement within a batch file because the tilde is a special character within the SET command for substrings.
But this should get you headed in the right direction.
#echo off
set "longline=This is a long string~which should be many lines~and yet it is not"
set count=1
:loop
FOR /F "tokens=1* delims=~" %%G IN ("%longline%") DO (
SET "line%count%=%%G"
set "longline=%%H"
IF DEFINED longline (set /a count+=1 &goto loop)
)
FOR /L %%I IN (1,1,%count%) DO call echo %%line%%I%%
pause
I suppose you could also utilise PowerShell from your batch file too:
#If "%~1"=="" (Exit/B) Else If Not Exist "%~1" Exit/B
#Powershell -C "(GC '%~1') -Replace '~',\"`r`n\"|SC '%~1'"
The above accepts your input file as its argument, which means it could be as simple as a drag and drop job. The output file will be ASCII encoded by default.
First let me explain the three different types of line break/newline/line ending/line termination types.
There is carriage return with the escape sequence \r with hexadecimal code value 0D abbreviated with CR and line-feed with the escape sequence \n with hexadecimal code value 0A abbreviated with LF.
Text files on MS-DOS/Windows use CR+LF as newline.
Text files on Unix/Linux/MAC (since OS X) use just LF as newline.
Text files on MAC before OS X use just CR as newline.
So I suppose in real the task is to insert after tilde not just a carriage return, but a carriage return + line-feed.
The answers on How can you find and replace text in a file using the Windows command-line environment? offer many solutions for replacing strings in text files using Windows command line.
The first suggested solution is with using JREPL.BAT written by Dave Benham.
jrepl.bat "~" "~\r\n" /X /F "FileToModify.txt" /O -
This solution works for a text file containing the posted line and produce the expected output.

Trimming unwanted characters from a large file via batch commands

I have a datafile in the following format
012394994SomeunwantedString
394949585MoreUnwantedString
348020200
349585940FurtherUnwantedString
I want to remove the unwanted strings from the file. The problem is, neither the unwanted string not the characters before the string are consistent. The only consistent part is the length of the string that is needed, and after this position I want to trim the rest of the line.
I realize that I can simply extract the characters from the left given that I know the count, but is there a more efficient manner to do this? The file contains over 80,000 lines, out of which only 10-20 will have unwanted characters.
Looking for a set of simple batch commands to get this done as this will need to run on a server.
#echo off
setlocal enabledelayedexpansion
(
for /f "delims=" %%a in (infile.name) do (
set "line=%%a"
echo(!line:~0,9!
)
)>"outfile.name"
Read each line from the input file, assign to line and echo the 9 characters starting from index 0

Windows Batch: Call function for each path in delimited string

I have a variable that contains the following:
"pdfList=D:\BundleManager\AssetPackageManager.unity;D:\BundleManager\AssetPackageManager.unity\.vs\AssetPackageManager.unity;D:\Crafting\CraftingExample.unity;D:\ResMan\FFResManExample.Unity;"
And I want to call a function for each of those paths separated by ; The code I currently have is
for /F "delims=;" %%a in ("%pdfList%") do (#echo %%a)
But this is just echoing the following
D:\BundleManager\AssetPackageManager.unity
I've tried setting tokens=* as an option but this just echos the value of pdfList complete with ; still present. Where am I going wrong?
The problem with your code is that for /F loops line-by-line, rather than token-by-token. A better solution is to use for without any switches. for will tokenize data, splitting on unquoted spaces, commas, semicolons, and tabs.
To prevent pathnames containing spaces or commas from being truncated, you can massage the data and replace all semicolons with ";" using batch variable substring substitution, then surround the whole thing in quotation marks. See this page for full details on substring replacements.
So basically,
a;b c;d e;f
becomes
a";"b c";"d e";"f
becomes
"a";"b c";"d e";"f"
which is easily tokenized, and spaces are preserved. Apply this principle to your code above:
#echo off & setlocal
set "pdfList=D:\BundleManager\AssetPackageManager.unity;D:\BundleManager\AssetPackageManager.unity\.vs\AssetPackageManager.unity;D:\Crafting\CraftingExample.unity;D:\ResMan\FFResManExample.Unity"
for %%a in ("%pdfList:;=";"%") do echo(%%~a
The output is now:
D:\BundleManager\AssetPackageManager.unity
D:\BundleManager\AssetPackageManager.unity.vs\AssetPackageManager.unity
D:\Crafting\CraftingExample.unity
D:\ResMan\FFResManExample.Unity

Batch trim characters at the EOL

i would like to create a batch file that goes through txt files in a directory and every line that has #EXTINF in it, remove the last 15 characters..
basically if it is possible, i would like to trim the last 15-20 characters from any lines that is longer than a certain number. for some reason i have a software that if the line is too long, it will screw up the data.
thanks
See How can you find and replace text in a file using the Windows command-line environment? for various options to modify text files using Windows batch.
The following simple code uses REPL.BAT to truncate long lines that begin with #extinf (case insensitive) at 600 characters. Lines that are less than 600 characters are preserved in their entirety.
type test.txt|repl.bat "^(#extinf.{593}).*$" "$1" I>test.txt.new
move /y test.txt.new test.txt >nul
User231429 wrote: "i need the script to go through all files in a directory, and each line that starts with #extinf, remove XX number of characters from the end."
The Batch file below do precisely that:
#echo off
setlocal EnableDelayedExpansion
for %%f in (*.txt) do (
(for /F "usebackq delims=" %%a in ("%%f") do (
set "line=%%a"
if "!line:~0,7!" equ "#extinf" set "line=!line:~0,-XX!"
echo !line!
)) > "%%~Nf.new"
)
REM del *.txt
REM ren *.new *.txt
Note that you must replace XX by a number in the long if command.
Test this program and check the result in *.NEW files. If the result is correct, remove REM part from two last lines.
This program remove exclamation marks from the file. This detail may be fixed, if required.

how to copy a single instance of a string from one text file into a new text file?

I have a text file with n number of rows. m number of the rows contain a string that I'm interested in (m<=n). I need a batch file that will copy only a single row (e.g. the first occurance) containing the string to a new text file. When I use the findstr command it will copy all rows containing the string.
Thanks!
Paul Safier
Given your FINDSTR command that locates your m rows (it can be as simple or as complicated as you need)
findstr "search" "fileName.txt"
then you can process the results of that command with a FOR /F loop. You can break out of the loop after the first matching line by using GOTO.
for /f "delims=" %%A in ('findstr "search" "fileName.txt"') do (
echo %%A >>"outFile.txt"
goto :break
)
:break
The FOR command is one of the more complicated commands available to batch. There are many options. You can get help on the command by typing HELP FOR or FOR /? from a command prompt.
The "DELIMS=" option disables the parsing of the line into tokens. Without that option, the FOR /F would break each line into tokens, delimited by space or tab characters. The list of delimiters can be set to other caracter(s), or in your case, set to nothing.
The code I gave above will skip lines that begin with ; because FOR /F will skip any lines that begin with the EOL character - ; by default. You can change the EOL character to any single character. But if you don't know what your matching line might start with, then you don't know what character to use for EOL. The syntax to completely disable all token parsing and EOL line skipping is odd:
for /f delims^=^ eol^= %%A in (...) do ...

Resources