Destination Folder Batch file - batch-file

I currently use a .bat file
I'm able to copy a test.cfg file in a folder named testVersion434 using copy test.cfg testVersion434.
Unfortunately I am unable to copy the file test.cfg to a folder that begins with testXXXXXXXX
I'll have in the future Folders testVersion43x that will create , and I would copy test.cfg in all folders who begin with test
I can list all of them with dir test*

You will need to enumerate the target folders and execute a copy operation for each one
for /d %%a in ("c:\folders\test*") do copy "c:\file\test.cfg" "%%~fa"
The for /d command will iterate over the indicated set of folders and, for each one, the code after the do clause is executed, with %%a holding a reference to the folder being iterated. %%~fa is the full path of the folder.

Related

CMD command to copy only files and not folders in a folder tree

Let's assume the following folder structure:
c:\tab\file1.txt
c:\tab\insidetab1\file2.txt
c:\tab\insidetab2\file3.txt
c:\tab\insidetab3\insidetab4\file4.txt
I want to copy only files file1.txt, file2.txt, file3.txt, file4.txt to some destination. Basically I want to remove all folders and keep only the files. Is it even possible?
This will copy all files that match the pattern file*.txt in C:\Tab to C:\Newtab.
for /R C:\tab %%G in (file*.txt) do copy %%G C:\newtab\

How to extract all multi-volume RAR archives from subfolders of a folder?

I search for a way to unpack multi-volume archives after download via batch.
I download folders with .r?? files in it over a FTP monitoring program and want that WinRAR goes in the first subfolder in the source folder and start unpacking .r00, delete the archive and move the folder with the unpacked files to a new location.
Then the batch script should start this process again with the next subfolder.
Let's say the source folder C:\Users\unpack contains following subfolders with files:
source folder
subfolder1
Archive1.r00
Archive1.r01
Archive1.r02
xxx.txt
subfolder2
Archive2.r00
Archive2.r01
yyy.txt
subfolder3
Archive3.r00
Archive3.r01
Archive3.r02
Archive3.r04
Archive3.r05
zzz.txt
I had start to do this with the script in the link below, but that script can't do what I want, so I have started a new question.
How to unpack all rar archives in all subfolders of a folder and then delete the archives?
The script in the link above unrars all files in all subfolders and then moves the folder with its files to a new location. I want that the script unrars and moves subfolder for subfolder in the source folder.
Edit.1
If winrar is ready with the first subfolder the structure in the source folder should look like this:
source folder
subfolder2
Archive2.r00
Archive2.r01
yyy.txt
subfolder3
Archive3.r00
Archive3.r01
Archive3.r02
Archive3.r04
Archive3.r05
zzz.txt
The files and folders in C:\Users\new-location should look like this:
source folder
subfolder1
xxx.mp4
xxx.txt
subfolder2
yyy.mp4
yyy.txt
subfolder3
zzz.mp4
zzz.txt
A possible batch code for this task is:
#echo off
setlocal EnableDelayedExpansion
set "BaseSourceFolder=C:\Users\Unpack"
set "BaseTargetFolder=C:\Users\New-Location"
for /D %%D in ("%BaseSourceFolder%\*") do (
set "TargetFolder=%BaseTargetFolder%\%%~nxD"
if not exist "!TargetFolder!" md "!TargetFolder!"
"%ProgramFiles%\WinRAR\Rar.exe" x -cfg- -idq -y "%%~fD\*.r??" "!TargetFolder!"
if not errorlevel 1 (
del /F /Q "%%~fD\*.r??"
move /Y "%%~fD\*" "!TargetFolder!">nul 2>nul
rd "%%~fD" 2>nul
)
)
rem rd "%BaseSourceFolder%" 2>nul
endlocal
for /? executed in a command prompt window displays help for command for with parameter /D which means for each directory matched by * in base source folder.
In the loop first the target folder name is defined based on name of the subfolder to process. %%~fD and %%~nxD are also explained by for /? whereby folders usually do not have an extension and therefore %%~nD is often also enough.
Next this target folder is created if not already existing.
Then Rar.exe is executed to extract the multi-volume archive in the current subfolder directly to the defined target folder.
*.r?? is used to make this batch file work for multi-volume archives with old naming scheme ArchiveName.r00, ArchiveName.r01, ... as well as better naming scheme ArchiveName.part01.rar, ArchiveName.part02.rar, ... which is used by default by WinRAR version 5.21. RAR automatically skips the archive files processed already during extraction of a multi-volume archive from the list matching *.r??.
Exit code of Rar.exe is evaluated to determine if any error occurred. If exit code assigned to errorlevel is lower than 1, there was no error and the 3 commands of the if branch are executed resulting in deleting first all RAR archive files.
The remaining files in current subfolder are also moved to the current target folder which is the *.txt file in the folder structure example.
As the current subfolder should be empty now, the command rd should be able to remove the directory. In case of an error because subfolder is still not empty, the subfolder remains in base source folder.
The base source folder is empty if everything worked without an error. The commented line after for loop could be used to remove the empty base source folder as well, but keep the folder if anything failed.

How to create a windows batch file that combines all *NAD.TXT recursively within each directory?

I have a group of directories, and within those directories a have some files that end in *NAD.TXT. I need to combine, or copy together, all *NAD.TXT files within each directory to a file called COMBINED_NAD.TXT in each of the directories. Basically, so it combines those files and creates it in the same directory, and then recurses to the next one. Thanks for the help.
for /d /r %%a in (*) do (
del "%%~fa\COMBINED_NAD.TXT" >nul 2>nul
copy /b "%%~fa\*NAD.TXT" "%%~fa\COMBINED_NAD.TMP"
ren "%%~fa\COMBINED_NAD.TMP" "COMBINED_NAD.TXT"
)
For each directory, recursively :
If the target file exists, remove it
Combine all the source files into target. .tmp extension ensures the target file will not be processed as a source file
Rename the target file to final name

copy all files recursively into a single folder (without recreating folders)

With a batch (.bat), I want to copy all mp3 files that are in 1 subdirectory of D:\TEMP
D:\TEMP\\(anyfolder)\\(anyfile.mp3)
to
E:\MYFOLDER\
I tried with xcopy but
I don't know how to tell "just recurse subfolders of D:\TEMP and not subsubfolders, subsubsubfolders, etc."
When using xcopy, folders are created in the destination (in order to replicate source's folder tree), I don't want this : files should be copied in just 1 single folder.
for command is your friend. Read help for and then try this in the command prompt
for /d %a in (*) do #echo %a
as you see, it follows all subfolders in the current directory.
thus,
for /d %a in (*) do #copy %a\*.mp3 e:\myfolder
will copy all your mp3 to the destination folder.

copy .h files (only) form subfolders under main folder to new folder using cmd

I want to copy all .txt files which might be in sub folders or under main folder(source) to destination main folder
i.e E:\source\sub1\sub11 or E:\source\sub2 or E:\source\ to d:\test folder.
Here i want to copy only .txt files not there subfolder to destination folder.
I have tired many options suggested in this forum .I am unable to ignore sub folders.
Suppose i have .txt file in E:\source\sub1\sub11 when tired to copy.txt file to destination folder then hierarchy in destination folder will be d:\test folder\sub1\sub11
here i want to copy to main folder d:\test folder not d:\test folder\sub1\sub11
Please can any one help me here
Thanks in advance
You can use the FOR command to copy recursively each file individually:
for /r e:\source %a in (*.txt) do #copy "%a" "d:\test folder\"
(You need to use %%a instead of %a if the command is inside a batch file).

Resources