Create multiple text files based on existing files - for-loop

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.

Related

Renaming a list of 'Filename' inside a folder, with Prefix+'(existing)Filename' . For every file. using command promt/batch file

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.

Command prompt batch renaming results in syntax error

I need to rename 80k files in multiple folders & subfolders in the same directory. I have been trying to use ren but have been unsuccessful; I get an incorrect syntax error.
My old name looks like this:
c:/users/alice/BiDIR_DOCS_2017_Nov08020423\Company,LLC##NA##7967425.00##7967425.00\Company LLC A and A - Aug2017.pdf BiDIR_DOCS_2017_Nov08020423\Company, LLC##NA##7967425.00##7967425.00\document_# (x.y.z)-test~.pdf
and my new name looks like this:
c:/users/alice/BiDIR_DOCS_2017_Nov08020423\Company,LLC##NA##7967425.00##7967425.00\Company LLC A and A - Aug2017.pdf BiDIR_DOCS_2017_Nov08020423\Company, LLC##NA##7967425.00##7967425.00\system, a old name~ ` to # system b document (xyz)-test.pdf
I have the existing directory print in one column of Excel and in the next column what I want the directory print to be.
I'm not sure if I'm starting my ren command at the right hierarchy of my directory, or if I need quotation marks to keep the spaces and symbols in my new name.
I have tried improvising and testing on my own without success and I cannot find an article online on point.
Try FAR (find and replace) - it a free utility that works well.
http://findandreplace.sourceforge.net/

wanted to use results of find command in custom script that i am building

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.

Renaming files of variable length in DOS

I'm trying to rename files that have '#' to '_'.
I understand that there is a straigt forward way of replacing the nth character in a file.
How do we rename files , if # symbol is present in different places in different files
For example, assuming the below files are present in a directory
a#file.txt
asdf#kfi.png
uiuydfjfk#kdi.txt
I want the output to be like this one
a_file.txt
asdf_kfi.png
uiuydfjfk_kdi.txt
Is there anyway to accomplish this ?
This uses a helper batch file called repl.bat from - http://www.dostips.com/forum/viewtopic.php?f=3&t=3855
It creates renfile.bat for you to examine for errors, and then execute.
dir *#* /b |repl "(.*)(#)(.*)" "ren \x22$&\x22 \x22$1_$3\x22" x >renfile.bat

Windows Batch file Dynamic create statements

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).

Resources