A little help with batch file

snjpverma

Well-known Member
Joined
Oct 2, 2008
Messages
1,585
Office Version
  1. 365
Platform
  1. Windows
I want to write a batch script to create folders.
I have a notepad with multiple lines in the following location C:\Users\sanjeep.verma\Desktop\Production Files\folder_list.txt
The batch file should create folders with names that is written on each line in the above text file.
In each of the newly created folder the following xlsm file must get pasted: C:\Users\sanjeep.verma\Desktop\DIW.xlsm
Please help with the batch file script as the below batch script is not working as intended.

Code:
@echo off
set folder_list=C:\Users\sanjeep.verma\Desktop\Production Files\folder_list.txt
set source_file=C:\Users\sanjeep.verma\Desktop\Debt Income Worksheet.xlsm
set target_folder=C:\Users\sanjeep.verma\Desktop\Production Files\
for /f "delims=" %%i in (%folder_list%) do (
  md "%target_folder%\%%i"
  copy "%source_file%" "%target_folder%\%%i"
)

Note: The below code works file to create the folders from each line of the text file. however, it does not paste the xlsm file into each of the newly created folders.
Code:
@echo off
for /f "delims=" %%a in (folder_list.txt) do (
    mkdir "%%a"
)
 

Excel Facts

Highlight Duplicates
Home, Conditional Formatting, Highlight Cells, Duplicate records, OK to add pink formatting to any duplicates in selected range.
I'm surprised it works at all. Quotes are needed in your SET commands for any file/folder with spaces in the name.
Code:
@echo off
set folder_list="C:\Users\sanjeep.verma\Desktop\Production Files\folder_list.txt"
set source_file="C:\Users\sanjeep.verma\Desktop\Debt Income Worksheet.xlsm"
set target_folder="C:\Users\sanjeep.verma\Desktop\Production Files\"
for /f "delims=" %%i in (%folder_list%) do (
  md "%target_folder%\%%i"
  copy "%source_file%" "%target_folder%\%%i"
)
 
Upvote 0
@jdellasala , thanks for your response.

I think the problem was with some access issues due to which the batch file wasn't working. So, I copied the source file in the same directory as the target folder and it worked. I guess, since everything is in the same folder, the batch file works without the quotes.

However, I need one help with the below code (latest version that works). The source file that gets copied into each folder should be renamed with the same name as the directories they are pasted in.

Example: if the directory created is named "John - 567", then the Debt Income Worksheet.xlsm should get pasted inside it and then renamed as "John - 567" i.e. same as the directory name.

Rich (BB code):
@echo off
set folder_list=folder_list.txt
set source_file=Debt Income Worksheet.xlsm
set target_folder=C:\Users\sanjeep.verma\Desktop\Production Files\
for /f "delims=" %%i in (%folder_list%) do (
  md "%target_folder%\%%i"
  copy "%source_file%" "%target_folder%\%%i"
)

I tried adding this line of code at the end but it doesn't work.

VBA Code:
ren "%target_folder%%%i%source_file%" "%%i.xlsm"
 
Last edited:
Upvote 0
try this..
VBA Code:
  copy "%source_file%" "%target_folder%\%%i\%%i.xlsm"
 
Upvote 0
Change it.
FROM
VBA Code:
  copy "%source_file%" "%target_folder%\%%i"
TO
VBA Code:
  copy "%source_file%" "%target_folder%\%%i\%%i.xlsm"
 
Upvote 0
Rich (BB code):
@echo off
set folder_list=folder_list.txt
set source_file=Debt Income Worksheet.xlsm
set target_folder=C:\Users\sanjeep.verma\Desktop\Production Files\
for /f "delims=" %%i in (%folder_list%) do (
  md "%target_folder%\%%i"
  copy "%source_file%" "%target_folder%\%%i\%%i.xlsm"
)

@veyselemre the above code is the one that I am using now. It is working perfectly but the problem is that if the folders already exists. It overwrites the files inside it. I do not want any overwriting of the files.

If the folders/directories of the same name already exist, then the code should just not proceed.

 
Upvote 0
Hi, try this.
Code:
@echo off
set folder_list=folder_list.txt
set source_file=Debt Income Worksheet.xlsm
set target_folder=C:\Users\sanjeep.verma\Desktop\Production Files\
for /f "delims=" %%i in (%folder_list%) do (
  if not exist "%target_folder%\%%i\" md "%target_folder%\%%i"
  if not exist "%target_folder%\%%i\%%i.xlsm" copy "%source_file%" "%target_folder%\%%i\%%i.xlsm"
)
 
Upvote 0

Forum statistics

Threads
1,223,908
Messages
6,175,306
Members
452,633
Latest member
DougMo

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top