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
Batch file needed to copy and delete original files
Moderator: DosItHelp
Re: Batch file needed to copy and delete original files
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.
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
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
Re: Batch file needed to copy and delete original files
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.
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
Re: Batch file needed to copy and delete original files
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
Here is the device location:
ftp://root@123.456.7.890/../folder1/fol ... 3/folder4/"
the recommended user is root
the password is 1234
Re: Batch file needed to copy and delete original files
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
)
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
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

Re: Batch file needed to copy and delete original files
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:
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