1.

Solve : Batch file to copy only new files - help?

Answer»

This is my batch file :

@ECHO OFF
FOR %%a IN (f:\data) DO IF NOT EXIST e:\data\%%a COPY f:\data\%a% e:\data
pause

This batch always copy all files from f:\data to e:\data but i want to copy only NEW files.
When i first run this batch - copy files, when i once again run, still copy all files from f:\data even e:\data contains this files. Where is bug ? Please help.Quote from: paveu on November 04, 2013, 12:53:18 PM

Where is bug ?

FOR %%a IN (f:\data) DO IF NOT EXIST e:\data\%%a COPY f:\data\%a% e:\data
^^^
|||
there is an error here

FOR %%a IN (f:\data) DO IF NOT EXIST e:\data\%%a COPY f:\data\%a% e:\data

Quote from: Salmon Trout on November 04, 2013, 01:00:38 PM
FOR %%a IN (f:\data) DO IF NOT EXIST e:\data\%%a COPY f:\data\%a% e:\data
^^^
|||
there is an error here

FOR %%a IN (f:\data) DO IF NOT EXIST e:\data\%%a COPY f:\data\%a% e:\data

No. It's correct. When i changed from %a% to %%a i get error.FOR loop variables in a batch file are 2 percent signs followed by a letter from A to Z or from a to z such as %%a %%R etc.

A variable with a percent sign, then a text string and finally another percent signs like %a% or %size% or %name% is an ordinary batch variable.

If you did not have a set a=something statement PREVIOUSLY, then I expect %a% is blank and thus f:\data\%a% expands to f:\data\

so INTHAT case your command becomes

copy f:\data\ e:\data

Probably you have files with spaces in the names?

Quote from: Salmon Trout on November 04, 2013, 01:39:57 PM
FOR loop variables in a batch file are 2 percent signs followed by a letter from A to Z or from a to z such as %%a %%R etc.

A variable with a percent sign, then a text string and finally another percent signs like %a% or %size% or %name% is an ordinary batch variable.

If you did not have a set a=something statement previously, then I expect %a% is blank and thus f:\data\%a% expands to f:\data\

so inthat case your command becomes

copy f:\data\ e:\data

Probably you have files with spaces in the names?

I checked and i have no spaces in the names. Could you write correct batch file in this case please ?Try this (will also handle filenames with spaces)

FOR %%a IN (f:\data\*.*) DO IF NOT EXIST "e:\data\%%~nxa" COPY "f:\data\%%~nxa" e:\data

Quote from: Salmon Trout on November 04, 2013, 02:07:44 PM
Try this (will also handle filenames with spaces)

FOR %%a IN (f:\data\*.*) DO IF NOT EXIST "e:\data\%%~nxa" COPY "f:\data\%%~nxa" e:\data

It's working ! Thank you


Discussion

No Comment Found