Batch file -> copying and comparing ASCII files - windows

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

Related

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

Batch : how to run a program for all files in a folder and how to create an output file for the result using the original filename?

I'm a noob at batch programming so excuse me for the simple question.
I have a folder called FSC in C:\TTG\FSC, with a thousand and thousand of text files called like 1_A.txt 2_A.txt etc.
I would like to run a programm and have its output in another file renaming the original one ( for ex. 1_A.txt will have an output file called 1_A_out.txt ). I have already tested to run the programm on 1 file and it works (simply writing on the command line tag-french filename).
So how can I create the for loop for parsing each file on the folder and create the output files?
I've tried this but it doesn't work
for /F %%i in ('dir /b "c:\treetagger\FSCB1\"') do
tag-french %%i > %%i_out
I can do it in ubuntu writing this :
for l in /home/sp/Desktop/FSCM5/*
do
echo $l
filename=$(basename "$l")
extension="${filename##*.}"
filename="${filename%.*}"
filename=($(echo "/home/sp/Desktop/FSCM5TTG/"$filename"_"ttg"_."$extension))
echo $filename
sh /home/sp/Desktop/TTG/cmd/tree-tagger-french $l > $filename
done
But I would like to learn how to do this in windows too
I've tested this with success:
for /F "delims=" %%i in ('dir /b "c:\treetagger\FSCB1\*.*"') do tag-french "%%i" > "%%i_out"
I had to use the delims= option to preserve filenames even if they contained spaces.
edit: options need to be placed before the loop variable.

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.

Script hangs when executing in windows batch file

Hello I have a batch file that is used to move text file from on directory to another. The problem is that when there is a text file larger then 7 MB the scrip hangs and freezes the process, which leads to manually force the batch to end.
Why does this bat hang when it moves larger files then 7mb?. How can I solve this to let it move any size text file?
Thank you in advance for your help.
PS. TYPE was used because the original file is in ANSI/UNIX format and the only way we found to convert it to ANSI/PC was using TYPE.
cd /d "c:\users\you\"
for %%i in (*.txt) do (
echo processing %%i
TYPE "%%i" | MORE /P > "c:\temp\%%i"
del "%%i"
)
The purpose of the more command is to present only one page worth of text at a time. When the output device is not a console, the effective page length is very long but not infinite - it will still pause after writing 65534 lines.
Instead, try this:
(for /F "delims=" %%L in (%%i) do #echo %%L) > "c:\temp\%%i"
Command line breakdown:
for /F - read the contents of a file
"delims=" - don't treat spaces or tabs as delimeters
%%L - the variable (same as the %%i in the for command from your original script)
%%i - the file to read
#echo - writes the variable to standard output
( ) > file.txt - redirects standard output to the destination file

batch file to read specific letters from a text file?

I have a txt file called named.txt Is there a way to create a batch file to read every letters 2 through 7 letters from every line from name.txt and ignore the rest and output it onto a different txt file called name2.txt. For example I have this in the txt file:
G2010060sample.png
G2010061sample.png
G2010062sample.png
G2010063sample.png
and the batch file would create a new txt file like this :
2010060.png
2010061.png
2010062.png
2010063.png
Excellent online ressource http://ss64.com/nt/syntax.html
#echo off
if exist output.txt del output.txt
for /f "delims=" %%i in (input.txt) do call :ParseLine %%i
goto :eof
:ParseLine
set line=%1
set line=%line:~1,7%
echo %line%.png>> output.txt
goto :eof
cut -b 2-7,15-18 < infile.txt > outfile.txt

Resources