Copy File:If Empty Or Not Exists - NOTIFY- If Exists - COPY

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
Anonimoouse
Posts: 10
Joined: 19 Jun 2012 13:45

Copy File:If Empty Or Not Exists - NOTIFY- If Exists - COPY

#1 Post by Anonimoouse » 19 Jun 2012 13:50

Hi
I am having source and destination folder locations...I have to check if the file in source is empty or not exists then notify(for which I have the code). What I have to do is delete the file in destination location if any exists and copy form source to destination by renaming the file. I am having multiple files in the source and have to do same for all the files.
Please help me, Thank you !!
Last edited by Anonimoouse on 22 Jun 2012 11:24, edited 1 time in total.

Fawers
Posts: 187
Joined: 08 Apr 2012 17:11
Contact:

Re: Copy File:If Empty Or Not Exists - NOTIFY- If Exists - C

#2 Post by Fawers » 19 Jun 2012 16:45

What are the source and destinations paths?

Anonimoouse
Posts: 10
Joined: 19 Jun 2012 13:45

Re: Copy File:If Empty Or Not Exists - NOTIFY- If Exists - C

#3 Post by Anonimoouse » 19 Jun 2012 17:59

Source and Destination are the directort locations as parameters %SourcePath% and %DestPath%

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Copy File:If Empty Or Not Exists - NOTIFY- If Exists - C

#4 Post by foxidrive » 19 Jun 2012 19:52

Anonimoouse wrote:Hi
What I have to do is delete the file in destination location if any exists and copy form source to destination by renaming the file.


I don't know if Fawers can't understand what you need to do but your description is unclear.

Give some examples and shows us the code you have already.

Anonimoouse
Posts: 10
Joined: 19 Jun 2012 13:45

Re: Copy File:If Empty Or Not Exists - NOTIFY- If Exists - C

#5 Post by Anonimoouse » 19 Jun 2012 21:24

I have this being done for a single file in Source location. Now, I have to modify it to have applied to a bunch of files in Source location.
For the single file the Source location has file FileName.dat and the Destination location will have Final.dat. But in the new requirement I have a number of files named FileName(1).dat, FileName(2).dat, FileName(3).dat.......so on, and these have to be renamed as Final(1).dat, Final(2).dat, Final(3).dat......and so on..
Last edited by Anonimoouse on 22 Jun 2012 11:25, edited 1 time in total.

Fawers
Posts: 187
Joined: 08 Apr 2012 17:11
Contact:

Re: Copy File:If Empty Or Not Exists - NOTIFY- If Exists - C

#6 Post by Fawers » 19 Jun 2012 23:00

That's right - I can't understand neither description nor code.

Here's my bet anyway:

Code: Select all

@echo off
::define your variables
pushd "%SourcePath%"||(echo "SourcePath" is not defined.&pause&exit /b)
for /f "tokens=1,2" %%a in ('dir *.dat') do if /i "%%b" == "file(s)" set /a "datFiles=%%a"
for /l %%a in (1,1,%datFiles%) do echo copy /y "FileName(%%a).dat" "%DestPath%.\Final(%%a).dat"
pause
::remove echo if you see what you expect

Untested; will copy all "filename(number).dat" files to "destpath".
You'll have to change the "file(s)" part in the FOR /F if your OS is not in English.

Aacini
Expert
Posts: 1885
Joined: 06 Dec 2011 22:15
Location: México City, México
Contact:

Re: Copy File:If Empty Or Not Exists - NOTIFY- If Exists - C

#7 Post by Aacini » 19 Jun 2012 23:19

There is a basic point you had not mentioned yet: How do you know that a file not exists? At first I thought that the file name must be the same in both source and destination folders, but you said that the file must be renamed when copied. So, what you are talking about? I further suppose that the files have a number that must be sequential, so if a number in the sequence is missing, then the file "not exists". Is this what you want?

The Batch file below use this idea and works correctly as long as the number of numbered-files be less than 10:

Code: Select all

@echo off
setlocal EnableDelayedExpansion
set SourcePath=C:\SourcePath
set DestPath=C:\DestPath
set i=1
REM pushd %SourcePath%
for %%a in ("File(*).DAT") do (
   for /F "tokens=1,2 delims=()" %%b in ("%%a") do (
      if !i! lss %%c (
         set /A j=%%c-1
         for /L %%i in (!i!,1,!j!) do (
            ECHO/
            ECHO File "File(%%i).DAT does NOT exists"
            ECHO Copy it as "%DestPath%\Rev(%%i).DAT"
         )
         set i=%%c
      )
      if %%~Za equ 0 (
         ECHO/
         ECHO File "File(%%c).DAT is EMPTY"
         ECHO Copy it as "%DestPath%\Rev(%%c).DAT"
      )
   )
   set /A i+=1
)
REM popd

Output wrote:>dir file*.*
Volume in drive C has no label.
Volume Serial Number is CCA1-5338

Directory of C:\Documents and Settings\Antonio\My D...

19/06/2012 10:31 p.m. 7 File(1).DAT
19/06/2012 10:31 p.m. 7 File(2).DAT
19/06/2012 10:31 p.m. 7 File(4).DAT
20/06/2012 12:02 a.m. 0 File(5).DAT
19/06/2012 10:31 p.m. 7 File(8).DAT
5 File(s) 28 bytes
0 Dir(s) 1,833,807,872 bytes free

>test

File "File(3).DAT does NOT exists"
Copy it as "C:\DestPath\Rev(3).DAT"

File "File(5).DAT is EMPTY"
Copy it as "C:\DestPath\Rev(5).DAT"

File "File(6).DAT does NOT exists"
Copy it as "C:\DestPath\Rev(6).DAT"

File "File(7).DAT does NOT exists"
Copy it as "C:\DestPath\Rev(7).DAT"

>

If this is not what you want, then I'm sorry. It is difficult to guess what the OP wants when not enough information is given, and I hate to made several additional questions...

Anonimoouse
Posts: 10
Joined: 19 Jun 2012 13:45

Re: Copy File:If Empty Or Not Exists - NOTIFY- If Exists - C

#8 Post by Anonimoouse » 19 Jun 2012 23:32

I am sorry of not being clear enough. Here's my scenario in a more descriptive way:

I have a folder A as Source location and folder B as destination location.
Folder A has files File(1).dat, File(2).dat, File(3).dat and so on.....

What I have to do is, I have to check in source folder, A if the first file File(1).dat is empty or doesn't exist at all.
If in the case it doesn't exist or the file does exist but is empty, then do nothing.

Now, check the destination location, folder B for any .dat files. If any, then delete them.

Copy the source files in folder A (File(1).dat, File(2).dat, File(3).dat,....) to destination folder B as (Rev(1).dat, Rev(2).dat, Rev(3).dat,.........))

Aacini
Expert
Posts: 1885
Joined: 06 Dec 2011 22:15
Location: México City, México
Contact:

Re: Copy File:If Empty Or Not Exists - NOTIFY- If Exists - C

#9 Post by Aacini » 19 Jun 2012 23:44

This is even funnier than before.

You said:
- In source folder A
IF File(1).DAT is empty or doesn't exist THEN
Do nothing
ELSE
???????
ENDIF

If do nothing if the file doesn't exist or is empty, and do nothing if the file exist, then the whole IF don't cares at all!

Anonimoouse
Posts: 10
Joined: 19 Jun 2012 13:45

Re: Copy File:If Empty Or Not Exists - NOTIFY- If Exists - C

#10 Post by Anonimoouse » 20 Jun 2012 00:11

:idea: Else part is:

check the destination location, folder B for any .dat files. If any, then delete them.
Copy the source files in folder A (File(1).dat, File(2).dat, File(3).dat,....) to destination folder B as (Rev(1).dat, Rev(2).dat, Rev(3).dat,.........))

Sorry, i hope i am clear now. my apologies...

Aacini
Expert
Posts: 1885
Joined: 06 Dec 2011 22:15
Location: México City, México
Contact:

Re: Copy File:If Empty Or Not Exists - NOTIFY- If Exists - C

#11 Post by Aacini » 20 Jun 2012 00:45

This becomes worse at each step! :(

Please, try to follow this description/pseudo-code:

Code: Select all

- FOR each i File(*).DAT in source folder:
-    IF File(i) is empty or doesn't exist THEN
         Do nothing
-    ELSE
        Delete any *.dat files in destination folder.
        Copy the source *.dat files to destination folder Rev(*).dat
-    ENDIF
- NEXT File(i)

1- Previous pseudo-code imply than *.dat files will be deleted in destination folder and *.dat source files will be copied as Rev(*).dat files with every file in source folder, that is, the same delete/copy process will be repeated multiple times!. Although the last time will produce the right final result (the same than all previous times), this is NOT the way to achieve this process.

2- If the delete/copy part must be placed outside the FOR process, then my last question arises again: if the IF does the same in both parts (nothing), then the whole IF don't cares at all: exactly the same result would be obtained if we eliminate the FOR and just execute the delete/copy part!

I'm sorry, I have not anymore time to waste by now... :?

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Copy File:If Empty Or Not Exists - NOTIFY- If Exists - C

#12 Post by foxidrive » 20 Jun 2012 00:48

This is too convoluted to read. :)

I added a post to your other thread doing what you described there and based on code by Aacini. See if that can help you.

Anonimoouse
Posts: 10
Joined: 19 Jun 2012 13:45

Re: Copy File:If Empty Or Not Exists - NOTIFY- If Exists - C

#13 Post by Anonimoouse » 21 Jun 2012 12:46

foxdrive, I am sorry, that's not my case. But here it is:
I have few files in source location to be copied to destination location by renaming them. But before that I have to check if any file exists or is empty(no data in file) in the source location. If it does not exist or exists but is empty in the source, then do nothing.
Else copy the file from source to the destination by renaming it.
Example would be like...
Source has files:
File(1).dat
File(2).dat
File(3).dat
Destination should have:
Rev(1).dat
Rev(2).dat
Rev(3).dat
assuming all files in source are non-empty. Thank you!!

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Copy File:If Empty Or Not Exists - NOTIFY- If Exists - C

#14 Post by foxidrive » 21 Jun 2012 22:44

This is untested.

It assumes that the source files are named "xxxfile(1).dat" and only one "(" character is in the filename.

It will move and rename each DAT file to the destination folder only if the filename "REV(1).DAT" already exists and is not zero bytes.
The "(1).DAT" portion of the target filename is taken from the source filename.

Code: Select all

@echo off
set "dest=d:\target folder\here"
for %%a in ("File(*).DAT") do (
 for /f "tokens=1,2 delims=(" %%b in ("%%a") do (
   if exist "%dest%\Rev(%%c" (
    for /F "delims=" %%z in ("%dest%\REV(%%c") do (
       if %%~zz NEQ 0 (
         del  "%dest%\REV(%%c"
         move "%%a" "%dest%" >nul
         ren "%dest%\%%a" "REV(%%c"
       )
     )
   )
 )
)

Post Reply