.bat file to loop through folder and append text files
Date : March 29 2020, 07:55 AM
around this issue Windows batch does not have a native command to edit a file in place (other than to append data to it). So for each file, you need to create a temporary file with the desired content and then delete the original and rename the temp to the original. The delete and rename can be accomplished with a single MOVE command. @echo off
set "header=c:\SomeFolder\Headings.txt"
set "folder=c:\SomeFolder\FolderWithTextFiles"
set "tempFile=%folder%\temp.txt"
for %%F in ("%folder%\*.txt") do (
type "%header%" >"%tempFile%"
type "%%F" >>"%tempFile%"
move /y "%tempFile%" "%%F" >nul
)
|
Merge multiple text files and append current file name at the end of each line
Date : March 29 2020, 07:55 AM
will help you Text::CSV can be used to parse CSV. The following script is to be run from within the directory containing the CSV files. It is not recursive (a glob has been used). If you require it to recursively find files, you can use the File::Find Perl module. #!/usr/bin/env perl
use strict;
use warnings;
use Text::CSV;
my $csv = Text::CSV->new( { 'sep_char' => ';' } );
open my $fho, '>', 'combined.csv' or die "Error opening file: $!";
while ( my $file = <*.csv> ) {
open my $fhi, '<', $file or die "Error opening file: $!";
( my $last_field = $file ) =~ s/\.[^\.]+$//; # Strip the file extension off
while ( my $row = $csv->getline($fhi) ) {
$csv->combine( @$row, $last_field ); # Construct new row by appending the file name without the extension
print $fho $csv->string, "\n"; # Write the combined string to combined.csv
}
}
|
VBscript: Loop through all text files in a folder, delete the first and last line of each file and merge into one file
Date : March 29 2020, 07:55 AM
this one helps. Always use option explicit statement. Doing that helps to reveal errors in syntax and in logic as well. Here are some code improvement hints: Set testfile = objFSO.OpenTextFile(file.path, ForReading)
line = ""
linecount = 1 ' or 0 ?
Do Until testfile.AtEndOfStream
If linecount <= 1 Then
testfile.SkipLine
line = ""
Else
if Not Trim(line) = "" Then outFile.WriteLine(line)
line = testfile.ReadLine
End If
linecount = linecount +1
Loop
testfile.Close
Set testfile = objFSO.OpenTextFile(file.path, ForReading)
line = ""
if not testfile.AtEndOfStream then testfile.SkipLine
Do Until testfile.AtEndOfStream
if Not Trim(line) = "" Then outFile.WriteLine(line)
line = testfile.ReadLine
Loop
testfile.Close
Set testfile = objFSO.OpenTextFile(file.path, ForReading)
set line=Nothing
if not testfile.AtEndOfStream then testfile.SkipLine
Do Until testfile.AtEndOfStream
if Not IsObject(line) Then outFile.WriteLine(line)
line = testfile.ReadLine
Loop
testfile.Close
|
Append Text to End of File Name for Multiple .xlsx Files in a Folder
Date : March 29 2020, 07:55 AM
Does that help This should work. I changed If Right(myFileName, 5) = ".xlsx" Then to... If Right(myFileName, 4) = ".xlsx" Then Sub RenameFiles()
Dim myFilePath As String, myFileName, NewFileName As String
Dim objFSO As Object
Dim objFolder As Object
Dim objFile As Object
myFilePath = "C:\Temp\"
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFolder = objFSO.GetFolder(myFilePath)
For Each objFile In objFolder.Files
myFileName = objFile.Name
If Right(myFileName, 5) = ".xlsx" Then
NewFileName = Replace(myFileName, ".xlsx", "-MN.xlsx")
Name myFilePath & objFile.Name As myFilePath & NewFileName
End If
Next objFile
End Sub
|
Merge 2 text files to one in every subfolder and save them in the same Folder as Date and File named csv file.
Tag : file , By : Funkwarrior
Date : March 29 2020, 07:55 AM
Hope that helps There is no such token as G%%. If you want to copy the file from a .txt extension to .csv use %%~nG for name of file, excluding extension. @echo off
for /r "C:\Users\Juan\Desktop\Konturograph\" %%G in (*.txt) do (
xcopy "%%G" "%%~nG %date% %time:~0,2%-%time:~3,2%-%time:~6,2%.csv"
echo "%%G"
)
pause
@echo off
for /r "C:\Users\Juan\Desktop\Konturograph\" %%G in (*.txt) do (
type %%G >> OUTPUT.csv
echo "%%G"
)
pause
|