Batch file for file merge - windows

I have one folder inside that there 15 sub folder Folder1, Folder2, Folder3, etc... Each subfolder (Folder1, Folder2) has 2 or 3 level subfolders. Each folder has some .txt files. I would like to create a batch file that can 15 create merge files for each folder.
I can do it with cmd commands like for %f in (*.txt) do type "%f" >> output.txt.
In the above command, I need to open 15 different command prompt windows and run the above command.
Is there any way that I can achieve this programmatically like in a batch file?

The following batch will create in all direct subfolders of Mainfolder a file Merge.txt containing all text files of this subfolder and below.
It inserts a Header with full path for each text file and appends an empty line to avoid joining lines if the last line of a file doesn't end with a cr/lf.
#Echo off
Set "MainFolder=C:\PathTo\wherever"
For /D %%A in ("%MainFolder%\*") do (
For /F "Delims=" %%B in ('Dir /B/S "%%A\*.txt" 2^>NUL^|Find /V "Merge.txt"') Do (
Echo ==== %%~fB ====
Type "%%~fB"
Echo:
) "
) > "%%~fA\Merge.txt

Related

Bat script to find and replace files in multiple subfolders - replace .java files with .class files from specific folder

I am new to windows batch scripting .. please help on this scenario
I have file structures as below ::
dir1:
c:\workspace\changeset\com\folder
subfolder1
one.java
subfolder-2
two.java
dir2:
c:\workspace\target\class\com\folder
subfolder1
one.class
subfolder2
two.class
Subfolder3
three.class
Need to find and replace dir1 files in respective subfolders i.e one.java and two.java from dir2 files i.e one.class and two.class ( need to replace certain .java files with .class files from specific folder )
much appreciated for your help.
Thanks
Arjun
for /f "delims=" %%a in ('dir /b /a-d "c:\workspace\changeset\com\folder\*.java"') do (
if exist "c:\workspace\target\class\com\folder\%%~na.class" (
echo copy "c:\workspace\target\class\com\folder\%%~na.class" "c:\workspace\changeset\com\folder\%%a")
)
The required COPY commands are merely ECHOed for testing purposes. After you've verified that the commands are correct, change ECHO COPY to COPY to actually copy the files. Append >nul to suppress report messages (eg. 1 file copied)
Note that execution directly from the prompt and as lines with a batch file are different. The metavariable (loop-control variable) %%x must be referenced as %%x for a batch line and %x if executed from the command prompt. Since it makes little sense to repeatedly execute a line containing a for from the prompt (it's easier to create a batch file), I post the batch-file version. User's responsibility to adjust for direct-from-prompt if desired.
Read each filename in /b basic form /a-d without directories and assign the filename+extension to %%a.
If a file in the other directory called thenamepartofthefile.class exists, then copy that file to the first directory.
Please post the entire problem to be solved. The approach can change radically, as in this case.
#ECHO OFF
SETLOCAL ENABLEDELAYEDEXPANSION
SET "sourcedir1=U:\sourcedir\changeset"
SET "sourcedir2=U:\sourcedir\target"
FOR /f "delims=" %%a IN (
'dir /b /s /a-d "%sourcedir1%\*.java" '
) DO (
SET "javadir=%%~dpa"
SET "classfile=!javadir:%sourcedir1%=%sourcedir2%!%%~na.class"
IF EXIST !classfile! ECHO COPY "!classfile!" "%%a"
)
GOTO :EOF
You would need to change the settings of sourcedir1 and sourcedir2 to suit your circumstances.
Essentially, the same approach and the same comments re messages. The difference is that this procedure uses files and subdirectories in the dir list and substitutes the first part of the directoryname in deriving the expected name of the .class file.

How to create folders and files from text file with same names to insert with corresponding name?

I was inspired on works of our colleagues from portal
Have a batch create text files based on a list
Batch script to read input text file and make text file for each line of input text file
to develop creation from a text file for example
"comps.txt" ( comp1,comp2....
for each PC a batch script read from a list of PCs in a text file and create a text file for each line of text as local.
Later in code are created folders files named - the same names ( comp1,comp2....
at the end we have text files : comp1.txt ,comp2.txt ... and folders: comp1 , comp2.... till 400 computers.
Any idea how to add in code or write another separate batch code to move for each coresponding folder text file for text file
comp1.txt->comp1 folder
comp2.txt->comp2.txt
....
We have more than 400 lines!!
I am very begginers for any scrips working very hard every single day and this is my first question. My code in Windows Batch is below
#echo off
setlocal
for /f "tokens=*" %%a in (comps.txt) do (type nul>"%%a.txt")
for /f "tokens=*" %%a in (comps.txt) do (
echo This is line 1 of text>"%%a.txt"
)
for /f %%i in (comps.txt) do mkdir %%i
do (
echo "%%i.txt"
)
endlocal
[]
It doesn't make any sense to generate text files and then folders to move the files to. Create the folders first place and create the files into the folders. Using a (code block) only one for loop is needed.
#echo off
setlocal
for /f "tokens=*" %%a in (comps.txt) do (
if not exist "%%a" mkdir "%%a"
echo This is line 1 of text>"%%a\%%a.txt"
)
endlocal

Apply a batch script to all the csv file of my directory

I have a script that allow me to extract a specific columns from a csv file.
My aim subject is to apply my script to all csv files of my directory.
#echo off>fourcol.csv
setlocal
for /f "tokens=1-22* delims=," %%1 in (ManyColumns.csv) do (
echo %%4,%%5,%%6>>fourcol.csv
)
type fourcol.csv
Apply the script to all the csv file in my directory
Remove duplicates
FileA.csv ...FileZ.csv
server1,Dell,1.0.1,server1,Dell,28/06/2016,...
server2,Hp,1.0.2,server2,Hp,29/06/2016,...
server3,Dell,1.2.1,server3,Dell,30/06/2016,...
server4,Hp,1.3.1,server4,Hp,27/06/2016,...
server3,Dell,1.2.1,server3,Dell,30/06/2016,...
server4,Hp,1.3.1,server4,Hp,27/06/2016,...
My CSV files have the same header and the same data.
Output after applying the script (without duplicates):
server1,Dell,1.0.1
server2,Hp,1.0.2
server3,Dell,1.2.1
server4,Hp,1.3.1
How do I apply the script to all the csv files in my directory?
A couple of small changes should do it:
Don't use numbers as for loop variables. They are reserved for command line parameters.
Use dir /b *.csv to get the list of files to process.
Start from token 1 not token 4 to extract the first three columns.
Use the first for loop to process each file, and a second (nested) for loop to process the lines within the file.
Note:
delims=, may not do what you want (if there are values in the file that contain commas ,).
Try the following batch file (test.cmd):
#echo off
setlocal
rem remove output file if it already exists
if exist fourcol.csv del fourcol.csv
rem loop through the csv files
for /f "usebackq" %%a in (`dir /b *.csv`) do (
rem loop through the lines in each file
for /f "usebackq tokens=1,2,3 delims=," %%b in (%%a) do (
echo %%b,%%c,%%d>>fourcol.csv
)
)
rem add code here to strip duplicates
endlocal
test.csv:
server1,Dell,1.0.1,server1,Dell,28/06/2016,...
server2,Hp,1.0.2,server2,Hp,29/06/2016,...
server3,Dell,1.2.1,server3,Dell,30/06/2016,...
server4,Hp,1.3.1,server4,Hp,27/06/2016,...
server3,Dell,1.2.1,server3,Dell,30/06/2016,...
server4,Hp,1.3.1,server4,Hp,27/06/2016,...
fourcol.csv:
server1,Dell,1.0.1
server2,Hp,1.0.2
server3,Dell,1.2.1
server4,Hp,1.3.1
server3,Dell,1.2.1
server4,Hp,1.3.1
Further Reading
An A-Z Index of the Windows CMD command line - An excellent reference for all things Windows cmd line related.
dir - Display a list of files and subfolders.
for /f - Loop command against the results of another command.

Batch file -> copying and comparing ASCII files

Can anyone help me write this batch file?
If there is no csv file inside "copy\" folder, batch file to copy
"original\file.csv" to "copy\file.txt"
Batch file to generate another csv file with the differences
between the newest .OK and the "copy/file.txt".
First point:
if not exist "copy\file.csv" copy "original\file.csv" "copy\file.csv"
Second point:
#echo off
( for /f "delims=" %%i in (file.txt) do (
findstr /C:"%%i" file.OK >nul || echo %%i
)
)>out.txt
Pseudo-Code for better understanding:
For each line in file text do:
Does this line exist in file.OK? If not, write the line...
...to out.txt

batch file to list all txt files from other directories

I am in the process of fetching all the .txt files from one directory to another (my current).
My current directory is
C:\USERS\MRAH
where i have the batch file and i have the code to fetch all .TXT files from the directory
dir E:\S_RUNS\12 month_STAR\S_2013\tst\*.txt /b >> INPUT_FILE_LIST.TXT
I am not able to fetch all the .TXT file which are in the E:\ DIREC into INPUT_FILE_LIST.TXT file on C:\USERS\MRAH
Can anyone let me know as to what should be code to fetch all the .txt file from one directory to another...
Thanks!
I'm not completely sure this will work on multiple directories but you could try it.
Cd E:\[path]
for /d %%a in (*) do (if %~xa == .txt echo %%a >> input_list.tmp)
for /f %%a in (input_list.tmp) do (copy %%a C:\USERS\MRAH)
note the batch file needs to be run from the E:[path]
also note you save it as a .tmp file to prevent it from logging itself
also instead of making an input_list file do it directly:
for /d %%a in (*) do (if %~xa == .txt copy %%a C:\users\MRAH)
Tell me if this doesn't work
Yours, Mona
Assume your current working directory is c:\testDir and you wanna copy all txt files from c:\source to d:\dest then use following content in a batch file
copy c:\source*.txt d:\dest

Resources