1
for /L %%f in (1,1,10) do copy File1.txt %%f.txt

this code does the job very well, but I'm trying to understand how to change it to make it read
subfolders, so that I don't have to keep moving the batch file to every folder

I saw this, but not really sure how to put it together

@echo off

SET "sourcedir=C:\Users\user\Desktop\Main\Original"
SET "destdir=C:\Users\user\Desktop\Main\Copied"

for /L %%f in ('dir /s 1,1,10) do copy *.txt %%f.txt

in the section - copy *.txt %%f - I put a * so that it can only look for .txt files, but this action
slows down the coping and then stops working.

I know my code is a mess, just trying to put something together
I have many Subfolders and each folder has 1 txt file in it with all random names
and I need to make multiple copies of each file.txt in each folder
I have so many subfolders that it would literally take me months of time to get all files copied
and by then I would have many more new files to work on
so getting this copier to read Subfolders is like top priority for me.

I would like help putting this together and then explaining how it links
because I'm interested in applying the Set and dir to other batch file I have

Please any details on this will be much appreciated

I was told to look into xcopy and robocopy, but I have no idea were to add a counter

@echo off
for /1 %f in (1,1,10) do xcopy "C:\Sources" "C:\Target" /c /d /i /y
exit

So I have this that reads from source and dumps in main folder where the batch is

Option 1)

for /L %%f in (1,1,10) do xcopy "C:\Source Address\*.txt" %%f.txt

Option 2)

for /L %%f in (1,1,10) do xcopy "C:\Source Address\" "C:\Destination Address\ %%f.txt"

The thing I don't like is that is asks me a question
and I have over 10,000 txt files, I can't sit here and press F for Filename
10,000 times, can we disable that

OK, so I got this working
all I need help with is were to add this /c /d /i /y
I am still trying to get it to read Subfolders with the batch sitting
in the main folder and me not having to move files back and forth

Option 3)

for /L %%f in (1,1,110) do copy "C:\Source Address\*.txt" %%f.txt`

This works well with the Source and the wild card @magoo told me to add
the Source before the .txt file

But with this code I would still have to open hundreds of folders and move
the file to the source run the copier and then move back all copied files

Still need help with Subfolders

Wild Cats
  • 11
  • 4
  • you may want to check out `xcopy /?` and `robocopy /?` - both of them are able to copy files recursively (aka. "process files in subfolders") – Stephan Dec 30 '20 at 07:52
  • @Stephan I have added a new xcopy script to my question, I have no idea how to make a new black window, and I don't want to ask many question in one post, but how do I add a counter, I need to make 10 to 100 copies of each file found inside each folder, and subfolder – Wild Cats Dec 30 '20 at 16:07
  • I always find it easier to take small steps through a larger task, to determine a path to that goal. In this case, I'd list all directories within the current tree, _(recursive)_, which contain only one `*.txt` file. Then I would work directly with each of those directories, retrieving the unknown txt file name in each, before copying it ten times as needed. – Compo Dec 30 '20 at 18:46
  • @compo OK I agree, so this is what I have gotten so far, since I have no idea how to add another grey screen here I will just update my original post, can you tell me how I can add another screen to show my progress. I got something working, and I think it's because of the xcopy command, but it asks me if the file is a file name or directory, could you help me get around that – Wild Cats Dec 30 '20 at 19:26
  • I listed the three steps, yet you're still working around the loop for the last part, start at the beginning, not at the end. – Compo Dec 30 '20 at 20:47
  • @Compo I read over your third comment and the problem is I have over 10,000 folders easy, so I couldn't be able to work with each of those directories to retrieve the names to all the unknown txt file, Also you said from the beginning from here for /L %%f in ? – Wild Cats Dec 30 '20 at 21:52
  • @Compo you said, " I listed" did you mean (you listed) as in me – Wild Cats Dec 30 '20 at 21:54
  • No, I said what I meant! **Step 1** List all directories containing just one `*.txt` match. **Step 2** Retrieve the unknown text file name in each of those directories **Step 3** make 10 copies of the now known text file. _(As you can clearly see, you appear to be trying to work from **Step 3**)_. – Compo Dec 30 '20 at 22:09
  • I was brought up on the phrase "learn to walk before you run". You really should learn to use the commands from your site searches before attempting something which is currently clearly beyond your current capabilities, hence the reason I'm advising you to do it in a seies of smaller steps. – Compo Dec 30 '20 at 22:21
  • @Compo Wow OK, well to understand what you are telling me, Step 1) List all directories - I have one D: drive with 10000 folders, with Subfolders, which all 10000 folder and subfolders contain 1 text file in each, Step 2) retrieve all 10000 txt plus file names from those directories, step 3) run the copier using the 10000 plus names I got from the directory, or did I miss understand your explanation – Wild Cats Dec 30 '20 at 22:29

1 Answers1

0
for /L %%f in (1,1,10) do copy File1.txt %%f.txt

will vary %%f from 1 (the first number in the parenthesised list) to 10 (the last) in steps of 1 (the middle) and will therefore copy file1.txt to 10 separate files, 1.txt to 10.txt.

Since there are no paths specified, the copy will be performed from file1.txt in the current directory to 1.txt .. 10.txt in the current directory.

If you were to put your batch file in any directory that is mentioned in the path variable (just execute path at the prompt to show it) then no matter where the current directory is, you could just use that batch filename to execute the batch, and the files would be created by copying file1.txt in the now-current directory to 1.txt .. 10.txt in the now-current directory - if file1.txt exists in the now-current directory, and if not, it will generate error messages reporting that the source file is missing.

If you were to replace file1.txt in the batch with "x:\wherever\file1.txt" then the file would be copied specifically from the file "x:\wherever\file1.txt" regardless of whether file1.txt exists in the now-current directory. (And please get used to "quoting file or pathnames" as it will avoid a whole slough of problems when you tackle names containing spaces and some other special characters).

I have no idea how for /L %%f in ('dir /s 1,1,10) do is supposed to work since according to Hoyle, the first element in the parenthesised list should be a number. I'd suggest that we have a small transcription problem here.

Th slower and slower problem - yes, understandable. You are creating more and more .txt files, and copying all of them to a new destination...

Perhaps referring to This response and question might show how to solve your subdirectory-scan issue.

Magoo
  • 72,034
  • 7
  • 58
  • 78
  • I want to do is for the copier to read every file, folder from Folder A, place it in Folder B, Folder A will have for example 3 folders names Folder1, Folder2, Folder3, with 3 txt files one in each, and after running the copier, in Folder B, I would have the same Folder1, Folder2,Folder3, but each folder would have xnumbers of file.txt copies, in this case each folder would have 10 txt files – Wild Cats Dec 30 '20 at 01:36
  • when I use this do copy filename.txt it works great, but when I put *.txt it slows down, and stops – Wild Cats Dec 30 '20 at 02:49
  • The obvious issue with using a wildcard @WildCats, is that you copy `unknownname.txt` to `1.txt`, then you'll be trying to copy `unknownname.txt` and `1.txt` to `2.txt`, then `unknownname.txt`, `1.txt` and `2.txt` to `3.txt` etc. up to copying, `unknownname.txt`, `1.txt`, `2.txt`, `3.txt`, `4.txt`, `5.txt`, `6.txt`, `7.txt`, `8.txt` and `9.txt` to `10.txt`. It doesn't look like an efficient process does it? – Compo Dec 30 '20 at 04:03
  • @Comp No, and I understood why the Wildcard failed, because it's getting confused with all the txt files, thus the reason why I would like to change Source and Destinations, if it can copy from one folder and place it in another then it wouldn't fail, and I don't know if the copier could handle coping multiple files into different folder, as I commented, if someone could answer that question maybe this will save me some time, can the copier read from Folder A with many SUB folders and duplicate it to Folder B, with many Subfolders, but each Subfolders would have 10 copies, is it possible. – Wild Cats Dec 30 '20 at 15:19
  • I tried to do what Magoo said, to place "x:\wherever\file1.txt", that I take it would change the source, but I couldn't get it to work, I will try again, but if I do get it going, I would still need to open every folder and add the copies into the location of where the files goes, Now my goal is Copy Folder A that has many Subfolders with 1 txt file in each and run a copier to Folder B with all the Subfolder but with 10 txt files in each subfolder, I don't know if a copier can even do that, if it can handle that – Wild Cats Dec 30 '20 at 15:30
  • @Magoo I was able to make the copier read from a Source folder and using the wild card worked great since the copied files appeared in a different folder So thank you for that. I'm still trying to make it read from source folder that contains many folders and subfolders, and image every folder and subfolder to a target folder, but with the extra copies, I know by using /s or something like that can read sub folders, but I don't know where in the code to intergrade it Option 2 or Option 3 would be fine, if I could get help with it reading all folders and subs folders, I tried /L /S and it filed – Wild Cats Dec 30 '20 at 23:07