- 1 year ago
Copy the first file from every folder to a new location in windows
Comment
1 / 1
To copy the first file from every folder to a new location in Windows, you can use a batch script that utilizes a for
loop to iterate through the folders and the xcopy
command to perform the copying. Here's an example:
batch@echo off
set "sourceFolder=C:\Path\to\source\folder"
set "destinationFolder=C:\Path\to\destination\folder"
for /d %%F in ("%sourceFolder%\*") do (
for %%I in ("%%F\*") do (
xcopy "%%I" "%destinationFolder%" /Y
goto next
)
:next
)
In this example:
- Set the
sourceFolder
variable to the path of the folder containing the subfolders from which you want to copy the first file. - Set the
destinationFolder
variable to the path of the folder where you want to copy the first files. - The
for /d
loop iterates through the subfolders in thesourceFolder
. - The inner
for
loop iterates through the files within each subfolder. - The
xcopy
command is used to copy the first file (%%I
) from each subfolder to thedestinationFolder
. The/Y
flag suppresses prompts to overwrite existing files. - After copying the first file from a subfolder, the
goto
command is used to skip to the next subfolder, so only the first file is copied.
Adjust the sourceFolder
and destinationFolder
paths according to your actual source and destination locations. Save the script with a .bat extension, run it, and the first file from each subfolder in the source folder will be copied to the destination folder.