1

I have a source file blue01.environment (text file)

I need to copy the text in this file and paste it into all of the below files, but saving them with their original name.

blue02.environment
yellow01.environment
yellow02.environment
yellow03.environment
purple01.environment
purple02.environment
...

Somebody could point me towards a batch command I could use?

4
  • 1
    The other files already have some content? Can it be deleted? Jun 28, 2019 at 23:58
  • Are all the files in the same directory? Jun 29, 2019 at 0:02
  • The files are in the same directory, but I can split them to have 1 master file in a directory, then all 79 files in a sub directory. I will be copying the 79 files out of the directory when they are all copied and saved.
    – Momo
    Jun 29, 2019 at 0:14
  • Also, the other files do have content in them, but yes I would like it all overwritten.
    – Momo
    Jun 29, 2019 at 0:15

2 Answers 2

1

Try this:

#!/bin/bash

# the file you want the content to be copied
master=/your_dir/master_file

# get the content
content="$(cat $master)"

# loop the files .environment
for file in /your_dir/*.environment; do 
  # if the file is not the master file copy the content
  [ "$file" != "$master" ] && echo "$content" > "$file"
done
0

What about this :

rem // Define constants here:
set "_ROOT=." & rem // (path to root directory; `.` is current, `%~dp0.` is batch file parent)
set "_MASK=*.environment" & rem // (pattern to match files to process)
set "_MASTER=blue01.environment" & rem // (name of master file)

rem // Change to root directory:
pushd "%_ROOT%" && (
    // Iterate over all matching files:
    for %%I in ("%_MASK%") do @(
        rem // Exclude master file to be overwritten:
        if /I not "%%~nxI" == "%_MASTER%" (
            rem // Copy master file onto current file:
            copy /Y "%_MASTER%" "%%~I" > nul
        )
    )
    rem // Return from root directory:
    popd
)

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .