How to delete Linefeed using batch file - batch-file

I want to delete Linefeed in text file using batch file.
Is it possible to do.
Please provide some help

If you mean removing empty lines from text files try this in a batch file:
for /f "delims= tokens=*" %%x in (inputfile.txt) do echo %%x >> outputfile.txt
you may also want to replace inputfile.txt and outputfile.txt by %1 and %2 to be used for any file given its name to the batch file

Related

Batch read and rename from text file issue?

I'm a beginner with .bat files, and I'm attempting to rename multiple drawing (.dwg) files using a list generated in notepad. The notepad list contains a list of drawing numbers that look like this:
01013-13000p001
06301-12550p001
etc..
There is hundreds of them, and I want to take those numbers from the text and put it into a blank series of dwg files that are generic named for now (drawing.dwg, drawing(2).dwg, drawing(3).dwg etc..) I've only come up with a way to read a text file, but cant figure out how to take from the text file and rename multiple drawing files with it. Below is as far as I have gotten, after failed attempts of trying to take whats read from a text file and put it into the .dwg files. I plan on working this out in all the same directory, and any suggestions will be greatly appreciated. Thanks.
#echo off
for /f "tokens=* delims=" %%x in (dwgNumbers.txt.txt) do echo %%x
pause
would
for /f "delims=" %%x in (dwgNumbers.txt.txt) do echo copy /b "blank generic drawing.dwg" "%%x.dwg"
(as a batch line) do what you want? - note that the ECHO keyword is there to show what would be done. The echo keyword needs to be removed to actually execute the copy.
This will take the numbers from .txt, renaming the existing .dwg files with the data readed. If there are more files that numbers in .txt, it will rename until number exhaustion, no more.
for loop is using a dir command to get the list of files to avoid the case of files that after being renamed falls under the filter of the for and gets reprocessed.
This code has a echo command included in rename line to prevent data loss in case of malfunction. When the output to console is what is needed, remove the echo command from the rename line.
#echo off
rem Prepare environment
setlocal enableextensions enabledelayedexpansion
rem Read input file for numbers
< dwgNumbers.txt (
rem Process dwg files in directory
for /f "tokens=*" %%f in ('dir /b *.dwg') do (
rem Get number from input file
set "number="
set /p "number="
rem If there is a number, rename the file
if defined number echo ren "%%~f" "!number!.dwg"
)
)
rem Clean
endlocal

Reading input from a file doesn't work for filenames containing spaces

I have a case where I have to move over 15,000 files. There are many duplicates, so I parsed them by MD5 and now I have a list of file names in an input file (files.txt).
I want to read from it, then copy the listed files to a new directory.
I pulled an old batch that someone had written as a two-part simple script and modified it.
It works for files without spaces. How can I get this to cover all file names?
Also, can't I put all of this into one file?
Cstart.bat:
for /f %%x in (files.txt) do call copyfiles.bat
copyfiles.bat:
set filename=%1
copy "C:\temp\%filename%" "C:\temp\pruned files\"
Your current code doesn't even pass the filename to copyfiles.bat, so it's not working with or without spaces. (If you need to confirm that, add echo %1 %filename & pause before the copy line in copyfiles.bat and run cstart.bat.)
With that being said, you can do it all in one file easily:
for /f %%x "tokens=1 delims=*" in (files.txt) do copy "C:\Temp\%%x" "C:\Temp\pruned files\%%x"
To make sure it works, just replace the copy in the line above with echo and run it from a command prompt.
I tested this with a text file named test.txt that contained the following:
One.txt
Two.txt
Three.txt
And Four.txt
with a batch file named testcopy.bat containing this:
#echo off
for /f "tokens=1 delims=*" %%x in (test.txt) do echo "C:\Temp\%%x" "C:\Temp\test this\%%x"
The above test showed this output:
e:\TempFiles>testcopy
"C:\Temp\One.txt" "C:\Temp\test this\One.txt"
"C:\Temp\Two.txt" "C:\Temp\test this\Two.txt"
"C:\Temp\Three.txt" "C:\Temp\test this\Three.txt"
"C:\Temp\And Four.txt" "C:\Temp\test this\And Four.txt"
for /f "usebackqdelims=" %%x in ("my file list.txt") do copy "C:\temp\%%~x" "C:\temp\pruned files"

copy file into directory based on filename

I have thousands of files to move.
I have already used a batch file to create the directories I need.
My file names look like this:
6711_05_12.pdf
10504_06_15.pdf
559_07_11.pdf
The first characters up to the "_" are the directory the files need to go into. Started the batch file - but don't know how to identify the file name.
#echo off
setlocal EnableDelayedExpansion
for %%I in (*.pdf) do (
xcopy ???
)
Is there a manual for batch files?
You don't even need a batch file. The following one line command will do the trick.
for %I in (*.pdf) do #for /f "eol=_ delims=_" %A in ("%I") do #copy "%I" "%A"
Simply double up the percents if you want to put the command in a batch file.

Batch File to rename txt file

I need to write a batch file that renames a text file with the file content. for example flie1.txt containing the word alpha becomes alpha.txt. I just some tips of what commands to use to create this batch file.
This only uses the first line or word in the file:
set/p var=<file.txt ---Setting a variable to the content inside the file.
ren file.txt %var%.txt ---Renaming the file to eh assigned variable.
windows move. unix mv
Example
Windows:
move my_original_file_name.txt my_new_file_name.txt
Unix:
mv my_original_file_name.txt my_new_file_name.txt
Your specific task in Windows:
SetLocal EnableDelayedExpansion
set your_file_location=%userprofile%
set your_file_name=!your_file_location!\file1.txt
for "eol=; tokens=1 delims=" %%n in ('type "!your_file_name!"') do (
set new_file_name=%%n
)
move "!your_file_name!" "!your_file_location!\!new_file_name!.txt"

batch file to append file names to end of lines

I have a directory of files and I need to loop through the files, outputting the content of each file to a new file while appending the filename to the end of each line.
So from a directory of 10 files I want to end up with 1 file made up of the contents of each file but with each filename on the end of each line so i know which file it originaly came from.
I can append fixed text to the end of each line but I can't work out how to use the file name and also change it to work for any file. I've tried using a wildcard((%~dp0*.csv) but it says it cant find the file specified.
This is what I have so far:
for /F "delims=" %%j in (%~dp0\6691_706.csv) do echo.%%jAddToEndofLine >> %~dp0\New.txt
Can anyone help? Thanks.
It fails, as FOR /F doesn't allow wildcards for files, it is intended for reading a file line by line.
You should use the "normal" FOR
for %%A in (%~dp0\*.bat) do (
echo Processing file '%%A'
FOR /F "delims=" %%L in (%%A) do (
echo Line %%L from file %%A >> %~dp0\New.txt
)
)

Resources