extract a files from many folder and save it in outside

i need to take the particular zip files in four folders and save it outside of the folders.
anyone help me for the code in batch files.


thanks in advance
sasi vAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Bill PrewIT / Software Engineering ConsultantCommented:
You will need to be a lot more specific about what you are trying to do.

  1. How are the four folders found to process?
  2. Are there any subfolders in those four folders that need to be considered?
  3. Are all ZIP files in each of the four folders to be processed?
  4. What is the new location you want the files copied to?
  5. Do you want the files copied, or moved?
  6. Do you just want to copy the ZIP file itself, or do you want to extract the files contained in the ZIP file to the new location?
  7. Please provide some examples of the file and folder names and layouts and explanation of what files should be processed, how they should be processed, and what the results should be afterwards.

As you can see there are many details that you need to provide.  When asking for help please try and think through every detail that an expert would need to know to answer your question before you post it.  Then include all those details in the original question.  You will find this saves a lot of time and gets to much more accurate suggestions.


»bp
0
Gustav BrockCIOCommented:
You don't need batch files for this.
It can easily be done with pure VBA:

Zip and unzip files and folders with VBA the Windows Explorer way
0
sasi vAuthor Commented:
hi prew,

ok i will give you all the details as you needs..

How are the four folders found to process?
we have a program that spliited into 4 files . we are creating each folder for the 4 files. then we doing process and save it in zip files.
Are there any subfolders in those four folders that need to be considered? no subfolder. inside a folder have only zip file.
Are all ZIP files in each of the four folders to be processed? yes
What is the new location you want the files copied to? backpath in code;
SET BackupPath=\\ecc9000203\proj\DI_PMTI_SE\PAT\PAT\PATFiles

Do you want the files copied, or moved? copy alone

Do you just want to copy the ZIP file itself, or do you want to extract the files contained in the ZIP file to the new location?zip files only.
folder-created.PNG
zip-file-inside-a-folder..PNG
0
The 7 Worst Nightmares of a Sysadmin

Fear not! To defend your business’ IT systems we’re going to shine a light on the seven most sinister terrors that haunt sysadmins. That way you can be sure there’s nothing in your stack waiting to go bump in the night.

Bill PrewIT / Software Engineering ConsultantCommented:
So then, this is the BEFORE:

Base-Folder
  + Subfolder1
    + Zipfile1.zip
    + Zipfile2.zip
    + Zipfile3.zip
  + Subfolder2
    + Zipfile4.zip
    + Zipfile5.zip
    + Zipfile6.zip
  + Subfolder3
    + Zipfile7.zip
    + Zipfile8.zip
  + Subfolder4
    + Zipfile9.zip
    + Zipfile10.zip
    + Zipfile11.zip
    + Zipfile12.zip

Open in new window

And this would be the AFTER:

\\ecc9000203\proj\DI_PMTI_SE\PAT\PAT\PATFiles
  + Zipfile1.zip
  + Zipfile2.zip
  + Zipfile3.zip
  + Zipfile4.zip
  + Zipfile5.zip
  + Zipfile6.zip
  + Zipfile7.zip
  + Zipfile8.zip
  + Zipfile9.zip
  + Zipfile10.zip
  + Zipfile11.zip
  + Zipfile12.zip

Open in new window

Is that correct?

If that is correct, then what are the specific paths of the 4 subfolders where the ZIP files exist?


»bp
0
sasi vAuthor Commented:
yes correct.
 zip files exist in my local drive like below
C:\FNASmartServices\REPORTS\BPNO\CD41_2018-------- basefolder
then,
C:\FNASmartServices\REPORTS\BPNO\CD41_2018\New folder
C:\FNASmartServices\REPORTS\BPNO\CD41_2018\New folder (2)
C:\FNASmartServices\REPORTS\BPNO\CD41_2018\New folder (3)

zip files available in all the above folder
0
Bill PrewIT / Software Engineering ConsultantCommented:
Okay, seems like it should be as simple as this then:

@echo off
setlocal

set BaseDir=C:\FNASmartServices\REPORTS\BPNO\CD41_2018
set DestDir=\\ecc9000203\proj\DI_PMTI_SE\PAT\PAT\PATFiles

for /d %%A in ("%BaseDir%\*.*") do (
    copy "%%~A\*.zip" "%DestDir%"
)

Open in new window


»bp
0
sasi vAuthor Commented:
thank you its worked but same for txt files i tried .but i have many txt files in a each sub folder. so how i can take the specific text files from each sub folder.
before
Base-Folder
  + Subfolder1
    + Zipfile1.txt
    + Zipfile2.txt
    + Zipfile3.txt
  + Subfolder2
    + Zipfile4.txt
    + Zipfile5.txt
    + Zipfile6.txt
  + Subfolder3
    + Zipfile7.txt
    + Zipfile8.txt
 
after:

\\ecc9000203\proj\DI_PMTI_SE\PAT\PAT\PATFiles
  + Zipfile1.txt
  + Zipfile4.txt
  + Zipfile7.txt

for this above code, its possible to copy only specific text file to destination folder. with the name of the text files only possible to copy to destination or any other chance to copy ??? i attached snap for you
0
Bill PrewIT / Software Engineering ConsultantCommented:
To copy TXT files simply change this line:

    copy "%%~A\*.zip" "%DestDir%"

to:

    copy "%%~A\*.txt" "%DestDir%"


»bp
0
sasi vAuthor Commented:
yes i tried. i said was i have a many .txt files. i have to take specific txt file. so for that i need to use only the name of the txt file or any code ??
0
Bill PrewIT / Software Engineering ConsultantCommented:
Is there just one TXT file name to be copied, or are there many?


»bp
0
sasi vAuthor Commented:
one txt file only need to copy.. but many txt files are ter
0
sasi vAuthor Commented:
same like previous code .
need to take specific .txt file from each subfolder.
0
Bill PrewIT / Software Engineering ConsultantCommented:
So what is the name of the TXT file?  And does it exist in multiple folders, or just one?


»bp
0
sasi vAuthor Commented:
name is CD391n_txt file
 no its not exist in all sub folder.
in each sub folder, i have a different text file name. so i thought to give only the filename then only it will copy to my destination..
U r idea??
0
Bill PrewIT / Software Engineering ConsultantCommented:
Okay, this will look for the specific file name you specify, in all subfolders under the base folder, and when found copy it to the destination folder.

@echo off
setlocal

set BaseDir=C:\FNASmartServices\REPORTS\BPNO\CD41_2018
set DestDir=\\ecc9000203\proj\DI_PMTI_SE\PAT\PAT\PATFiles
set FileToCopy=CD391n_txt

for /f "tokens=*" %%A in ('dir /b /s /a-d "%BaseDir%\%FileToCopy%"') do (
    copy "%%~A" "%DestDir%"
)

Open in new window


»bp
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
sasi vAuthor Commented:
thank you.
filetocopy
if suppose four files means i have to set filetocopy four times in code rite??
0
Bill PrewIT / Software Engineering ConsultantCommented:
If you have 4 files you can do like this:

@echo off
setlocal

set BaseDir=C:\FNASmartServices\REPORTS\BPNO\CD41_2018
set DestDir=\\ecc9000203\proj\DI_PMTI_SE\PAT\PAT\PATFiles

for /f "tokens=*" %%A in ('dir /b /s /a-d "%BaseDir%\CD391n_txt" "%BaseDir%\CD392n_txt" "%BaseDir%\CD393n_txt" "%BaseDir%\CD394n_txt"') do (
    copy "%%~A" "%DestDir%"
)

Open in new window


»bp
0
sasi vAuthor Commented:
thank you...

this is my full code:

ECHO OFF
Set mm=%DATE:~4,2%
Set dd=%DATE:~7,2%
Set yy=%DATE:~12,2%

ECHO ON
set dateFile=%mm%%dd%%yy%
echo off
SET programName=
SET /P programName=Enter vehicle program name :

SET ProgramPath=%CD%

SET BackupPath=\\ecc9000203\proj\DI_PMTI_SE\PAT\PAT\PATFiles

echo off

TITLE=%programName% BPNO Data serie 

IF "%programName%"=="CD41_2018" goto SPLITTER
IF "%programName%"=="CD539C" goto NORMAL
IF "%programName%"=="P375ICA2" goto NORMAL
IF "%programName%"=="P375N" goto NORMAL
IF "%programName%"=="U375ICA" goto NORMAL
IF "%programName%"=="U375ICA_C" goto NORMAL
IF "%programName%"=="CD42" goto CD42

:NORMAL
dir /a =%BackupPath%\%programName%\ /b > filename1.txt
setlocal EnableDelayedExpansion & time /T
date /T

rem Initialize variables
set OldestDate=999999
set OldestFile=

rem Read each line from list file
for /f "usebackq tokens=*" %%A in ("filename1.txt") do (

    rem Get right 6 characters of file name and arrange in YYMMDD format
    set FileName=%%~nA
    set FileDate=!FileName:~-2!!FileName:~-6,4!

     rem See if this is the oldest file so far, if so save it
    if "!FileDate!" LSS "!OldestDate!" (
        set OldestDate=!FileDate!
        set OldestFile=%%~A
    ))
rem Delete the oldest file
echo Deleting oldest file "%OldestFile%"
del "%BackupPath%\%programName%\%OldestFile%" 
del "filename1.txt"
Copy "%ProgramPath%\%programName%\%dateFile%\*.zip" "%BackupPath%\%programName%\"
echo Zip File Moved

rem PAT BTT files 

Del "%BackupPath%\PAT BTT Text Files\%programName%\Old\" /Q
echo Old File Removed
Move "%BackupPath%\PAT BTT Text Files\%programName%\New\*.txt" "%BackupPath%\PAT BTT Text Files\%programName%\Old\" 
copy "%ProgramPath%\%programName%\%dateFile%\*_BTT.txt" "%BackupPath%\PAT BTT Text Files\%programName%\New\" 

Time /T
pause
exit

:SPLITTER
call :DeleteFolder

call :DeleteFolder "BackupPath"
:DeleteFolder [folder-path]
  
  dir /a =%BackupPath%\%programName%\ /b > filename.txt
  setlocal EnableDelayedExpansion & time /T
  Date /T

   REM Initialize variables
    set OldestDate=999999
    set OldestFile=

   REM Read each line from list file
    for /f "usebackq tokens=*" %%A in ("filename.txt") do (

       REM Get right 6 characters of file name and arrange in YYMMDD format
        set FileName=%%~nA
        set FileDate=!FileName:~-2!!FileName:~-6,4!

      REM  See if this is the oldest file so far, if so save it
        if "!FileDate!" LSS "!OldestDate!" (
            set OldestDate=!FileDate!
            set OldestFile=%%~A
        )  )
   REM Delete the oldest file
    echo Deleting oldest file "%~1\%OldestFile%"
    del "filename.txt"
Mkdir "%BackupPath%\%programName%\%date:~4,2%%date:~7,2%%date:~12,2%"
for /d %%A in ("%ProgramPath%\%programName%\%dateFile%\*.*") do (
    copy "%%~A\*.zip" "%BackupPath%\%programName%\%dateFile%"
	)
   echo Zip Files Moved
  del "%BackupPath%\PAT BTT Text Files\%programName%\Old\" /Q
Echo Old File removed
Move "%BackupPath%\PAT BTT Text Files\%programName%\New\*.txt" "%BackupPath%\PAT BTT Text Files\%programName%\Old\"
set FileToCopy=*_CD391N.txt
for /f "tokens=*" %%A in ('dir /b /s /a-d "%ProgramPath%\%programName%\%dateFile%\%FileToCopy%"') do (
    copy "%%~A" "%BackupPath%\PAT BTT Text Files\%programName%\New\"
	)
Time /T
pause
exit /b  

:CD42
call :DeleteFolder

call :DeleteFolder "BackupPath"
:DeleteFolder [folder-path]
  
  dir /a =%BackupPath%\%programName%\ /b > filename.txt
  setlocal EnableDelayedExpansion & time /T
  Date /T

   REM Initialize variables
    set OldestDate=999999
    set OldestFile=

   REM Read each line from list file
    for /f "usebackq tokens=*" %%A in ("filename.txt") do (

       REM Get right 6 characters of file name and arrange in YYMMDD format
        set FileName=%%~nA
        set FileDate=!FileName:~-2!!FileName:~-6,4!

      REM  See if this is the oldest file so far, if so save it
        if "!FileDate!" LSS "!OldestDate!" (
            set OldestDate=!FileDate!
            set OldestFile=%%~A
        )  )
   REM Delete the oldest file
    echo Deleting oldest file "%~1\%OldestFile%"
    del "filename.txt"
Mkdir "%BackupPath%\%programName%\%date:~4,2%%date:~7,2%%date:~12,2%"
for /d %%A in ("%ProgramPath%\%programName%\%dateFile%\*.*") do (
    copy "%%~A\*.zip" "%BackupPath%\%programName%\%dateFile%"
	)
   echo Zip Files Moved
  del "%BackupPath%\PAT BTT Text Files\%programName%\Old\" /Q
Echo Old File removed
Move "%BackupPath%\PAT BTT Text Files\%programName%\New\*.txt" "%BackupPath%\PAT BTT Text Files\%programName%\Old\"
set FileToCopy=*_CD539C.txt
	for /f "tokens=*" %%A in ('dir /b /s /a-d "%ProgramPath%\%programName%\%dateFile%\%FileToCopy%"') do (
    copy "%%~A" "%BackupPath%\PAT BTT Text Files\%programName%\New\"
	)
Time /T
pause
exit /b  

Open in new window

my question is :
here if u see the :splitter and :cd42 call function ,both have the same code but at last set filetocopy code only different.. i am calling same code again and again only difference is filetocopy code. is there any chance to write filetocopy code somewhere to call??
0
Bill PrewIT / Software Engineering ConsultantCommented:
You seem to be asking another question, beyond the scope of this question.

It looks like you are trying to use CALL and subroutines already in this large script, but it does not look implemented correctly.  There are several blocks of code that look similar and may be able to be placed in a subroutine that can then be CALLed from your code, but I don't want to just rewrite your code.  You should look at it for repeating chunks of logic and explore CALLing them.

Subroutines (sometimes called "functions") are very useful in BAT scripts, so take the time to understand them and how to pass parameters to them if needed.  A small example is:

@echo off
setlocal

call :ShowValue "First value" "100"
call :ShowValue "Second value" "200"
call :ShowValue "Third value" "300"

exit /b

:ShowValue
    echo %~1 is %~2
    exit /b

Open in new window


»bp
0
sasi vAuthor Commented:
from ur code i tried in if condition
IF %programname%=="CD6"
(
for /f "tokens=*" %%A in ('dir /b /s /a-d "%ProgramPath%\%programName%\%dateFile%\*_CD622N.txt" "%ProgramPath%\%programName%\%dateFile%\*_U611N.txt"
      "%ProgramPath%\%programName%\%dateFile%\*_U625E.txt" "%ProgramPath%\%programName%\%dateFile%\*_U625N.txt" "%ProgramPath%\%programName%\%dateFile%\*_U625S.txt"') do (
    copy "%%~A" "%BackupPath%\PAT BTT Text Files\%programName%\New\"
))      
:exit
IF %programname%=="CD539MCAA" OR IF %programname%=="U540MCAA"
(
for /f "tokens=*" %%A in ('dir /b /s /a-d  "%ProgramPath%\%programName%\%dateFile%\*_CD539MCA.txt" "%ProgramPath%\%programName%\%dateFile%\*_U540MCA.txt"') do (
    copy "%%~A" "%BackupPath%\PAT BTT Text Files\%programName%\New\"
      ))
      :exit

:exit      
Time /T
pause
exit /b       

but its showing syntax is in incorrect..

can u help??
0
sasi vAuthor Commented:
what syntax error in my previous code??
0
sasi vAuthor Commented:
experts???
0
sasi vAuthor Commented:
what happened prew??
0
Bill PrewIT / Software Engineering ConsultantCommented:
There were a number of problems with that code.  I'm not sure exactly how you want it to work, but here are some corrections.  Study them carefully until you understand every change and difference!  A few notes:

  • You were missing quotes around some of the IF statement parameters
  • You can't split most statements across multiple lines
  • I don't know what you were doing with all the :exit labels, but those were not right
  • There is no OR in the IF statement, so I showed a way you can use a flag to get that affect

If you really want to write BAT scripts you need to find a book, and some of the online websites or courses to learn about the syntax and structure.  It would help you save a lot of time.

I don't know if this will do what you want but hopefully it shows a corrected set of code for what it seemed you were trying to do.  Good luck incorporating it into your larger script.

if "%programname%" == "CD6" (
    for /f "tokens=*" %%A in ('dir /b /s /a-d "%ProgramPath%\%programName%\%dateFile%\*_CD622N.txt" "%ProgramPath%\%programName%\%dateFile%\*_U611N.txt" "%ProgramPath%\%programName%\%dateFile%\*_U625E.txt" "%ProgramPath%\%programName%\%dateFile%\*_U625N.txt" "%ProgramPath%\%programName%\%dateFile%\*_U625S.txt"') do (
        copy "%%~A" "%BackupPath%\PAT BTT Text Files\%programName%\New\"
    )      
)

set DoCopy=N
if "%programname%" == "CD539MCAA" set DoCopy=Y
if "%programname%" == "U540MCAA" set DoCopy=Y
if "%DoCopy%" == "Y" (
    for /f "tokens=*" %%A in ('dir /b /s /a-d  "%ProgramPath%\%programName%\%dateFile%\*_CD539MCA.txt" "%ProgramPath%\%programName%\%dateFile%\*_U540MCA.txt"') do (
        copy "%%~A" "%BackupPath%\PAT BTT Text Files\%programName%\New\"
    )
)

Open in new window


»bp
0
sasi vAuthor Commented:
from your code:
if again next program need to add means need to set docopy =n???

code:

if "%programname%" == "CD6" (
    for /f "tokens=*" %%A in ('dir /b /s /a-d "%ProgramPath%\%programName%\%dateFile%\*_CD622N.txt" "%ProgramPath%\%programName%\%dateFile%\*_U611N.txt" "%ProgramPath%\%programName%\%dateFile%\*_U625E.txt" "%ProgramPath%\%programName%\%dateFile%\*_U625N.txt" "%ProgramPath%\%programName%\%dateFile%\*_U625S.txt"') do (
        copy "%%~A" "%BackupPath%\PAT BTT Text Files\%programName%\New\"
    )      
)

set DoCopy=N
if "%programname%" == "CD539MCAA" set DoCopy=Y
if "%programname%" == "U540MCAA" set DoCopy=Y
if "%DoCopy%" == "Y" (
    for /f "tokens=*" %%A in ('dir /b /s /a-d  "%ProgramPath%\%programName%\%dateFile%\*_CD539MCA.txt" "%ProgramPath%\%programName%\%dateFile%\*_U540MCA.txt"') do (
        copy "%%~A" "%BackupPath%\PAT BTT Text Files\%programName%\New\"
    )
)
set DoCopy=N
iF %programName%=="U553" set DoCopy=Y
if "%DoCopy%" == "Y" (
(
for /f "tokens=*" %%A in ('dir /b /s /a-d  "%ProgramPath%\%programName%\%dateFile%\*_U553.txt"') do (
    copy "%%~A" "%BackupPath%\PAT BTT Text Files\%programName%\New\"
)))  

like that goes on????????????????
0
Bill PrewIT / Software Engineering ConsultantCommented:
Do you understand the purpose of the DoCopy variable?


»bp
0
sasi vAuthor Commented:
yes, if its a yes it will go onto the for loop and execute it..
0
Bill PrewIT / Software Engineering ConsultantCommented:
Right, so you reset it to N as the default, do your check(s), setting it to Y if they pass, and then test it.

~bp
0
sasi vAuthor Commented:
I TRIED BUT automatically batch file will closed if its come to that set docopy=n code
i dont know what i did wrong.


code:
del "%BackupPath%\PAT BTT Text Files\%programName%\Old\" /Q
echo Old File Removed
Move "%BackupPath%\PAT BTT Text Files\%programName%\New\*.txt" "%BackupPath%\PAT BTT Text Files\%programName%\Old\"
set DoCopy=N
(
if %programName%=="U540MCAA"  set DoCopy=Y
if "%DoCopy%" == "Y"
(
for /f "tokens=*" %%A in ('dir /b /s /a-d  "%ProgramPath%\%programName%\%dateFile%\*_U540MCA.txt"') do (
    copy "%%~A" "%BackupPath%\PAT BTT Text Files\%programName%\New\"
))
IF %programName%=="CD539MCAA" set DoCopy=Y
if "%DoCopy%" == "Y"
(
for /f "tokens=*" %%A in ('dir /b /s /a-d "%ProgramPath%\%programName%\%dateFile%\*_CD539MCA.txt"') do (
    copy "%%~A" "%BackupPath%\PAT BTT Text Files\%programName%\New\"
      ))

iF %programName%=="U553" set DoCopy=Y
if "%DoCopy%" == "Y"
(
for /f "tokens=*" %%A in ('dir /b /s /a-d  "%ProgramPath%\%programName%\%dateFile%\*_U553.txt"') do (
    copy "%%~A" "%BackupPath%\PAT BTT Text Files\%programName%\New\"
))

IF %programName%=="U554" set DoCopy=Y
if "%DoCopy%" == "Y"
(
for /f "tokens=*" %%A in ('dir /b /s /a-d "%ProgramPath%\%programName%\%dateFile%\*_U554.txt"') do (
    copy "%%~A" "%BackupPath%\PAT BTT Text Files\%programName%\New\"
)))
copy "%ProgramPath%\%programName%\%dateFile%\*_BTT.txt" "%BackupPath%\PAT BTT Text Files\%programName%\New\"

Time /T
pause
exit
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Bat

From novice to tech pro — start learning today.