please check this batch, work.... but have error during the code run

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
sal21
Posts: 22
Joined: 12 Jul 2016 12:58
Location: Italy

please check this batch, work.... but have error during the code run

#1 Post by sal21 » 09 Jan 2017 06:52

:: need to copy \\Myserverdir\File.txt to C:\mydir\Test_(date_creation).txt

setlocal

:: the following are what I used to test - change to yours!
set folder=\\fils\FTP_Web\OUT
set file=TAB_L0928.txt
set target=\\roms\workarea$\FSRM107\Ges\DATABASE\CEC

:: get the file's date as dd/mm/yyyy

for /f "tokens=1" %%a in ('dir "%folder%\%file%" ^| find /i "%file%"') do set filedate=%%a

:: change the date to yyyymmdd

set date_created=%filedate:~-4,4%%filedate:~-7,2%%filedate:~0,2%

:: perform the copy, if the file is not present in the target

if not exist "%target%\L0928-%date_created%.txt" ^

copy "%folder%\%file%" "%target%\L0928-%date_created%.txt"

:: check on the copied file
dir "%target%\L0928-%date_created%.txt"

endlocal

exit /b 0

aGerman
Expert
Posts: 4678
Joined: 22 Jan 2010 18:01
Location: Germany

Re: please check this batch, work.... but have error during the code run

#2 Post by aGerman » 09 Jan 2017 07:26

Rather try ROBOCOPY if you want to copy files using network paths.

Steffen

Compo
Posts: 600
Joined: 21 Mar 2014 08:50

Re: please check this batch, work.... but have error during the code run

#3 Post by Compo » 09 Jan 2017 11:27

If you use RoboCopy you will unfortunately need to copy and rename though.

Based on a guess at your date output, (09/01/2017), this may work:

Code: Select all

@Echo Off

Set "folder=\\fils\FTP_Web\OUT"
Set "file=TAB_L0928.txt"
Set "target=\\roms\workarea$\FSRM107\Ges\DATABASE\CEC"
Set "new=L0928-%%D%%C%%B.txt"

For /F "EOL= Delims=" %%A In ('Dir "%folder%\%file%"'
) Do For /F "Tokens=1-3 Delims=/ " %%B In ("%%A"
) Do If Not Exist "%target%\%new%" (RoboCopy "%folder%" "%target%" "%file%"
    Ren "%target%\%file%" "%new%")

sal21
Posts: 22
Joined: 12 Jul 2016 12:58
Location: Italy

Re: please check this batch, work.... but have error during the code run

#4 Post by sal21 » 09 Feb 2017 02:35

Compo wrote:If you use RoboCopy you will unfortunately need to copy and rename though.

Based on a guess at your date output, (09/01/2017), this may work:

Code: Select all

@Echo Off

Set "folder=\\fils\FTP_Web\OUT"
Set "file=TAB_L0928.txt"
Set "target=\\roms\workarea$\FSRM107\Ges\DATABASE\CEC"
Set "new=L0928-%%D%%C%%B.txt"

For /F "EOL= Delims=" %%A In ('Dir "%folder%\%file%"'
) Do For /F "Tokens=1-3 Delims=/ " %%B In ("%%A"
) Do If Not Exist "%target%\%new%" (RoboCopy "%folder%" "%target%" "%file%"
    Ren "%target%\%file%" "%new%")


Sorry for delay very busy...
tks for code! work great.
but...

this code overwrite the file if in destination dir just exists the file with same name?
Tks.

Compo
Posts: 600
Joined: 21 Mar 2014 08:50

Re: please check this batch, work.... but have error during the code run

#5 Post by Compo » 09 Feb 2017 03:43

sal21 wrote:Sorry for delay very busy...
tks for code! work great.
but...

this code overwrite the file if in destination dir just exists the file with same name?
Tks.
You can add an additional IF EXIST clause to the code. Also RoboCopy doesn't overwrite files if they are the same, if you wish to preserve one or other of those copies then there are a range of options available.

Type ROBOCOPY /? at the cmd.exe window prompt for those options.

sal21
Posts: 22
Joined: 12 Jul 2016 12:58
Location: Italy

Re: please check this batch, work.... but have error during the code run

#6 Post by sal21 » 09 Feb 2017 04:49

Compo wrote:
sal21 wrote:Sorry for delay very busy...
tks for code! work great.
but...

this code overwrite the file if in destination dir just exists the file with same name?
Tks.
You can add an additional IF EXIST clause to the code. Also RoboCopy doesn't overwrite files if they are the same, if you wish to preserve one or other of those copies then there are a range of options available.

Type ROBOCOPY /? at the cmd.exe window prompt for those options.


ok...
found this option /IS, but were in your code?
tks.

Post Reply