Batch file needed to copy and delete original files

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
dewviking
Posts: 3
Joined: 22 Apr 2013 09:43

Batch file needed to copy and delete original files

#1 Post by dewviking » 22 Apr 2013 10:12

Let me start by saying I have never written a batch file, or ever really worked with dos.

My thought was to have windows task scheduler run a batch file each morning to open the network drive, copy all files ending in "_3" to a folder on the local computer (C:\Panel) and delete the original files ending in "_3" on the network drive. The network drive is called "panel" (C:\Users\ADMIN\AppData\Roaming\Microsoft\Windows\Network Shortcuts\panel), and contains a series of files all starting with "mb123", followed by some random numbers and ending with either "_2" or "_3". Each file has the same extension and the network folder repopulates data multiple times per hour.

Does anyone have a suggestion or template I could use as a good starting point?

Thanks,
Daniel

abc0502
Posts: 1007
Joined: 26 Oct 2011 22:38
Location: Egypt

Re: Batch file needed to copy and delete original files

#2 Post by abc0502 » 22 Apr 2013 10:22

is this location "C:\Users\ADMIN\AppData\Roaming\Microsoft\Windows\Network Shortcuts\panel" is a shortcut ?, you can't use a shortcut, provide the real full path to the network drive.

This Should work, it will move files from the network drive location to the local drive location you set in the first two variables. lines 4 and 5.

Code: Select all

@echo off
cls

SET "NetworkDrive=%userprofile%\desktop\Test Folder"
SET "LocalDrive=%userprofile%\desktop"

PUSHD "%NetworkDrive%"
SETLOCAL EnableDelayedExpansion
For /F "delims=" %%A In (' DIR /B /A:-D "*.*" ') Do (
   SET "FileName=%%~nA"
   IF "!FileName:~-2!" == "_3" (
      Move "%%A" "%LocalDrive%\%%A"
   )
)
POPD
pause

If You want it to exit after it move the files, remove the "pause" at the end
Test it and if it work, put it in your schedule task

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

Re: Batch file needed to copy and delete original files

#3 Post by foxidrive » 22 Apr 2013 12:54

One change you can make is to use "mb123*_3.*" to simplify it.

It also uses the /y switch in the Move command to overwrite an existing file with the same name.

Code: Select all

@echo off

SET "NetworkDrive=%userprofile%\desktop\Test Folder"
SET "LocalDrive=%userprofile%\desktop"

PUSHD "%NetworkDrive%"
For /F "delims=" %%A In (' DIR /B /A:-D "mb123*_3.*" ') Do (
      Move /y "%%A" "%LocalDrive%"
)
POPD

pause

dewviking
Posts: 3
Joined: 22 Apr 2013 09:43

Re: Batch file needed to copy and delete original files

#4 Post by dewviking » 23 Apr 2013 10:38

Thanks for the quick replies. I believe one piece of information was left out of my original post on accident. The remote device is a linux based system which requires a user name and password upon initializing an ftp connection. How would I incorporate this into the code?

Here is the device location:
ftp://root@123.456.7.890/../folder1/fol ... 3/folder4/"

the recommended user is root
the password is 1234

abc0502
Posts: 1007
Joined: 26 Oct 2011 22:38
Location: Egypt

Re: Batch file needed to copy and delete original files

#5 Post by abc0502 » 23 Apr 2013 15:25

That Will change every thing,
Just set a FTP connection, ( i didn't try it before between two different systems but it could be the same :?: )

Code: Select all

@Echo OFF

SET "USER=root"
SET "PASS=1234"
SET "Remote=123.456.7.890"
SET "NetworkDrive=%userprofile%\desktop\Test Folder"
SET "LocalDrive=%userprofile%\desktop"

Rem Create Commands File
(
Echo open "%Remote%"
Echo %USER%
Echo %PASS%
Echo lcd "%LocalDrive%"
Echo cd "%NetworkDrive%"
Echo mget mb123*_3.*
Echo mdelete mb123*_3.*
Echo disconnect
Echo Bye
)>"Commands.file"

Rem Connect To Remote PC
Ftp -v -i -s:"Commands.file"

Del /F /Q "Commands.file" >NUL
Exit


Change the NetworkDrive variable to the location inside the remote pc where your files is
But i'm not sure the mget command will work that way and use "mb123*_3.*" as an input ( didn't try that before )
But Test it and we will see :lol:

dewviking
Posts: 3
Joined: 22 Apr 2013 09:43

Re: Batch file needed to copy and delete original files

#6 Post by dewviking » 24 Apr 2013 13:33

I would like to thank everyone for the quick and helpful replies. With a very small amount of manipulation, abc's code exactly met my needs. Thanks again!


Here is the final code for reference:

Code: Select all

@Echo OFF
chdir /C %~dp0

SET "USER=root"
SET "PASS=1234"
SET "Remote=123.456.7.890"
SET "NetworkDrive=%Admin%/Folder1/Folder2/Folder3/Folder4/
SET "LocalDrive=%Admin%\desktop"

Rem Create Commands File
(
Echo open "%Remote%"
Echo %USER%
Echo %PASS%
Echo lcd %~dp0
Echo cd "%NetworkDrive%"
Echo mget mb123*_3.*
Echo mdelete mb123*_3.*
Echo disconnect
Echo Bye
)>"Commands.file"

Rem Connect To Remote PC
Ftp -v -i -s:"Commands.file"

Del /F /Q "Commands.file" >NUL
Exit

Post Reply