Batch file to copy one file.qwz to every subfolder having text files and rename them accordingly

Josef Miller

Junior Member
Mar 7, 2018
7
0
1
I have one directory with hundreds of sub and sub sub directories down the tree with a total number of text files over 22000 files. I need to copy one file with .qwz extension to every sub directory that contains text files. Copies of the .qwz file will be in the same number of existing text files in any sub directory and will be renamed in the same names of those text files.

Example: if a sub directory contains 200 text files, I need the generic c:\file.qwz to be copied in that sub directory 200 times and each file of those 200 files.qwz to be renamed the same names of the text files in the same sub directory.

This loop should be applied on any and every sub folder down the tree that contains text files only.

Can I get some precious assistance with this challenge please?

Many thanks in advance ..
 

TheELF

Diamond Member
Dec 22, 2012
4,027
753
126
cd /d %0\..
dir /s /b *.txt >file.txt
for /f "delims=" %%a in (file.txt) do echo %%~da%%~pa%%~na.qwz >>file1.txt
for /f "delims=" %%a in (file1.txt) do copy c:\file.qwz %%a


1. change the directory to where this bat is so put this in the root of the disk
2. searches for all .txt files and makes a list of them in a text file complete with path name
3.changes the .txt extension to .qwz inside the textfile
4.takes the list in the new textfile as a name input for the copy command

delete file1.txt after each time or add del file1.txt before the first for command otherwise you gonna end up with duplicates, >> adds to what's in the file already.
 
  • Like
Reactions: Ken g6

Josef Miller

Junior Member
Mar 7, 2018
7
0
1
Such genius way of thinking .. thank you very much for your invaluable input .. the script ran smoothly and yielded two files: file.txt which has an entire list of all .txt files, and file1.txt which has the same list but all the .txt files renamed with .qwz extension.
In fact, what is needed is the actual physical copying of c:/file.qwz side by side to its equivalent .txt file and to be renamed in the same name of that .txt file in all sub-directories.
Can this script be modified to accommodate this need please?

I'm more than grateful for you precious assistance, thank you very much indeed.

J.
 

TheELF

Diamond Member
Dec 22, 2012
4,027
753
126
In fact, what is needed is the actual physical copying of c:/file.qwz side by side to its equivalent .txt file and to be renamed in the same name of that .txt file in all sub-directories.
Can this script be modified to accommodate this need please?
Hmm,the last line
for /f "delims=" %%a in (file1.txt) do copy c:\file.qwz %%a
was supposed to do just that,it takes the names of file1.txt and uses them to copy the c:\file.qwz with the name it get's from %%a which is each line of the text file.

Do you run the script with admin rights? Could there be any other kind of protection that would prevent something be written to the folders?
 

Josef Miller

Junior Member
Mar 7, 2018
7
0
1
Yes, for sure, I ran the script as an administrator. In fact, there's no any kind of protection to prevent the script from running properly. Before I typed these words I ran it again to make sure that I wouldn't be embarrassed by a silly mistake from my side. I got the same result as I mentioned in my previous message; no .qwz file was copied except in the root folder, one copy was existing after the script stopped. Your opinion is highly appreciated ... thank you very much.
 

TheELF

Diamond Member
Dec 22, 2012
4,027
753
126
Didn't think about spaces,if the filenames have spaces you need " " around them,make a new batch with only this line since you already have the file1.txt file and try again.
for /f "delims=" %%a in (file1.txt) do copy c:\file.qwz "%%a"
 
  • Like
Reactions: Ken g6

Josef Miller

Junior Member
Mar 7, 2018
7
0
1
Yes, yes, yes ... you did it. I really don't know how to thank you. Please accept my deep gratitude and full respect to your genuine person.

All the best ..