Automatic folder creation and copying a file with specific extension

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
rakshan
Posts: 1
Joined: 29 Mar 2023 03:40

Automatic folder creation and copying a file with specific extension

#1 Post by rakshan » 29 Mar 2023 03:49

I have a folder structrure which looks as below:
Folder1/FolderY/YYY.hpp
Folder2/FolderX/xxx.hpp
Build_5.5/build.a, build.m
src

I need to create a folder in another directory( say Result), inside which it contains the folder (Build.5.5) and followed by the .a file

I am not able to replicate the requirement in batch although I sort of have a blueprint for them

Code: Select all

for /d in (*) do
	if %%G has (Build_*)
	   look for .a file
	   copy the whole folder and paste it to he directory
Expectation:
Result
Build_5.5/build.a

PS: beginner in batch scripting
Any suggestions?

DOSadnie
Posts: 143
Joined: 21 Jul 2022 15:12
Location: Coding Kindergarten

Re: Automatic folder creation and copying a file with specific extension

#2 Post by DOSadnie » 24 Apr 2023 16:09

rakshan wrote:
29 Mar 2023 03:49
[...]
PS: beginner in batch scripting
Any suggestions?
Maybe free NirCmd by NirSoft has such ability. Or some other of his free tools

koko
Posts: 38
Joined: 13 Oct 2016 00:40

Re: Automatic folder creation and copying a file with specific extension

#3 Post by koko » 25 Apr 2023 08:31

Based on the OP it sounds like you only want a copy of the singular 'build.a' file in the name of the 'Build_...' directory name, within a sibling top-level 'Result' directory.

In which case the following works. Simply have the parent directory as the input of the script (eg: by dragging the directory that contains 'Build', etc sub-directories onto the script). The 'sourcenamecheck', 'filename' and 'destname' variables can be changed in the script if your actual file/dir names differ from your examples.

If there are additional 'Build_...' directories it will create a separate sub-directory in Results named after each one.

Code: Select all

:: Assumes input directory is parent directory of 'Build_...', etc directories

@echo off
setlocal enableextensions enabledelayedexpansion

set "sourcenamecheck=Build_"
set "filename=build.a"
set "destname=Result"

set "input=%1"
:: Remove double quotes
set "input=!input:"=!"

echo [Script] Input path: !input!

:: Not using recursive (/r) option since only wanting to check top-level directories
for /d %%b in ("!input!\!sourcenamecheck!*") do (
    set "source=%%b"
    set "sourcename=%%~nxb"

    if exist "!source!\!filename!" (
        rem Creates destination directories if they don't exist
        if not exist "!input!\!destname!" md "!input!\!destname!"
        if not exist "!input!\!destname!\!sourcename!" md "!input!\!destname!\!sourcename!"

        echo [Script] Found "!filename!" file. Copying to "!destname!"...
        
        copy "!source!\!filename!" "!input!\!destname!\!sourcename!\!filename!"
    
        if !errorlevel! neq 0 (
            echo [Script] Error encountered. Exit code: !errorlevel!.
        ) else (
            echo [Script] Complete.
        )
    
    ) else (
        echo [Script] "!filename!" file not found.
    )
)

pause >nul|set /p "=[Script] Press any key to exit... " & exit

exit /b
This will produce, eg:

Result\Build_5.5\build.a

Where 'Result' is beside the original 'Build_5.5' directory (or whatever number it is).

Post Reply