I am trying to write to xml file using a batch script, however it only writes the last loop to the file. The variable Project_Choice is just two comma separated strings. It prints to the console correctly, however writes only the last loop in the xml file. Where am I going wrong?
for %%i in (%Project_choice%) do (
echo(^<?xml version="1.0" encoding="utf-16" ?^>
echo(^<testConfiguration^>
echo(^<TestEvents^>
echo(^<TestEvent^>%%i^</TestEvent^>
echo(^</TestEvents^>
echo(^</testConfiguration^>
)> C:\cijobscript\Test.xml
> creates a new file. >> appends to an existing file. Change the > to >> BUT this will append the new data to any existing file, so you'd need to delete it first.
-- OR --
use
(for .... do (echo....))>filename
to create a new file
Note here the for command itself is within parentheses.
Related
For example : Suppose we have 4 files inside a folder,
1656088200108CONTPE_CONR.txt
1656174600239CONTPE_NTSL.txt
1656261000254CONTPE_PMLL.txt
1656174600114CONTPE_CONR.txt
I wanted to add 'CONTPE_' in the beginning of every file.
Then i want to replace the filename as :
CONTPE_1656088200108CONTPE_CONR.txt
CONTPE_1656174600239CONTPE_NTSL.txt
CONTPE_1656261000254CONTPE_PMLL.txt
CONTPE_1656174600114CONTPE_CONR.txt
But there are incremental files in the folder. So destination code shouldn't be static/pre-defined.
I tried to do various code like:
ren *CONTPE*.* CONTPE_*
ren *CONTPE*.* CONTPE_????*.*
All the above methods, replaces the first characters in filename with CONTPE, thus outputs are like CONTPE_000254CONTPE_PMLL.txt, which is basically trimming the actual filename however I wanted as CONTPE_1656261000254CONTPE_PMLL.txt.
Screenshot will describe the problem.
I have a large number of existing files. I want to create new text files that include the name of the existing files plus some additional text and then names the file with a different extension based on the original file name. I can use command or powershell.
For example.
Current filenames
123.yuv
456.yuv
Content in new (separate) files
123.yuv Hello World123
456.yuv Hello World546
New filenames
123.avs
456.avs
What I've tried: I created this simple code that puts all the output into a single file. I can then go through some gyrations to create separate files. But it would be easier to just create the separate files straight away. I've researched but was not able to figure it out.
(for %i in (*.yuv) do #echo %i Hello world%i) > "%~ni.avs"
The following code should accomplish what you desire:
for %i in ("*.yuv") do > "%~ni.avs" echo/%~i Hello World%~ni
Since in your code you placed parentheses around the whole for loop, the redirection > happens outside of the loop context, hence the loop variable %~ni is not available.
I want to validate my XML's for well-formed ness, but some of my files are not having a single root (which is fine as per business req eg. <ri>...</ri><ri>..</ri> is valid xml in my context) , xmlwf can do this, but it flags out a file if it's not having single root, So wanted to build a custom script which internally uses xmlwf, my custom script should do below,
iterate through list of files passed as input (eg. sample.xml or s*.xml or *.xml)
for each file prepare a temporary file as <A>+contents of file+</A>
and call xmlwf on that temp file,
Can some one help on this?
You could add text to the beginning and end of the file using cat and bash, so that your file has a root added to it for validation purposes.
cat <(echo '<root>') sample.xml <(echo '</root>') | xmlwf
This way you don't need to write temporary files out.
I'm creating a batch file and the final part of the batch is to concatenate the text from multiple csv files into one. The csv that is created is correct, however when I run the command it shows the file names on a separate line. Even with echo off it still shows. When I simply copy and paste the line into a blank txt file and use #echo off and that's it.. it still shows the output.
type lak-print01.csv lak-print02.csv lak-print03.csv lak-print04.csv or-sdc-print.csv tac-print01.csv tac-print02.csv tuk-print01.csv wa-02print01.csv wa-110print01.csv wa-61print01.csv > MasterPrinterList.csv
The output shown in cmd line is as follows. I just want it to perform the type command w/o showing the files.
lak-print02.csv
lak-print03.csv
lak-print04.csv
or-sdc-print.csv
tac-print01.csv
tac-print02.csv
tuk-print01.csv
wa-02print01.csv
wa-110print01.csv
wa-61print01.csv
Any help?
Redirect StdErr to nul by appending 2>nul. (type outputs filenames into that.) The command you want is this:
type lak-print01.csv lak-print02.csv lak-print03.csv lak-print04.csv or-sdc-print.csv tac-print01.csv tac-print02.csv tuk-print01.csv wa-02print01.csv wa-110print01.csv wa-61print01.csv > MasterPrinterList.csv 2>nul
type lak-print01.csv lak-print02.csv lak-print03.csv lak-print04.csv or-sdc-print.csv tac-print01.csv tac-print02.csv tuk-print01.csv wa-02print01.csv wa-110print01.csv wa-61print01.csv > MasterPrinterList.csv 2>nul
the file names are printed in error stream so all you need is to add 2>nul at the end.
type also accepts wildcards so you can make your line shorter e.g
type lak*.csv or*.csv tac*.csv wa*.csv > MasterPrinterList.csv 2>nul
I need to get a list of file names from a directory using a windows batch program. I would like to take each FILE NAME and combine that with another command line statement.
Note i only need the file name not the contents of the file.
How would this be done?
If i have a 'Data' directory on the D drive with the below files (note there could be many files)
--------------
myFile1.abc
myfile2.abc
------------------
How could i dynamically create something like this using a windows batch program?
move C:\myFile1.abc C:\newdir
move C:\myFile2.abc C:\newdir
note - (i know there is a easier way move files but but i am trying to understand the logic so i can use it in a different command)
You can use a for loop:
for %%X in (D:\*) do (
echo move %%X C:\newdir
)
Try on the command line:
for %X in (D:\DataFiles\*) do echo move "%~fX" C:\newdir>>logfile.txt
It puts all file names from D:\DataFiles in logfile.txt (except hidden files).