help scripting way to copy/rename one file to subfolders

ron7000

New member
Power User
Local time
7:50 PM
Messages
90
i have a bunch of files on F: drive. There are many subfolders. i have one file called 'F:\master' whos data i want to copy to another file within the subfolders having some unknown name- the only thing i know is the suffix of the file name. so i have folders a, b, c, d, and so on. within each folder are a bunch of files named whatever. for every .txt file in the subfolders i want to copy the file 'master' to.
for example in folder 'a' if i have file named 'hello.txt', what i want to happen essentially is 'copy F:\master F:\a\hello.txt'. If i do a 'dir F:\a\*.txt' and find it has 100 files called hello1.txt, hello2.txt and so on then i need 'copy f:\master f:\a\hello1.txt' and to hello2.txt hello3.txt and so on.

how can i accomplish this?
thanks.
 

My Computer My Computer

At a glance

Windows 7 x64, ultimate/pro/home, SLES x86 & ...intel & amdyesgtx970
Computer type
PC/Desktop
Computer Manufacturer/Model Number
homebuilt & dell & sgi
OS
Windows 7 x64, ultimate/pro/home, SLES x86 & ia64
CPU
intel & amd
Motherboard
yes
Memory
yes
Graphics Card(s)
gtx970
Sound Card
same as memory
Monitor(s) Displays
benq 27"
Screen Resolution
2560*1440
Hard Drives
many
So you want all TXT files inside a folder (and its subfolders) to be replaced by master?
why?
 

My Computer My Computer

At a glance

Microsoft Windows 7 Home Premium 64-bits 7601...Intel(R) Core(TM) i3 CPU M 370 @ 2.40GHz4,00 GBATI Mobility Radeon HD 5400 Series
Computer type
Laptop
Computer Manufacturer/Model Number
ACER ASPIRE 5742G
OS
Microsoft Windows 7 Home Premium 64-bits 7601 Multiprocessor Free Service Pack 1
CPU
Intel(R) Core(TM) i3 CPU M 370 @ 2.40GHz
Motherboard
Acer Aspire 5742G
Memory
4,00 GB
Graphics Card(s)
ATI Mobility Radeon HD 5400 Series
Sound Card
(1) AMD High Definition Audio Device (2) Realtek High Defi
Screen Resolution
1366 x 768 x 32 bits (4294967296 colors) @ 60 Hz
Hard Drives
WDC WD5000BEVT-22ZAT0
Let's see if I understand correctly:
You want to find all .txt files in ALL subfolders of F:, but NOT .txt files from the start folder itself (F:\) ?

If that's correct this script will do a dir of .txt files including ALL subfolders recursively.
Then it will jump over any .txt files that are found in the start folder (F:\)

Code:
@ECHO OFF
for /f "delims=*" %%A in ('dir "F:\*.txt" /s /b') do (
  IF NOT "%%~dA%%~pA" == "F:\" (
    echo "%%~fA"
    REM copy F:\master "%%~fA"
  )
)
You should run the script first and fully verify that if finds all the right files + that it excludes all .txt files from the start folder F:\
If all looks OK you can delete the word REM before the copy command
 

My Computer My Computer

At a glance

Windows 7 Pro 32Intel(R) Core(TM) i5 CPU M 540 @ 2.53GHz4,00 GB (Usable 2,98)NVIDIA NVS 5100M
Computer type
Laptop
Computer Manufacturer/Model Number
HP Elitebook 8540p
OS
Windows 7 Pro 32
CPU
Intel(R) Core(TM) i5 CPU M 540 @ 2.53GHz
Motherboard
Hewlett-Packard 1521
Memory
4,00 GB (Usable 2,98)
Graphics Card(s)
NVIDIA NVS 5100M
Sound Card
NVIDIA High Definition Audio
Screen Resolution
1600x900
Hard Drives
INTEL SSDSA2CW120G3
Antivirus
F-Secure Internet Security
Browser
IE, Firefox, Opera
Other Info
Sandboxie,
SRP (Software Restriction Policy),
EMET (Enhanced Mitigation Experience Toolkit),
WFC (Windows Firewall Control by BiniSoft),
Malwarebytes Premium
F:\master is that the real name? Or is it F:\master.txt? Please give the real name.
What is the root folder where all txt files have to be replaced?

With that info I can make a script
 

My Computer My Computer

At a glance

Microsoft Windows 7 Home Premium 64-bits 7601...Intel(R) Core(TM) i3 CPU M 370 @ 2.40GHz4,00 GBATI Mobility Radeon HD 5400 Series
Computer type
Laptop
Computer Manufacturer/Model Number
ACER ASPIRE 5742G
OS
Microsoft Windows 7 Home Premium 64-bits 7601 Multiprocessor Free Service Pack 1
CPU
Intel(R) Core(TM) i3 CPU M 370 @ 2.40GHz
Motherboard
Acer Aspire 5742G
Memory
4,00 GB
Graphics Card(s)
ATI Mobility Radeon HD 5400 Series
Sound Card
(1) AMD High Definition Audio Device (2) Realtek High Defi
Screen Resolution
1366 x 768 x 32 bits (4294967296 colors) @ 60 Hz
Hard Drives
WDC WD5000BEVT-22ZAT0
Code:
@echo off
for  /R  [COLOR=red][B]F:\[/B][/COLOR]  %%i  in  (*.txt)  do  echo  copy "[COLOR=red][B]F:\master[/B][/COLOR]"  "%%i"
pause
Put in bat file. Replace D:\ with actual root of txt files to replace. Replace f:\master with actual template file.
Run it (it shows what it does). all is fine? if so... remove the word echo and run for the real work
 
Last edited:

My Computer My Computer

At a glance

Microsoft Windows 7 Home Premium 64-bits 7601...Intel(R) Core(TM) i3 CPU M 370 @ 2.40GHz4,00 GBATI Mobility Radeon HD 5400 Series
Computer type
Laptop
Computer Manufacturer/Model Number
ACER ASPIRE 5742G
OS
Microsoft Windows 7 Home Premium 64-bits 7601 Multiprocessor Free Service Pack 1
CPU
Intel(R) Core(TM) i3 CPU M 370 @ 2.40GHz
Motherboard
Acer Aspire 5742G
Memory
4,00 GB
Graphics Card(s)
ATI Mobility Radeon HD 5400 Series
Sound Card
(1) AMD High Definition Audio Device (2) Realtek High Defi
Screen Resolution
1366 x 768 x 32 bits (4294967296 colors) @ 60 Hz
Hard Drives
WDC WD5000BEVT-22ZAT0
thanks.

yes 'F:\master' was the actual name i was going with.

I used the .txt file suffix as an example, I need to do various files but the principle is all the same. The big thing is i don't know the file name before the suffix, and i don't know the names of the subfolders or how many subfolders there are.
 

My Computer My Computer

At a glance

Windows 7 x64, ultimate/pro/home, SLES x86 & ...intel & amdyesgtx970
Computer type
PC/Desktop
Computer Manufacturer/Model Number
homebuilt & dell & sgi
OS
Windows 7 x64, ultimate/pro/home, SLES x86 & ia64
CPU
intel & amd
Motherboard
yes
Memory
yes
Graphics Card(s)
gtx970
Sound Card
same as memory
Monitor(s) Displays
benq 27"
Screen Resolution
2560*1440
Hard Drives
many
thanks.

yes 'F:\master' was the actual name i was going with.

I used the .txt file suffix as an example, I need to do various files but the principle is all the same. The big thing is i don't know the file name before the suffix, and i don't know the names of the subfolders or how many subfolders there are.
Try my script as a test. Is that what you want?
 

My Computer My Computer

At a glance

Microsoft Windows 7 Home Premium 64-bits 7601...Intel(R) Core(TM) i3 CPU M 370 @ 2.40GHz4,00 GBATI Mobility Radeon HD 5400 Series
Computer type
Laptop
Computer Manufacturer/Model Number
ACER ASPIRE 5742G
OS
Microsoft Windows 7 Home Premium 64-bits 7601 Multiprocessor Free Service Pack 1
CPU
Intel(R) Core(TM) i3 CPU M 370 @ 2.40GHz
Motherboard
Acer Aspire 5742G
Memory
4,00 GB
Graphics Card(s)
ATI Mobility Radeon HD 5400 Series
Sound Card
(1) AMD High Definition Audio Device (2) Realtek High Defi
Screen Resolution
1366 x 768 x 32 bits (4294967296 colors) @ 60 Hz
Hard Drives
WDC WD5000BEVT-22ZAT0
F:\master ...hmmm so a file without an extension?
 

My Computer My Computer

At a glance

Microsoft Windows 7 Home Premium 64-bits 7601...Intel(R) Core(TM) i3 CPU M 370 @ 2.40GHz4,00 GBATI Mobility Radeon HD 5400 Series
Computer type
Laptop
Computer Manufacturer/Model Number
ACER ASPIRE 5742G
OS
Microsoft Windows 7 Home Premium 64-bits 7601 Multiprocessor Free Service Pack 1
CPU
Intel(R) Core(TM) i3 CPU M 370 @ 2.40GHz
Motherboard
Acer Aspire 5742G
Memory
4,00 GB
Graphics Card(s)
ATI Mobility Radeon HD 5400 Series
Sound Card
(1) AMD High Definition Audio Device (2) Realtek High Defi
Screen Resolution
1366 x 768 x 32 bits (4294967296 colors) @ 60 Hz
Hard Drives
WDC WD5000BEVT-22ZAT0
yes, without the extension. Or make it whatever you like, whatever's easiest to write the script. I'm copying some file that i need which has whatever name and calling it 'master' for sake of simplicity. And it's that file I want to copy to all subfolders and replace every file that has a given suffix (i used .txt). But i don't want to change the copy destination filename, just the contents of the file.


it looks like either one will do it. not sure if i'll get to it today,
i'll respond tomorrow or as soon as i get a chance to try it out.
thanks again.
 

My Computer My Computer

At a glance

Windows 7 x64, ultimate/pro/home, SLES x86 & ...intel & amdyesgtx970
Computer type
PC/Desktop
Computer Manufacturer/Model Number
homebuilt & dell & sgi
OS
Windows 7 x64, ultimate/pro/home, SLES x86 & ia64
CPU
intel & amd
Motherboard
yes
Memory
yes
Graphics Card(s)
gtx970
Sound Card
same as memory
Monitor(s) Displays
benq 27"
Screen Resolution
2560*1440
Hard Drives
many
what is the root folder of all txt files to replace? Is it f:\ ?
 

My Computer My Computer

At a glance

Microsoft Windows 7 Home Premium 64-bits 7601...Intel(R) Core(TM) i3 CPU M 370 @ 2.40GHz4,00 GBATI Mobility Radeon HD 5400 Series
Computer type
Laptop
Computer Manufacturer/Model Number
ACER ASPIRE 5742G
OS
Microsoft Windows 7 Home Premium 64-bits 7601 Multiprocessor Free Service Pack 1
CPU
Intel(R) Core(TM) i3 CPU M 370 @ 2.40GHz
Motherboard
Acer Aspire 5742G
Memory
4,00 GB
Graphics Card(s)
ATI Mobility Radeon HD 5400 Series
Sound Card
(1) AMD High Definition Audio Device (2) Realtek High Defi
Screen Resolution
1366 x 768 x 32 bits (4294967296 colors) @ 60 Hz
Hard Drives
WDC WD5000BEVT-22ZAT0
yes, everything is on F:\ drive. i copied my master file to F:\master.txt. or to F:\master without an extension. whatever you prefer.
the subfolder part i was kind of vague on, but let's keep it simple.
I will manually go into each folder that contains all the files i want to replace, so it would only be a matter of doing a 'dir' in that folder to obtain every filename matching the suffix i am looking for,
then doing a 'copy F:\master <destination name>'

if you can put a variable in the script, such that suffix = '.txt' and use that so it's obvious all i have to do is change that one thing at the top of the script then cool.

if you can further somehow recursively search subfolders from the location the script is run and change all matching files within that directory tree, then bonus it saves me work searching.... which was why i asked this because i don't know windows scripting :(
 

My Computer My Computer

At a glance

Windows 7 x64, ultimate/pro/home, SLES x86 & ...intel & amdyesgtx970
Computer type
PC/Desktop
Computer Manufacturer/Model Number
homebuilt & dell & sgi
OS
Windows 7 x64, ultimate/pro/home, SLES x86 & ia64
CPU
intel & amd
Motherboard
yes
Memory
yes
Graphics Card(s)
gtx970
Sound Card
same as memory
Monitor(s) Displays
benq 27"
Screen Resolution
2560*1440
Hard Drives
many
I don't prefer F:\master.txt of course! Otherwise that file is also one of the files to be renamed!
 

My Computer My Computer

At a glance

Microsoft Windows 7 Home Premium 64-bits 7601...Intel(R) Core(TM) i3 CPU M 370 @ 2.40GHz4,00 GBATI Mobility Radeon HD 5400 Series
Computer type
Laptop
Computer Manufacturer/Model Number
ACER ASPIRE 5742G
OS
Microsoft Windows 7 Home Premium 64-bits 7601 Multiprocessor Free Service Pack 1
CPU
Intel(R) Core(TM) i3 CPU M 370 @ 2.40GHz
Motherboard
Acer Aspire 5742G
Memory
4,00 GB
Graphics Card(s)
ATI Mobility Radeon HD 5400 Series
Sound Card
(1) AMD High Definition Audio Device (2) Realtek High Defi
Screen Resolution
1366 x 768 x 32 bits (4294967296 colors) @ 60 Hz
Hard Drives
WDC WD5000BEVT-22ZAT0
#5 has been modified. F:\ is the root where the *.txt files are. The may also be located in its subfolders. F:\master is the template.

Do you want F:\ to be configurable? F:\master is a fixed name and contents, or also configurable?
*.txt must be configurable?
 

My Computer My Computer

At a glance

Microsoft Windows 7 Home Premium 64-bits 7601...Intel(R) Core(TM) i3 CPU M 370 @ 2.40GHz4,00 GBATI Mobility Radeon HD 5400 Series
Computer type
Laptop
Computer Manufacturer/Model Number
ACER ASPIRE 5742G
OS
Microsoft Windows 7 Home Premium 64-bits 7601 Multiprocessor Free Service Pack 1
CPU
Intel(R) Core(TM) i3 CPU M 370 @ 2.40GHz
Motherboard
Acer Aspire 5742G
Memory
4,00 GB
Graphics Card(s)
ATI Mobility Radeon HD 5400 Series
Sound Card
(1) AMD High Definition Audio Device (2) Realtek High Defi
Screen Resolution
1366 x 768 x 32 bits (4294967296 colors) @ 60 Hz
Hard Drives
WDC WD5000BEVT-22ZAT0
Back
Top