Link to home
Start Free TrialLog in
Avatar of sasi v
sasi v

asked on

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
Avatar of Bill Prew
Bill Prew

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
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
Avatar of sasi v

ASKER

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
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
Avatar of sasi v

ASKER

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
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
Avatar of sasi v

ASKER

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
To copy TXT files simply change this line:

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

to:

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


»bp
Avatar of sasi v

ASKER

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 ??
Is there just one TXT file name to be copied, or are there many?


»bp
Avatar of sasi v

ASKER

one txt file only need to copy.. but many txt files are ter
Avatar of sasi v

ASKER

same like previous code .
need to take specific .txt file from each subfolder.
So what is the name of the TXT file?  And does it exist in multiple folders, or just one?


»bp
Avatar of sasi v

ASKER

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??
ASKER CERTIFIED SOLUTION
Avatar of Bill Prew
Bill Prew

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of sasi v

ASKER

thank you.
filetocopy
if suppose four files means i have to set filetocopy four times in code rite??
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
Avatar of sasi v

ASKER

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??
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
Avatar of sasi v

ASKER

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??
Avatar of sasi v

ASKER

what syntax error in my previous code??
Avatar of sasi v

ASKER

experts???
Avatar of sasi v

ASKER

what happened prew??
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
Avatar of sasi v

ASKER

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????????????????
Do you understand the purpose of the DoCopy variable?


»bp
Avatar of sasi v

ASKER

yes, if its a yes it will go onto the for loop and execute it..
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
Avatar of sasi v

ASKER

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