grouping sorted list using batch file - batch-file

I want to combine several files into a single one using a flat reference file. My flat file looks as follows :
master_A | SubA | SubSubA
master_A | SubA | SubSubB
master_A | SubA | SubSubC
master_A | SubB | SubSubA
master_B | SubC | SubSubA
master_B | SubC | SubSubB
...
Column B and C combined define my file location on disc and I want to use this flow to generate single files based on the grouped values of column A (the masters).
In other words, above example would result is 2 files, and the sub files would be added. So the flow would be as follows:
Generate a file called master_A.txt and add the content of SubA_SubSubA.txt, SubA_SubSubB.txt, SubA_SubSubC.txt and SubB_SubSubA.txt.
Next, generate master_B and add the content of SubC_SubSubA.txt and SubC_SubSubB.txt
And continue until end of reference file...
I didn't get much further than below to be honest...
for /F "tokens=1,2,3" %%i in (myFlatFile.txt) do (
set myMaster=%%i
set myFile=%%j_%%k.txt
rem if myMaster differs from the previous one generate a new file using myMaster
rem as filename, then add myFile content untill the master changes again.
rem What would be the best way to achieve this ?
)

#echo off
for /F "tokens=1-3 delims=| " %%i in (myFlatFile.txt) do (
type %%j_%%k.txt >> %%i.txt
)
The only problem with previous code is if the master... files may exist before the program run. In this case, a previous file delete cycle fix the problem.

Related

Batch file how to insert the text in next column?

I have an issue on inserting data into an excel sheet.
Example: I have bat file like below;
echo %computername%; %userdomain% ; %username%; %date% ; %time% >>
C:\Server\user\palani\logout.csv
This bat file returns the output like this:
ABCD-D-1256G; DIR ; viswa; Wed 09/02/2015 ; 19:17:11.85
The output below is stored in excel sheet single column.
But I want to output a separate column like this:
|ABCD-D-1256G |DIR | viswa |Wed 09/02/2015 | 19:17:11.85
Could you please guide me how to use the tab function or delimiter?
To add a new line you should add a:
& echo.
instead of the >>.
If I understand you're question right.
Your syntax is good, the only problem is the delimiter you use. check what is your List separator in your computer regional settings (control panel -> region and Language -> additionnal settings -> List separator).
Make sure your echo ouput the same thing to separate column than your computer regional settings. If your List separator is set to colon then you need to change your code to look like this:
echo %computername%,%userdomain%,%username%,%date%,%time% >> C:\Server\user\palani\logout.csv

Array objects with whitespace in names not conjoined

AVAILABLEDIR=("${AVAILABLEDIR[#]}" "$(ls $LOC -AFl | sed "1 d" | grep "/$" | awk '{ print $9,$10 }')")
I'm trying to create an array using this command, however when it adds objects to the array, it adds $9 and $10 seperately, is there a way to tell the array to have both of these arguments joined? This is what I want:
[Directory 1/] [Directory 2/] [Directory 3/]
instead of
[Directory] [1/] [Directory] [2/] [Directory] [3/]
Thank you for your help
Don't use a pipeline headed by ls for this; just use a glob.
pushd "$LOC"
AVAILABLEDIR+=( */ )
popd
pushd works like cd, but saves the current directory on a stack before changing. */ is a pattern that matches all directory names in the current directory; += appends the matching directories to the current value of AVAILABLEDIR. popd removes the directory name from the top of the stack and cds there. The pushd/popd combination is the easiest way to add Directory 1, rather than $LOC/Directory 1, to the array.

How to add a folder to 7z archive using 7za?

I am having a bad time with the standalone version of 7-Zip. I want to compress a folder with a password. I tried: 7za a my_folder -pmy_password. It's compressing all the files in which 7za.exe is located with file name of my_folder.
BTW: I am using a scripting language called AutoIt.
As per Command Line Syntax (7zip.chm) > Contents > Command Line Version > Syntax :
7za <command> [<switch>...] <base_archive_name> [<arguments>...]
<arguments> ::= <switch> | <wildcard> | <filename> | <list_file>
<switch>::= <switch_symbol><switch_characters>[<option>]
<switch_symbol> ::= '/' | '-'
<list_file> ::= #{filename}
a is the command.
-pmy_password is a switch and should be therefore after the command and not at end. But switches can be also appended after base archive file name although this makes it more difficult to read and parse.
my_folder should be an argument, but is interpreted as base archive file name because you have not specified any archive file name.
So try:
7za.exe a -r -pmy_password MyArchive.zip my_folder

Batch file to convert quote-enclosed .csv to fixed width columns

I have been tasked with getting data output from a handheld scanner into a legacy application. The scanner program does not give me any options for formatting output.
I have researched and played with this, but my DOS scripting experience is limited. I found this on your site:
Batch file convert comma delimited to fixed length
but it fails due to the quote-marks; and it does not have the capability to space fill to a desired column width.
The Input file below is what comes from the scanner. The Desired Output is what I need it to look like for the legacy application. The'|' ending each line is not desired. I used it to indicate there are trailing spaces. The output is 8 characters, a comma, then 14 characters; left justified; space filled to the right.
I have another file that has three columns 8,14,14.
Basically, I need to go from .csv to fixed width fields separated by commas.
Input:
"20009","01138913"
"20009","01138915"
"20009","01138916"
"20009","01138914"
"20009","01138918"
"20009","01138920"
"20009","01138919"
Desired output:
20009 ,01138913 |
20009 ,01138915 |
20009 ,01138916 |
20009 ,01138914 |
20009 ,01138918 |
20009 ,01138910 |
20009 ,01138919 |
Any help would be greatly appreciated.
You can use the following, which should also be robust for columns without quotation marks:
#echo off
for /f "tokens=1,2 delims=," %%x in (test.csv) do call :process %%x %%y
goto :eof
:process
set "A=%~1 "
set "B=%~2 "
set "A=%A:~0,8%"
set "B=%B:~0,14%"
(echo %A%,%B%)
There are a few tricks here. First for /f can be used to sort-of parse CSV. We get around the quotes by passing the two column values to a subroutine. As parameters can be quoted or not and we use %~1 to access them, which removes quotes if present, the quotes pose no problem here. %A% and %B% are set to the column values with lots of padding space to the right and the following two lines select the value with the appropriate padding. Then we simply output the lines one by one.
To get an output file, simply redirect into a new file, or add >> out.txt to the last line.
However, if at all you can use other tools, then by all means do so. E.g. in PowerShell this would be fairly trivial:
Parse the file
Import-Csv test.csv -Delimiter ',' -Header A,B |
Output, using a format string
ForEach-Object { '{0,-8},{1,-14}' -f $_.A,$_.B } |
Write to a new file
Out-File out.txt -Encoding Default

2 level file loop with variable

I am new to VBS and I am trying to do something I am not sure how.
First level:
I have a set of varible names(strVar()) that I want to use them to 1 check to see if the folder exsists and then if they do not exsits create a folder
strVar(0) = "AA
strVar(1) = "AB"
strVar(2) = "AK"
strRootFolder = "C:\IN\ED\Dat\" & strVar
strSourceFolder = X:\\IN\ED\Dat\" & strVar
Once the File structure is there as shown in strRootFolder I need to put another folder in each of those folders called "CAL"
So my file structure for example would be-
"C:\IN\ED\Dat\AA\Cal\"
"C:\IN\ED\Dat\AB\Cal\"
"C:\IN\ED\Dat\AK\Cal\"
Second level:
- There will be a multiple files(number unknown) that will have the same fileType as the Variable in my list. So it would look like (C:\IN\ED\Dat\AA\Cal\testfile.AA). I need to be able to go through Each file in each folder and compare the last date modified to the strSourceFolders last date modified and if source is newer replace the rootfolders file.
So
If not strRootFolder & "*.AA" = strSourceFolder & "*.AA" Then
Copyfile strsourcefolder ---> strRootFolder

Resources